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

评测结果:

相关推荐
ZPC821028 分钟前
YOLO26 识别串番茄果梗
人工智能·深度学习·算法·机器学习
景熙55231 小时前
14.Java 集合框架从入门到源码万字详解(含 ArrayList/LinkedList/HashMap 源码、泛型通配符、红黑树)
java·开发语言·数据结构·算法
四代水门1 小时前
游戏客户端——算法题
c++
aaaameliaaa1 小时前
结构体 结构体
c语言·笔记·算法
Xia__Quan1 小时前
centos 共享文件夹
c++
hi_ro_a2 小时前
基于正倒排索引的boost搜索引擎
linux·c++·搜索引擎·项目
SendTomo2 小时前
.ico透明图标在线生成下载平台深度解析
大数据·数据结构·数据库·数据仓库·个人开发
有Li2 小时前
【AI答疑】MR数据预处理步骤
人工智能·笔记·算法·语言模型·医学生
神仙别闹2 小时前
基于 C++ MFC 实现的个人通讯录管理系统
c++
GG-_-Bond2 小时前
9.1kv存储持久化模拟面试
linux·c语言·数据结构·c++