标签:break using out 元素 题解 struct 不能 lap amp
div4。。基本上都是构造题,水一波题解
就把每一位拆出来
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, a[10]; void solve() { cin >> n; int id = 0; for (int i = 1; n > 0; i *= 10, n /= 10) if (i * (n % 10) != 0) a[++id] = i * (n % 10); cout << id << endl; rep(i, 1, id) cout << a[i] << " "; cout << endl; } int main() { int t; cin >> t; while (t--) { solve(); } }
问能不能将n分为k个奇数或k个偶数的和,分情况讨论吧
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, k; void solve() { cin >> n >> k; if (!((n & 1) ^ (k & 1)) && n >= k) { puts("YES"); rep(i, 1, k - 1) cout << "1 "; cout << n - k + 1 << endl; } else if (!(n & 1) && n >= 2 * k) { puts("YES"); rep(i, 1, k - 1) cout << "2 "; cout << n - k * 2 + 2 << endl; } else puts("NO"); } int main() { int t; cin >> t; while (t--) { solve(); } }
找到第k个不被n整除的数,规律也很明显
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) ll n, k; void solve() { cin >> n >> k; cout << (k - 1) / (n - 1) + k << endl; } int main() { int t; cin >> t; while (t--) { solve(); } }
alice和bob从两端一次吃糖,每次要吃的比对方多,简单的模拟就好了
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) ll n, a[1010]; void solve() { cin >> n; rep(i, 1, n) cin >> a[i]; int l = 1, r = n, sum1 = 0, sum2 = 0, ans1 = 0, ans2 = 0; int cnt = 0; while (l <= r) { sum1 = 0; cnt++; while (l <= r && sum1 <= sum2) { sum1 += a[l]; ans1 += a[l]; l++; } if (l <= r) cnt++; sum2 = 0; while (l <= r && sum2 <= sum1) { sum2 += a[r]; ans2 += a[r]; r--; } } cout << cnt << " " << ans1 << " " << ans2 << endl; } int main() { int t; cin >> t; while (t--) { solve(); } }
E题我想得太难了,实际上就n2也能过。
序列中找到能用序列中连续元素和表示的数的个数
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, a[8010]; int sum[8010]; int vis[8010]; int ans; void solve() { cin >> n; ans = 0; rep(i, 1, n) vis[i] = 0; rep(i, 1, n) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; vis[a[i]]++; } rep(i, 2, n) for (int j = i - 1; j; j--) { if (sum[i] - sum[j - 1] > n) break; if (vis[sum[i] - sum[j - 1]]) { ans += vis[sum[i] - sum[j - 1]]; vis[sum[i] - sum[j - 1]] = 0; } } cout << ans << endl; } int main() { int t; cin >> t; while (t--) { solve(); } }
构造01字符串,使字符串每相邻两项拿出来,(00),(01,10),(11)的个数分别为n0,n1,n2
我是按n2,n0,n1的顺序构造的,细节看代码
int n0, n1, n2; void solve() { cin >> n0 >> n1 >> n2; if (n0 == 0 && n1 == 0) rep(i, 0, n2) putchar(‘1‘); else if (n1 == 0 && n2 == 0) rep(i, 0, n0) putchar(‘0‘); else { rep(i, 0, n2) putchar(‘1‘); rep(j, 0, n0) putchar(‘0‘); rep(i, 1, n1 - 1) putchar(i & 1 ? ‘1‘ : ‘0‘); } cout << endl; } int main() { int t; cin >> t; while (t--) { solve(); } }
构造一个数组,使相邻两项差值在2到4之间
我先开始考虑的是从按差为2变化,比如8:8,6,4,2,1,3,5,7。多列举几项发现只需要改一下中间1-4的顺序就可以了
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n; void solve() { cin >> n; if (n == 2 || n == 3) { puts("-1"); return; } int tmp = n; while (n >= 5) { cout << n << " "; n -= 2; } if (n == 3) n = 6; if (n == 4) n = 5; cout << "3 1 4 2 "; while (n <= tmp) { cout << n << " "; n += 2; } cout << endl; } int main() { int t; cin >> t; while (t--) { solve(); } }
标签:break using out 元素 题解 struct 不能 lap amp
原文地址:https://www.cnblogs.com/likunhong/p/12861883.html