From 8dceb2d87e99cc50b98970661f6410ad9032327f Mon Sep 17 00:00:00 2001 From: zhangzc Date: Thu, 28 Oct 2021 16:17:50 +0800 Subject: [PATCH] update exercises --- .../solution.cpp" | 59 +++--- .../solution.md" | 179 +++++++++++++++++- 2 files changed, 198 insertions(+), 40 deletions(-) diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.cpp" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.cpp" index 2a1e7dd0c..11d0fb656 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.cpp" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.cpp" @@ -1,51 +1,40 @@ -/* - Name: 蓝桥杯:带分数(全排列) - Copyright: 本算法由KeepThinking_提供 - Author: Jopus - Date: 08/02/14 19:57 - Description: dev-cpp 5.5.3 -*/ + #include -/*思路:将list[1,2,3,4,5,6,7,8,9]数组进行全排列,然后对于每一种排列进行处理 -处理方法:将list数组划分为三部分a,b,c,判断是否满足number == a+b/c && b%c == 0; -具体见分析......... -*/ int x = 0, number = 0, count = 0; -//交换a,b两数 void Swap(int &a, int &b) { int temp = a; a = b; b = temp; } -//将数组区间转化为数字 + int getNum(int list[], int f, int r) { int i = 0, num = 0; for (i = f; i <= r; i++) - num = list[i] + num * 10; //进位 + num = list[i] + num * 10; return num; } -//进行全排列并对每种排列结果进行处理 + void Prim(int list[], int k, int m) { - if (k == m - 1) //前缀是最后一个位置,此时出现一种排列数. + if (k == m - 1) { - int a = 0, b = 0, c = 0, bLast = 0; //带分数:a+b/c - for (int i = 0; i < x; i++) //i表示a的末尾位置,不会超过number位数 + int a = 0, b = 0, c = 0, bLast = 0; + for (int i = 0; i < x; i++) { - a = getNum(list, 0, i); //将list数组中的[0-i]转化为数字,赋值给a - bLast = ((number - a) * list[8]) % 10; //确定b最后一个数字 - for (int j = i + (8 - i) / 2; j < 8; j++) //从list数组中间位置开始找b末尾位置 + a = getNum(list, 0, i); + bLast = ((number - a) * list[8]) % 10; + for (int j = i + (8 - i) / 2; j < 8; j++) { - if (list[j] == bLast) //找到b尾部 + if (list[j] == bLast) { - b = getNum(list, i + 1, j); //将list数组中的[i+1-j]转化为数字,赋值给b - c = getNum(list, j + 1, 8); //将list数组中的[j+1-8]转化为数字,赋值给c - if (b % c == 0 && a + b / c == number) //判断合理性 + b = getNum(list, i + 1, j); + c = getNum(list, j + 1, 8); + if (b % c == 0 && a + b / c == number) { - // printf("%d+%d/%d\n",a,b,c); //打印每种情况 + ++count; } break; @@ -55,29 +44,29 @@ void Prim(int list[], int k, int m) } else { - for (int i = k; i < m; i++) //全排列数组list[] + for (int i = k; i < m; i++) { - //交换前缀,使之产生下一个前缀. - Swap(list[k], list[i]); //>>A - Prim(list, k + 1, m); //>>B - //将前缀换回来,继续做上一个的前缀排列.//>>C + + Swap(list[k], list[i]); + Prim(list, k + 1, m); + Swap(list[k], list[i]); } } } -//主函数 + int main() { int temp = 0; - int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //定义全排列数组 + int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; scanf("%d", &number); temp = number; - while (temp != 0) //统计number总共多少位 + while (temp != 0) { ++x; temp /= 10; } Prim(list, 0, 9); - printf("%d", count); //打印总共多少种 + printf("%d", count); return 0; } \ No newline at end of file diff --git "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.md" "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.md" index c72981126..433f759dd 100644 --- "a/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.md" +++ "b/data/1.\347\256\227\346\263\225\345\210\235\351\230\266/1.\350\223\235\346\241\245\346\235\257/\345\270\246\345\210\206\346\225\260/solution.md" @@ -36,30 +36,199 @@ ## aop ### before ```cpp +#include +int x = 0, number = 0, count = 0; + +void Swap(int &a, int &b) +{ + int temp = a; + a = b; + b = temp; +} + +int getNum(int list[], int f, int r) +{ + int i = 0, num = 0; + for (i = f; i <= r; i++) + num = list[i] + num * 10; + return num; +} ``` ### after ```cpp - +int main() +{ + int temp = 0; + int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + scanf("%d", &number); + temp = number; + while (temp != 0) + { + ++x; + temp /= 10; + } + Prim(list, 0, 9); + printf("%d", count); + return 0; +} ``` ## 答案 ```cpp - +void Prim(int list[], int k, int m) +{ + if (k == m - 1) + { + int a = 0, b = 0, c = 0, bLast = 0; + for (int i = 0; i < x; i++) + { + a = getNum(list, 0, i); + bLast = ((number - a) * list[8]) % 10; + for (int j = i + (8 - i) / 2; j < 8; j++) + { + if (list[j] == bLast) + { + b = getNum(list, i + 1, j); + c = getNum(list, j + 1, 8); + if (b % c == 0 && a + b / c == number) + { + + ++count; + } + break; + } + } + } + } + else + { + for (int i = k; i < m; i++) + { + + Swap(list[k], list[i]); + Prim(list, k + 1, m); + + Swap(list[k], list[i]); + } + } +} ``` ## 选项 ### A ```cpp - +void Prim(int list[], int k, int m) +{ + if (k == m - 1) + { + int a = 0, b = 0, c = 0, bLast = 0; + for (int i = 0; i < x; i++) + { + a = getNum(list, 0, i); + bLast = ((number - a) * list[8]) % 10; + for (int j = i + (8 - i) / 2; j < 8; j++) + { + if (list[j] == bLast) + { + b = getNum(list, i, j); + c = getNum(list, j, 8); + if (b % c == 0 && a + b / c == number) + { + + ++count; + } + break; + } + } + } + } + else + { + for (int i = k; i < m; i++) + { + + Swap(list[k], list[i]); + Prim(list, k + 1, m); + + Swap(list[k], list[i]); + } + } +} ``` ### B ```cpp - +void Prim(int list[], int k, int m) +{ + if (k == m - 1) + { + int a = 0, b = 0, c = 0, bLast = 0; + for (int i = 0; i < x; i++) + { + a = getNum(list, 0, i); + bLast = ((number - a) * list[8]) % 10; + for (int j = i + (8 - i) / 2; j < 8; j++) + { + + b = getNum(list, i, j); + c = getNum(list, j, 8); + if (b % c == 0 && a + b / c == number) + { + + ++count; + } + break; + } + } + } + else + { + for (int i = k; i < m; i++) + { + + Swap(list[k], list[i]); + Prim(list, k + 1, m); + + Swap(list[k], list[i]); + } + } +} ``` ### C ```cpp - +void Prim(int list[], int k, int m) +{ + if (k == m - 1) + { + int a = 0, b = 0, c = 0, bLast = 0; + for (int i = 0; i < x; i++) + { + a = getNum(list, 0, i); + bLast = ((number - a) * list[8]) % 10; + for (int j = i + (8 - i) / 2; j < 8; j++) + { + if (list[j] == bLast) + { + b = getNum(list, i + 1, j); + c = getNum(list, j + 1, 8); + ++count; + break; + } + } + } + } + else + { + for (int i = k; i < m; i++) + { + + Swap(list[k], list[i]); + Prim(list, k + 1, m); + + Swap(list[k], list[i]); + } + } +} ``` -- GitLab