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

评测结果:

相关推荐
敲上瘾4 小时前
Qt 主窗口QMainWindow:菜单栏、工具栏、状态栏与浮动窗口等
开发语言·c++·qt·ui·qt6.3
BK贝壳9 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
人间乄惊鸿客10 小时前
c++ 右值
c++·算法
fanstuck11 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
田里的水稻12 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
ShineWinsu12 小时前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
码完就睡13 小时前
数据结构——图的基础概念
数据结构
瑶池酒剑仙13 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio