1、最大波动
题目链接:https://sim.csp.thusaac.com/contest/8/problem/0
100分代码:
cpp
#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 max_value = 0;
for(int i = 1; i < n; i++){
max_value = max(max_value , abs(a[i] - a[i-1]));
}
cout << max_value << endl;
return 0;
}
评测结果:
2、火车购票
题目链接:https://sim.csp.thusaac.com/contest/8/problem/1
本题需要注意一个边界条件:20排座位中每排都有被安排了,且剩下的人购买的车票无法安排在同一排,只能安排在编号最小的几个空座位中。如果边界条件考虑少了,可能就只有90分。
未考虑边界条件的90分代码:
cpp
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cin >> n;
int p[110];
for(int i = 1; i <= n; i++){
cin >> p[i];
}
int a[30][15] = {0};
for(int i = 1; i <= n; i++){
int count2 = 0;
for(int j = 1; j <= 20; j++){
int count1 = 0;
for(int k = 1; k <= 5; k++){
if(a[j][k] == 0)count1++;
}
if(count1 >= p[i]){
int count3 = 0;
for(int t = 1; t <= 5; t++){
if(a[j][t] == 0){
a[j][t] = (j-1)*5+t;
cout << a[j][t] << " ";
count3++;
}
if(count3 == p[i])break;
}
cout << endl;
break;
}
else if(count1 < p[i]){
count2++;
continue;
}
}
}
return 0;
}
评测结果:
100分代码:
cpp
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cin >> n;
int p[110];
for(int i = 1; i <= n; i++){
cin >> p[i];
}
int a[30][15] = {0};
for(int i = 1; i <= n; i++){
int count2 = 0;
for(int j = 1; j <= 20; j++){
int count1 = 0;
for(int k = 1; k <= 5; k++){
if(a[j][k] == 0)count1++;
}
if(count1 >= p[i]){
int count3 = 0;
for(int t = 1; t <= 5; t++){
if(a[j][t] == 0){
a[j][t] = (j-1)*5+t;
cout << a[j][t] << " ";
count3++;
}
if(count3 == p[i])break;
}
cout << endl;
break;
}
else if(count1 < p[i]){
count2++;
continue;
}
}
if(count2 == 20){ //边界条件
for(int x = 1; x <= 20; x++){
for(int y = 1; y <= 5; y++){
if(a[x][y] == 0){
a[x][y] = (x-1)*5+y;
cout << a[x][y] << " ";
}
}
}
}
}
return 0;
}
评测结果: