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

评测结果:

相关推荐
qianpeng8971 小时前
水声匹配场定位原理及实验
算法
董董灿是个攻城狮13 小时前
AI视觉连载8:传统 CV 之边缘检测
算法
blasit19 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
AI软著研究员20 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish20 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱21 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
地平线开发者2 天前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮2 天前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者2 天前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考2 天前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习