From 64162ac57c4f69243fee6ecdb172538b77181365 Mon Sep 17 00:00:00 2001 From: zhangzc Date: Thu, 28 Oct 2021 18:32:50 +0800 Subject: [PATCH] update exercises --- .../solution.md" | 16 +- .../solution.cpp" | 113 ++++----- .../solution.md" | 219 +++++++++++++++++- 3 files changed, 264 insertions(+), 84 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/\347\254\254\345\207\240\345\244\251/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/\347\254\254\345\207\240\345\244\251/solution.md" index 3e11bdb95..850f185c4 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/\347\254\254\345\207\240\345\244\251/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/\347\254\254\345\207\240\345\244\251/solution.md" @@ -44,9 +44,9 @@ int main() int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - if (is_leap(y)) //判断闰年 + if (is_leap(y)) { - for (int i = 0; i < (m - 1); i++) //记录1-(m-1)月天数 + for (int i = 0; i < (m - 1); i++) { ans += L_m_d[i]; } @@ -76,9 +76,9 @@ int main() int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - if (is_leap(y)) //判断闰年 + if (is_leap(y)) { - for (int i = 0; i < m; i++) //记录1-(m-1)月天数 + for (int i = 0; i < m; i++) { ans += L_m_d[i]; } @@ -106,9 +106,9 @@ int main() int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - if (is_leap(y)) //判断闰年 + if (is_leap(y)) { - for (int i = 0; i < (m - 1); i++) //记录1-(m-1)月天数 + for (int i = 0; i < (m - 1); i++) { ans += L_m_d[i]; } @@ -136,9 +136,9 @@ int main() int L_m_d[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nonL_m_d[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - if (is_leap(y)) //判断闰年 + if (is_leap(y)) { - for (int i = 0; i < m; i++) //记录1-(m-1)月天数 + for (int i = 0; i < m; i++) { ans += L_m_d[i]; } 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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/solution.cpp" index 7f775b8b9..14d84f349 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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/solution.cpp" @@ -1,84 +1,55 @@ #include -#include -#include using namespace std; - -void i2s(int num, string &str) -{ //将数字转换为字符串 - stringstream ss; //用法类似于cout - ss << num; //>>是将那个num传入到stringstream中 - ss >> str; //用stringstream 类的函数str() 将对象所包含的内容赋给一个string对象。 -} - -void s2i(string &str, int &num) -{ - stringstream ss; //将字符串反转为数字 - ss << str; - ss >> num; -} -char to(char x) -{ //字符的翻转 - if (x == '6') - return '9'; - else if (x == '9') - return '6'; - else - return x; //x:指0,1,2,5,8,这几个数字反转后还是本身 -} - -string reserve(const string &str) -{ //reserve作用类似旋转180° - string ans; //结果 - for (int i = 3; i >= 0; i--) - { //扫描,从最后一位开始转换之后 - ans.insert(ans.end(), to(str[i])); //把最后一个字符插入 - } - return ans; -} -struct price -{ //定义一个价格的结构体 - int a, b, c; //a:原始价格 ,b:翻转颠倒价格,c:原始价格与颠倒价格的差 -}; -vector v1; //存储-200多的 -vector v2; //存储+800多的 int main() { - cout << reserve << endl; //在这个地方可以测试原题中给出的数据 - //枚举所有可以颠倒的四位数 - // 将其颠倒过来,与原来的数值做差,将-200多与800多记录下来,分别记录在两个集合中 - //遍历两个集合,-200多和800多两两求和,结果为558的即为正确答案 - for (int i = 1000; i < 10000; i++) + int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字 + int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字 + int profit1[1111][2]; //利润1 + int profit2[1111][2]; //利润2 + int before_reverse; //颠倒前 + int after_reverse; //颠倒后 + int i = 0; + int j = 0; + for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0 { - string str; - i2s(i, str); - if (str.find('3') != string::npos || str.find('4') != string::npos || str.find('7') != string::npos || str.rfind('0') == 3) - //str里面是i的字符串形式,里面的3,4,7,还有最后一位也就是数组第4位不能为0 - continue; - string r = reserve(str); //翻转的字符 - int r_int; - s2i(r, r_int); //r_int就是翻转后的价格,i是原始价格 - int plus = r_int - i; //plus就是价格差 - if (plus > -300 && plus < -200) - { //价格范围 - price p = {i, r_int, plus}; - v1.push_back(p); - } - else if (plus > 800 && plus < 900) - { //价格范围 - price p = {i, r_int, plus}; - v2.push_back(p); - } - //此时v1存储-200多的价格,v2存储+800多 - for (int i = 0; i < v1.size(); i++) + for (int b = 0; b < 7; b++) { - for (int j = 0; j < v2.size(); j++) + for (int c = 0; c < 7; c++) { - if (v1[i].c + v2[j].c == 558) + for (int d = 0; d < 7; d++) { - printf("%d %d %d %d %d %d\n", v1[i].a, v1[i].b, v1[i].c, v2[j].a, v2[j].b, v2[j].c); + before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d]; + after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a]; + //因为是颠倒之后所以第一位可以是0 + if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200) + //在200-300范围内,我们以正数为准 + { + profit1[i][0] = before_reverse; + profit1[i][1] = after_reverse - before_reverse; + i++; + } + else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900) + //在800-900范围内 + { + profit1[j][0] = before_reverse; + profit2[j][1] = after_reverse - before_reverse; + j++; + } } } } } + int answer = 0; + for (int a = 0; a < i; a++) + { + for (int b = 0; b < j; b++) + { + if (profit1[a][1] + profit2[b][1] == 558) + { + answer = profit1[a][0]; + } + } + } + cout << answer << endl; 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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/solution.md" index 95c4f3271..f6531270e 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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/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/\351\242\240\345\200\222\347\232\204\344\273\267\347\211\214/solution.md" @@ -14,7 +14,8 @@ ## aop ### before ```cpp - +#include +using namespace std; ``` ### after ```cpp @@ -23,21 +24,229 @@ ## 答案 ```cpp - +int main() +{ + int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字 + int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字 + int profit1[1111][2]; //利润1 + int profit2[1111][2]; //利润2 + int before_reverse; //颠倒前 + int after_reverse; //颠倒后 + int i = 0; + int j = 0; + for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0 + { + for (int b = 0; b < 7; b++) + { + for (int c = 0; c < 7; c++) + { + for (int d = 0; d < 7; d++) + { + before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d]; + after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a]; + //因为是颠倒之后所以第一位可以是0 + if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200) + //在200-300范围内,我们以正数为准 + { + profit1[i][0] = before_reverse; + profit1[i][1] = after_reverse - before_reverse; + i++; + } + else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900) + //在800-900范围内 + { + profit1[j][0] = before_reverse; + profit2[j][1] = after_reverse - before_reverse; + j++; + } + } + } + } + } + int answer = 0; + for (int a = 0; a < i; a++) + { + for (int b = 0; b < j; b++) + { + if (profit1[a][1] + profit2[b][1] == 558) + { + answer = profit1[a][0]; + } + } + } + cout << answer << endl; + return 0; +} ``` ## 选项 ### A ```cpp - +int main() +{ + int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字 + int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字 + int profit1[1111][2]; //利润1 + int profit2[1111][2]; //利润2 + int before_reverse; //颠倒前 + int after_reverse; //颠倒后 + int i = 0; + int j = 0; + for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0 + { + for (int b = 0; b < 7; b++) + { + for (int c = 0; c < 7; c++) + { + for (int d = 0; d < 7; d++) + { + before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d]; + after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a]; + //因为是颠倒之后所以第一位可以是0 + if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200) + //在200-300范围内,我们以正数为准 + { + profit1[i][0] = before_reverse; + profit1[i][1] = after_reverse - before_reverse; + i++; + } + else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900) + //在800-900范围内 + { + profit1[j][0] = before_reverse; + profit2[j][1] = after_reverse - before_reverse; + j++; + } + } + } + } + } + int answer = 0; + for (int a = 0; a < i; a++) + { + for (int b = 0; b < j; b++) + { + if (profit1[a][1] + profit2[b][1] == 558) + { + answer = profit1[b][1]; + } + } + } + cout << answer << endl; + return 0; +} ``` ### B ```cpp - +int main() +{ + int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字 + int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字 + int profit1[1111][2]; //利润1 + int profit2[1111][2]; //利润2 + int before_reverse; //颠倒前 + int after_reverse; //颠倒后 + int i = 0; + int j = 0; + for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0 + { + for (int b = 0; b < 7; b++) + { + for (int c = 0; c < 7; c++) + { + for (int d = 0; d < 7; d++) + { + before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d]; + after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a]; + //因为是颠倒之后所以第一位可以是0 + if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200) + //在200-300范围内,我们以正数为准 + { + profit1[i][0] = before_reverse; + profit1[i][1] = after_reverse - before_reverse; + i++; + } + else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900) + //在800-900范围内 + { + profit1[j][0] = before_reverse; + profit2[j][1] = after_reverse - before_reverse; + j++; + } + } + } + } + } + int answer = 0; + for (int a = 0; a < i; a++) + { + for (int b = 0; b < j; b++) + { + if (profit1[a][1] + profit2[b][1] == 558) + { + answer = profit1[b][0]; + } + } + } + cout << answer << endl; + return 0; +} ``` ### C ```cpp - +int main() +{ + int num1[7] = {0, 1, 2, 5, 6, 8, 9}; //价牌包含的数字 + int num2[7] = {0, 1, 2, 5, 9, 8, 6}; //价牌翻转对应的数字 + int profit1[1111][2]; //利润1 + int profit2[1111][2]; //利润2 + int before_reverse; //颠倒前 + int after_reverse; //颠倒后 + int i = 0; + int j = 0; + for (int a = 1; a < 7; a++) //从1开始,第一位数字不可以是0 + { + for (int b = 0; b < 7; b++) + { + for (int c = 0; c < 7; c++) + { + for (int d = 0; d < 7; d++) + { + before_reverse = num1[a] * 1000 + num1[b] * 100 + num1[c] * 10 + num1[d]; + after_reverse = num2[d] * 1000 + num2[c] * 100 + num2[b] * 10 + num2[a]; + //因为是颠倒之后所以第一位可以是0 + if (after_reverse - before_reverse > -300 && after_reverse - before_reverse < -200) + //在200-300范围内,我们以正数为准 + { + profit1[i][0] = before_reverse; + profit1[i][1] = after_reverse - before_reverse; + i++; + } + else if (after_reverse - before_reverse > 800 && after_reverse - before_reverse < 900) + //在800-900范围内 + { + profit1[j][0] = before_reverse; + profit2[j][1] = after_reverse - before_reverse; + j++; + } + } + } + } + } + int answer = 0; + for (int a = 0; a < i; a++) + { + for (int b = 0; b < j; b++) + { + if (profit1[a][1] + profit2[b][1] == 558) + { + answer = profit1[0][b]; + } + } + } + cout << answer << endl; + return 0; +} ``` -- GitLab