第8次CCF CSP认证真题解

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;
}

评测结果:

相关推荐
挺菜的5 分钟前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
mit6.8246 分钟前
7.6 优先队列| dijkstra | hash | rust
算法
2401_8582861139 分钟前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
钱彬 (Qian Bin)1 小时前
一文掌握Qt Quick数字图像处理项目开发(基于Qt 6.9 C++和QML,代码开源)
c++·开源·qml·qt quick·qt6.9·数字图像处理项目·美观界面
guygg881 小时前
基于matlab的FIR滤波器
开发语言·算法·matlab
双叶8362 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
源代码•宸2 小时前
C++高频知识点(二)
开发语言·c++·经验分享
ysh98882 小时前
PP-OCR:一款实用的超轻量级OCR系统
算法
遇雪长安2 小时前
差分定位技术:原理、分类与应用场景
算法·分类·数据挖掘·rtk·差分定位
数通Dinner2 小时前
RSTP 拓扑收敛机制
网络·网络协议·tcp/ip·算法·信息与通信