第12次CCF CSP认证真题解
1、最小差值
题目链接:https://sim.csp.thusaac.com/contest/12/problem/0
100分代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cin >> n;
int a[1010];
for(int i = 0; i < n; i++){
cin >> a[i];
}
int ans = 10010;
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
ans = min(ans , abs(a[i] - a[j]));
}
}
cout << ans << endl;
return 0;
}
评测结果:
2、游戏
题目链接:https://sim.csp.thusaac.com/contest/12/problem/1
本题需要注意k == 1的边界条件,否则可能只有90分。
未考虑边界条件的90分代码:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n,k;
cin >> n >> k;
int num = n,count = 0;
int a[1010] = {0};
for(int i = 1; i <= n; i++){
a[i] = i;
}
while(num > 1){
for(int i = 1; i <= n; i++){
if(a[i] != 0)count++;
if((count % k == 0 || count % 10 == k) && a[i] != 0){
a[i] = 0;
num--;
}
}
}
for(int i = 1; i <= n; i++){
if(a[i] != 0)cout << a[i] << endl;
}
return 0;
}
评测结果:
100分代码:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n,k;
cin >> n >> k;
int num = n,count = 0;
int a[1010] = {0};
for(int i = 1; i <= n; i++){
a[i] = i;
}
while(num > 1){
for(int i = 1; i <= n; i++){
if(a[i] != 0)count++;
if((count % k == 0 || count % 10 == k) && a[i] != 0){
a[i] = 0;
num--;
}
if(num == 1)break; //边界条件
}
}
for(int i = 1; i <= n; i++){
if(a[i] != 0)cout << a[i] << endl;
}
return 0;
}
评测结果: