CF1447A Add Candies
题目链接:https://codeforces.com/problemset/problem/1447/A
题目大意:
给一个长度为n的数组,第i个数为i(从1开始),进行m次操作,第j次操作时,选择一个数,除它以外每一个数都加上j,问如何操作可以使所有数都相等。(m基本没有限制,而且不要求m最小)
解题思路:
每一次的操作,可以看成将选择的数减去j(与,“每次选一个数,除它以外所有数都加j” 的效果相同),因为要操作的数是1到n,所以我们按顺序选,第一次操作选1,让1减去1,第二次操作选2,以此类推即可。
参考代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #define N 10010 10 #define ll long long 11 using namespace std; 12 13 inline int read() 14 { 15 int x = 0, y = 1; char c = getchar(); 16 while(c < '0' || c > '9') {if(c == '-') y = -1; c = getchar();} 17 while(c >= '0' && c <= '9') x = x*10+c-'0', c = getchar(); 18 return x*y; 19 } 20 21 int main() 22 { 23 int t,n; 24 t = read(); 25 while(t--) 26 { 27 n = read(); 28 cout << n << endl; 29 for(int i = 1; i <= n; i++) 30 { 31 cout << i << " "; 32 } 33 cout << endl; 34 } 35 return 0; 36 }

浙公网安备 33010602011771号