AC: 12 / 12
用时:2 h 21 min
没卡思路,卡了几个测试点。
7-1 输入输出整数
#include <iostream>using namespace std;int main() {int a;cin >> a;cout << a;return 0; }
7-2 调整数组使奇数全部都位于偶数前面其他数字顺序不变
#include <iostream> #include <string>using namespace std;int main() {string s, a;cin >> s;int len = s.size();for(int i = 0; i < len; i++){if(s[i] % 2 == 0) a += s[i];else cout << s[i];}cout << a << endl;return 0; }
逐一读取字符,奇数直接输出,偶数存入字符串a中,最后输出字符串a即可。
7-3 判断回文字符串
#include <iostream> #include <string>using namespace std;int main() {string s;cin >> s;int l = 0, r = s.size() - 1;bool flag = true;while(l < r){if(s[l] != s[r]){flag = false;break;}l++, r--;}cout << (flag ? "YES, " : "NO, ") << (int)s.size() << endl;return 0; }
头尾指针l和r,向中间逐个字符读取,一旦判假则退出循环,循环结束。
注意while循环条件为l <= r即可。
7-4 神密的数列
#include <iostream> #include <string> #include <cstring>using namespace std;const int N = 110; int a[N];int dfs(int x) {if(a[x]) return a[x]; //a[x] != 0说明此项已求出return a[x] = 2 * dfs(x - 1) - dfs(x - 2) + 1; }int main() {int t;cin >> t;a[1] = 1;a[2] = 2;while(t--){int n;cin >> n;cout << dfs(n) << endl;}return 0; }
两个等差数列式子得出二阶等差数列通项,初始化前两项,递归即可。
7-5 杨辉三角
#include <iostream> #include <string>using namespace std;int g[20][20];int main() {int n;cin >> n;if(n == 1) printf(" 1");else{printf(" 1\n 1 1\n");if(n > 2){g[2][1] = g[2][2] = 1;for(int i = 3; i <= n; i++){g[i][1] = g[i][i] = 1;for(int j = 1; j <= i; j++){if(j == 1 || j == i) printf(" 1");else{g[i][j] = g[i - 1][j - 1] + g[i - 1][j];printf("%4d", g[i][j]);}}puts("");}}}return 0; }
初始化前两行,从第三行开始,每行首尾均为1,中间每个数字由上一行两个数字相加得到。
7-6 九连环问题
#include <iostream> #include <string> #include <cstring>using namespace std;const int N = 20; bool st[N]; //false表示处于装上的状态,true表示处于卸下的状态void dfs(int x, char op) //给x号环进行op操作 {if(x == 1) //1号环任意装卸{printf("%d: %c\n", x, op);if(op == 'U') st[x] = false;else st[x] = true;return;}//非1号环,检查是否能直接装卸bool flag = true;if(st[x - 1] == true) flag = false;else{for(int i = x - 2; i >= 1; i--){if(st[i] == false){flag = false;break;}}}if(flag){printf("%d: %c\n", x, op);if(op == 'U') st[x] = false;else st[x] = true;}else //如果不能,则从后往前依次处理,直到x号环可以装卸{if(st[x - 1] == true) dfs(x - 1, 'U');for(int i = x - 2; i >= 1; i--){if(st[i] == false){dfs(i, 'D');}}printf("%d: %c\n", x, op);if(op == 'U') st[x] = false;else st[x] = true;} }int main() {int n;char ch;cin >> n >> ch;if(ch == 'U'){memset(st, true, sizeof st);for(int i = n; i >= 1; i--){if(st[i] == true) dfs(i, 'U');}}else{for(int i = n; i >= 1; i--){if(st[i] == false) dfs(i, 'D');}}return 0; }
参考汉诺塔问题,递归。
7-7 判断上三角矩阵
#include <iostream> #include <string>using namespace std;int g[20][20];int main() {int n;cin >> n;while(n--){int t;cin >> t;for(int i = 1; i <= t; i++)for(int j = 1; j <= t; j++)cin >> g[i][j];bool flag = true;for(int i = 2; i <= t && flag; i++)for(int j = 1; j <= i - 1; j++){if(g[i][j] != 0){flag = false;break;}}cout << (flag ? "YES" : "NO") << endl;}return 0; }
从第二行(如果有)开始判断即可。
7-8 考试座位号
#include <iostream> #include <string> #include <cstring> #include <map>using namespace std;const int N = 110;int n, m; map<int, string> shiji; map<string, int> kaoshi;int main() {scanf("%d", &n);while(n--){string a;int b, c;cin >> a >> b >> c;shiji[b] = a;kaoshi[a] = c;}scanf("%d", &m);while(m--){string a;int b, c;cin >> b;a = shiji[b];c = kaoshi[a];cout << a << ' ' << c << endl;}return 0; }
两个map容器即可。
7-9 吉老师的回归
#include <iostream> #include <algorithm> #include <string>using namespace std;const int N = 110;int n, m;int main() {scanf("%d%d", &n, &m);getchar();string s;while(n--){getline(cin, s);if(s.find("easy") != s.npos || s.find("qiandao") != s.npos){continue;}if(m == 0){cout << s << endl;break;}m--;}if(n == -1) puts("Wo AK le");return 0; }
注意判断当前已做题目数量和m关系的代码位置,测试点2答案错误的代码如下:
#include <iostream> #include <algorithm> #include <string>using namespace std;const int N = 110;int n, m;int main() {scanf("%d%d", &n, &m);getchar();string s;while(n--){getline(cin, s);if(m == 0){cout << s << endl;break;}if(s.find("easy") != s.npos || s.find("qiandao") != s.npos){continue;}else m--;}if(n == -1) puts("Wo AK le");return 0; }
7-10 病人排队
#include <iostream> #include <algorithm> #include <string>using namespace std;const int N = 110;struct Node {string id;int year;int order;bool isOld; }node[N];int n;bool cmp(Node a, Node b) {if(a.isOld == b.isOld){if(a.isOld == true){if(a.year != b.year) return a.year > b.year;else return a.order < b.order;}else return a.order < b.order;}else return a.isOld > b.isOld; }int main() {scanf("%d", &n);for(int i = 0; i < n; i++){string a;int b;bool c = false;cin >> a >> b;if(b >= 60) c = true;node[i] = {a, b, i, c};}sort(node, node + n, cmp);for(int i = 0; i < n; i++) cout << node[i].id << endl;return 0; }
根据题目要求写好cmp比较函数然后sort,依次打印id即可。
7-11 黑洞数
#include <iostream> #include <algorithm> #include <string>using namespace std;int main() {string s;cin >> s;int a, b, c;for(int i = 1; ; i++){sort(s.begin(), s.end());b = stoi(s);reverse(s.begin(), s.end());a = stoi(s);c = a - b;printf("%d: %d - %d = %d\n", i, a, b, c);if(c == 495) break;else s = to_string(c);}return 0; }
善用字符串和整型互相转换的函数即可。
7-12 h0034. 平方矩阵 II
#include <iostream> #include <algorithm> #include <string>using namespace std;const int N = 110;int g[N][N];int main() {int n;for(int i = 1; i <= 100; i++){for(int j = i; j >= 1; j--)g[i + 1 - j][i] = g[i][i + 1 - j] = j;}while(scanf("%d", &n), n){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){printf("%d ", g[i][j]);}puts("");}puts("");}return 0; }
先预处理好100阶的矩阵,然后根据要求打印即可。