题目链接
A:Sum of Round Numbers
题意:给出n个数,输出每个数有多少位是不为0的,再输出分解后的数
#include<bits/stdc++.h>
using namespace std;
int main(){
int t = 0;
int n = 0;
cin>>t;
for(int i = 0; i < t; i++){
scanf("%d", &n);
int num = 0;
int ans = 0;
string str = to_string(n);
int len = str.size();
for(int i = 0; i < len; i++){
if(str[i] != '0') num++;
}
printf("%d\n",num);
for(int i = 0; i < len; i++){
if(str[i] != '0'){
ans = (str[i] - '0') * pow(10,len - i - 1);
printf("%d ",ans);
}
}
printf("\n");
}
return 0;
}
B:Same Parity Summands
题意:给定t组数据,每组给一个n一个k,把n拆分成k个同奇或者同偶的数
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n, k;
cin >> n >> k;
int n1 = n - (k - 1);
if(n1 > 0 && n1 % 2 == 1){
cout<<"YES"<<endl;
for(int i = 0; i < k - 1; i++){
cout<<"1 ";
}
cout<<n1<<endl;
continue;
}
int n2 = n - 2*(k - 1);
if(n2 > 0 && n2 % 2 == 0){
cout<<"YES"<<endl;
for(int i = 0; i < k - 1; i++){
cout<<"2 ";
}
cout << n2 << endl;
continue;
}
cout << "NO" << endl;
}
return 0;
}
C: K-th Not Divisible by n
题意:给t组数据,每组给n和k,求第k个不能被n整除的数
一开始想着直接暴力循环…但是结果肯定超时
思路:找规律
n = 3
数: 1,2,4,5,7,8,10,11,13,14
下标: 1,2,3,4,5,6,7,8,9,10
数与下标相减
0,0,1,1,2,2,3,3,4
n=4
1 2 3 5 6 7 9 10 11 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 1 1 1 2 2 2 3 3 3...
可以看出n=3时以2为周期,n=4时以3为周期,以n-1为周期
将数与下标相减得到的值设为need,第k个数就是need+k
只需求出need
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
int need = (k - 1) / (n - 1);
cout << k + need << endl;
}
}
D: Alice, Bob and Candies
题意:给定长度为n的糖果序列,每个糖果有不同的值,Alice从左边吃,Bob从右边吃,Alice先开始吃,吃完到Bob,每一次吃的值要比上一次对方吃的值要大,吃完为止
思路:模拟
#include<bits/stdc++.h>
using namespace std;
int sweet[1005];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%d",&sweet[i]);
}
int a = 0, b = 0, l = 0, r = n- 1,last = 0,s1=0,s2=0;
int cnt = 0;
for(;l <= r;){
if(cnt & 1){//Bob
if(s2 <= last){
s2 += sweet[r--];
}
else{
last = s2;
b += s2;
s2 = 0;
cnt++;
}
}else{
if(s1 <= last){
s1 += sweet[l++];
}else{
last = s1;
a += s1;
s1 = 0;
cnt++;
}
}
}
//收尾
if(cnt & 1){
b += s2;
cnt++;
}else{
a += s1;
cnt++;
}
printf("%d %d %d\n",cnt,a,b);
}
return 0;
}
E: Special Elements
F: Binary String Reconstruction
G:Special Permutation