第6次CCF CSP认证真题解
1、数位之和
题目链接:https://sim.csp.thusaac.com/contest/6/problem/0
100分代码:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cin >> n;
int num;
int t = 10,sum = 0;
while(n / t != 0){
num = n % t;
num = num / (t / 10);
sum += num;
t *= 10;
}
sum += n / (t / 10);
cout << sum << endl;
return 0;
}
评测结果:
2、消除类游戏
题目链接:https://sim.csp.thusaac.com/contest/6/problem/1
100分代码:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n,m;
cin >> n >> m;
int a[1010][30];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
}
}
int b[1010][30] = {0};
for(int i = 0; i < n; i++){
for(int j = 1; j < m-1; j++){
if(a[i][j-1] == a[i][j] && a[i][j] == a[i][j+1]){
b[i][j-1] = a[i][j-1];
b[i][j] = a[i][j];
b[i][j+1] = a[i][j+1];
}
}
}
for(int i = 1; i < n-1; i++){
for(int j = 0; j < m; j++){
if(a[i-1][j] == a[i][j] && a[i][j] == a[i+1][j]){
b[i-1][j] = a[i-1][j];
b[i][j] = a[i][j];
b[i+1][j] = a[i+1][j];
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i][j] == b[i][j]){
a[i][j] = 0;
}
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
评测结果: