A - Sum of Round Numbers 链接:Problem - 1352A - Codeforces 这个题难度较低,题意就是问一个数字最少可以用几个整十数表示出来,统计一下这个数字里非零的数,然后乘上它对应的十位数的权值输出即可。 下面是一个比较麻烦的代码,把数字转成字符串来储存了,然后去挨个统计计算。 用 int 类型的 n 直接计算也可以。
1 2 3 4 5 6 7 int n;cin>>n; while (n){ 在循环内部直接进行n%10 的运算。同样是乘权值再保存到数组里 }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 ``` ```cpp #include <bits/stdc++.h> using namespace std; #define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); void solve() { string n; cin>>n; vector<int> a(n.size()+1); //开一个大小为字符串长度相同的数组,当然也可以不用,每次push_back()就行。 int ans=0; for(int i=n.size()-1;i>=0;i--) { if(n[i]!='0') { a[i]=(n[i]-'0')*pow(10,n.size()-i-1); ans++; } } cout<<ans<<endl; for(int i=n.size()-1;i>=0;i--) { if(a[i])cout<<a[i]<<" "; } cout<<endl; } signed main(){ IOS int t; cin>>t; //int t=1; while(t--) { solve(); } }
B - Division? 链接: codeforces.com/problemset/problem/1669/A 这个题就是简单的 if else 语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); void solve () { int n; cin>>n; if (n<=1399 )cout<<"Division 4" ; else if (n>=1400 &&n<=1599 )cout<<"Division 3" ; else if (n>=1600 &&n<=1899 )cout<<"Division 2" ; else cout<<"Division 1" ; cout<<endl; } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
C - Triple 链接: Problem - 1669B - Codeforces 这个题也比较简单,统计每个数出现的次数,对于第一个出现三次的直接输出,然后把之后出现的数记录下来就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); const int N=2e5 +5 ;void solve () { bool flag=true ; int n; cin>>n; int a[N]; memset (a,0 ,sizeof (a)); for (int i=0 ;i<n;i++) { int m; cin>>m; a[m]++; if (a[m]>=3 &&flag){ cout<<m<<endl; flag=false ; } } if (flag)cout<<"-1" <<endl; } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
D - Odd/Even Increments 链接:Problem - 1669C - Codeforces 这个题的意思就是要使最终数组结果都为偶数或者奇数,每次操作是给相同奇偶性下标的位置进行加一。如果可以完成操作,那么相同奇偶性下标的数的奇偶性也应该相同。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); const int N=2e5 +5 ;void solve () { int n; cin>>n; int a[N]; bool flag=true ; for (int i=1 ;i<=n;i++) { cin>>a[i]; } for (int i=1 ;i<=n-2 ;i++) { if ((a[i]%2 ==1 )&&(a[i+2 ]%2 ==0 )||(a[i]%2 ==0 )&&(a[i+2 ]%2 ==1 )) { cout<<"NO" <<endl; flag=false ; break ; } } if (flag)cout<<"YES" <<endl; } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
E - Colorful Stamp 链接 :Problem - 1669D - Codeforces 当字符串长度为 1 时不可能实现操作直接输出 no 就可以了。对于所有颜色都为 R 或者 B 也不可能实现(每次盖章都会出现两种颜色)。 那么我们来推理得到如果结果中存在 W 这个必然是没有被盖到的,那么我们可以以 W 为分界线。处理多段子串。如果这种字串长度为 1 或者所有颜色都一致无解。只要存在一个这样的子串则无解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); void solve () { int n; cin>>n; string s; cin>>s; bool flag=false ; int r=0 ,b=0 ; for (int i=0 ;i<s.size ();i++) { if (s[i]=='W' ) { if ((r==0 ||b==0 )&&flag){ cout<<"NO" <<endl; return ; }else {flag=false ,r=0 ,b=0 ;} }else { if (s[i]=='R' ) r++,flag=true ; if (s[i]=='B' ) b++,flag=true ; } } if ((r==0 ||b==0 )&&flag)cout<<"NO" <<endl; else cout<<"YES" <<endl; } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
F - Eating Candies 链接 :Problem - 1669F - Codeforces 这是一个双指针问题,一个从前往后扫,另一个从后往前扫,所实现的就是前扫的和等于后扫的和。要使所得到的糖果总数最多。 这里可以在前=后时记录下此时的糖果总数,然后对它取 max 就可以。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); const int N=2e5 +5 ;void solve () { int n; cin>>n; int w[N]; for (int i=0 ;i<n;i++)cin>>w[i]; int l=0 ,r=n-1 ; int sum1=0 ,sum2=0 ; int ans=0 ; while (l<=r) { sum1+=w[l]; while (l<r&&sum1>sum2)sum2+=w[r--]; if (sum1==sum2) ans=max (ans,l+n-r); l++; } cout<<ans<<endl; } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
G - Same Parity Summands 链接 :Problem - 1352B - Codeforces 要把 n 拆分成 k 个奇偶性相同的数。想一下一个奇数能不能拆分成偶数个?答案是不能。这里指的是拆分成奇偶性相同的数。这可以作为一个判断条件。 那一个偶数能不能拆分成奇数个?需要特殊判断一下,要拆分成偶数,最小的偶数是 2,所以 n 必须大于等于 2*k。 当 k>n 时也不可能实现,当 k =n 时可以实现 (输出 k 个 1)。 这样之后,对于一个奇数,拆分成奇数个,可以拆分成 k-1 个 1 和 n-k+1。(偶数个 1 加上一个奇数)。偶数+奇数=奇数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); void solve () { int n,k; cin>>n>>k; if (k>n){ cout<<"NO" <<endl; return ; } if (n%2 &&k%2 ==0 ) { cout<<"NO" <<endl; return ; } if (n==k) { cout<<"YES" <<endl; for (int i=1 ;i<=k;i++)cout<<"1 " ; cout<<endl; return ; } if (n%2 ) { cout<<"YES" <<endl; for (int i=1 ;i<=k-1 ;i++)cout<<"1 " ; cout<<n-k+1 <<endl; }else { if (k%2 &&n<2 *k) { cout<<"NO" <<endl; return ; } cout<<"YES" <<endl; if (n<2 *k) { for (int i=1 ;i<=k-1 ;i++)cout<<"1 " ; cout<<n-k+1 <<endl; }else { for (int i=1 ;i<=k-1 ;i++)cout<<"2 " ; cout<<n-2 *(k-1 )<<endl; } } } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
H - K-th Not Divisible by n 链接 :Problem - 1352C - Codeforces
1 2 3 4 5 6 7 8 来分析一下第一个示例 3 7 不能被三整除的数 1 2 4 5 7 8 10 11 13 第七个是10 再看4 12 1 2 3 5 6 7 9 10 11 13 14 15 17 18 19 我们可以发现被划分成每部分有(4 -1 )个元素
所以 (n-1)个数为一组,那么 k 中有几个这样的组呢? k/(n-1) 个,那么剩下的一个组有几个数呢?k%(n-1) 个。 所以最终结果就是 k/(n-1)*n+k%(n-1) 如果 k 能够整除 n-1 时,此时需要减一。(PS: 怎么思考呢,就是我们在乘 n 操作时是认为前面有 k/(n-1) 个完整的组,然后加上余数,但是如果没有余数此时的这个树是可以被 n 整除的,此时最后一个组其实是不完整的,并没有走到最后一个位置,所以减一)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); void solve () { int n,k; cin>>n>>k; int a=k/(n-1 ); int b=k%(n-1 ); if (b)cout<<a*n+b<<endl; else cout<<a*n+b-1 <<endl; } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
I - Alice, Bob and Candies 链接 :Problem - 1352D - Codeforces a从左往右枚举,b从右往左枚举。下一次吃的人要比上一次吃的人多。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <bits/stdc++.h> using namespace std;#define int long long #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); const int N=1e3 +5 ;void solve () { int n; cin>>n; int a[N]; for (int i=1 ;i<=n;i++)cin>>a[i]; int cnt=0 ,last=0 ,x=0 ,y=0 ; int l=0 ,r=n; while (l<r){ cnt++; int sum=0 ; if (cnt%2 ){ while (l<r&&sum<=last)sum+=a[++l]; x+=sum; }else { while (l<r&&sum<=last)sum+=a[r--]; y+=sum; } last=sum; } cout<<cnt<<" " <<x<<" " <<y<<endl; } signed main () { IOS int t; cin>>t; while (t--) { solve (); } }
欢迎关注我的微信公众号呀微信公众号文章链接