intmain(){ string s; cin >> s; int sum = 0; for (auto v : s) { sum += v - '0'; } string t = to_string(sum); for (int i = 0; i < t.size(); i++) { if (i != 0) cout << " "; cout << a[t[i] - '0']; } return0; }
1006 换个格式输出整数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream>
usingnamespace std;
intmain(){ int n; cin >> n; int a, b, c; // 分别代表百位,十位,个位 a = n / 100; b = (n / 10) % 10; c = n % 10; while (a--) cout << 'B'; while (b--) cout << 'S'; for (int i = 1; i <= c; i++) cout << i; return0; }
voidreverse(int x, int y){ // 将数组a中下标从x到y的部分翻转 for (int i = x, j = y; i < j; i++, j--) { int t = a[i]; a[i] = a[j]; a[j] = t; } }
intmain(){ cin >> n >> m; m = m % n; for (int i = 1; i <= n; i++) cin >> a[i]; reverse(1, n); reverse(1, m); reverse(m + 1, n); for (int i = 1; i <= n; i++) { if (i > 1) cout << " "; cout << a[i]; } return0; }
intmain(){ int n; cin >> n; for (int i = 1; i <= n; i++) { LL a, b, c; cin >> a >> b >> c; printf("Case #%d: ", i); if (a + b > c) { cout << "true" << endl; } else { cout << "false" << endl; } } return0; }
voidfindAndPrintPrime(int a, int b){ bool flag = true; int cnt = 0; int t = 0; for (int i = 2; flag; i++) { bool isPrime = true; for (int j = 2; j * j <= i; j++) { if (i % j == 0) isPrime = false; } if (isPrime) { cnt++; if (cnt >= a && cnt <= b) { t++; if (t == 1) cout << i; else cout << " " << i; if (t == 10) { cout << endl; t = 0; } } if (cnt == b) flag = false; } } }
intmain(){ int a, b; cin >> a >> b; findAndPrintPrime(a, b); return0; }
voidprintKary(int sum, int k){ vector<int> v; while (sum) { int d = sum % k; v.push_back(d); sum /= k; } for (int i = v.size() - 1; i >= 0; i--) { cout << v[i]; } }
intmain(){ int a, b, d, sum; cin >> a >> b >> d; sum = a + b; if (sum == 0) cout << 0 << endl; elseprintKary(sum, d); return0; }
intmain(){ int n, m, t; cin >> n; for (int i = 0; i < n; i++) { cin >> t; score[t]++; } cin >> m; for (int i = 0; i < m; i++) { cin >> t; if (i > 0) cout << ' '; cout << score[t]; } return0; }
int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0};
intmain(){ cin >> x; int y = sqrt(x); if (y * y == x) n = y, m = y; else { while (y * y < x) y++; while (x % y != 0) y++; n = y, m = x / n; } vector<vector<int>> ans(n, vector<int>(m)); vector<vector<bool>> vis(n, vector<bool>(m)); for (int i = 0; i < x; i++) cin >> p[i]; sort(p, p + x); reverse(p, p + x); int a = 0, b = 0, cnt = 0, k = 0; while (cnt < x) { ans[a][b] = p[cnt]; vis[a][b] = true; cnt++; int xx = a + dx[k], yy = b + dy[k]; if (xx < 0 || xx >= n || yy < 0 || yy >= m || vis[xx][yy]) k = (k + 1) % 4; a = a + dx[k], b = b + dy[k]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (j == 0) cout << ans[i][j]; else cout << " " << ans[i][j]; } cout << endl; } return0; }
intmain(){ scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { if (a[i] > n - i + 1) { ans = n - i + 1; break; } } cout << ans << endl; return0; }
int score[N]; // 记录每一题分数 int correct[N]; // 记录每一题正确答案 int n, m;
intmain(){ cin >> n >> m; for (int i = 1; i <= m; i++) cin >> score[i]; for (int i = 1; i <= m; i++) cin >> correct[i]; while (n--) { int ans = 0, t; for (int i = 1; i <= m; i++) { cin >> t; if (t == correct[i]) { ans += score[i]; } } cout << ans << endl; } return0; }
intgcd(int a, int b){ // 返回a与b的最大公约数 return b ? gcd(b, a % b) : a; }
intmain(){ int a, b, c, d, k; char ch; cin >> a >> ch >> b >> c >> ch >> d >> k; if ((double)a / b > (double)c / d) { // 题干并没有说前面的分数小于后面的分数,所以为了方便起见保证:前<后 swap(a, c); swap(b, d); } bool first = true; // 用于卡输出格式 for (int i = 1; i < k; i++) { if (a * k >= b * i || i * d >= k * c) continue; // 如果该分数不在给定的两个分数之间 if (gcd(i, k) == 1) { // 若是最简分式 if (first) { cout << i << '/' << k; first = false; } else { cout << ' ' << i << '/' << k; } } } return0; }
intmain(){ int n, a, b; double ans = 0.0, t; cin >> n; while (n--) { cin >> a >> b; t = sqrt(a * a + b * b); if (t > ans) ans = t; } printf("%.2f", ans); return0; }
intmain(){ cin >> n >> m >> a >> b >> k; while (n--) { for (int i = 0; i < m; i++) { scanf("%d", &t); // 这里用cin会超时 if (t >= a && t <= b) t = k; // 输出 if (i == 0) printf("%03d", t); elseprintf(" %03d", t); } cout << endl; } return0; }
intmain(){ cin >> m >> n >> s; if (s > m) { // 如果没有人抽奖直接按题干输出 cout << "Keep going..." << endl; return0; } for (int i = 1; i <= s - 1; i++) cin >> t; // 跳过前面不中奖的人 int cnt = 0; for (int i = s; i <= m; i++) { cin >> t; if (st.count(t) != 0) continue; // 如果之前抽到过,直接continue if (cnt % n == 0) { // n个一循环 cout << t << endl; st.insert(t); // 用set记录中间名单 } cnt++; } return0; }
intmain(){ int a, b, ans; cin >> a >> b; ans = a * b; string s = to_string(ans); reverse(s.begin(), s.end()); int i = 0; while (s[i] == '0') i++; // 跳过前导零 for (;i < s.size(); i++) cout << s[i]; return0; }
intmain(){ cin >> d >> n; pre = pre + to_string(d); cur = pre; // 防止n为1的情况 for (int i = 2; i <= n; i++) { cur = ""; for (int l = 0, r = 0; l < pre.size(); l = r) { r = l; while (pre[r] == pre[l]) { r++; } cur += pre[l]; cur += to_string(r - l); } pre = cur; } cout << cur << endl; return0; }
intmain(){ int n, cnt = 0; cin >> n; for (int i = 1; i <= n; i++) { int num = get(i); if (!st[num]) { st[num] = true; cnt++; } } cout << cnt << endl; return0; }
boolcheck(int x){ int t = x, p = 1; while (t) { p *= 10; t /= 10; } for (int i = 1; i <= 9; i++) { if (i * x * x % p == x) { k = i; s = i * x * x; returntrue; } } returnfalse; }
intmain(){ cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (check(a[i])) { cout << k << " " << s << endl; } else { cout << "No" << endl; } } return0; }