CSP练习题(基础部分C++)

跳一跳

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
	int a;
	int num = 0;//记录连跳次数
	int sum = 0;
	while (cin >> a && a != 0) {
		if (a == 1) {
			sum += 1;
			num = 0;
		}
		else if (a==2)
		{
			sum += 2*(++num);
		}
	}
	cout << sum;
	system("pause");
}

碰撞的小球

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
struct ball
{
	int station;
	int direction;
	int origin;

};
bool cmpweizhi(ball a, ball b) {
	return a.station < b.station;
}
bool cmpchushi(ball a, ball b) {
	return a.origin < b.origin;
}
int main()
{
	ball myball[100];
	int n,L, t;
	cin >> n >> L >> t;
	for (int i = 0; i < n; i++) {
		cin >> myball[i].station;
		myball[i].direction = 1;
		myball[i].origin = i;
	}
	//对初始的小球位置进行排序后小球才能动,否则计算的位置不正确(前后位置不对)
	sort(myball, myball+n, cmpweizhi);
	for (int i = 0; i < t; i++) {
		for (int j = 0; j < n; j++) {
			myball[j].station += myball[j].direction;
			//小球动完之后是否撞墙,撞墙方向要变
			if (myball[j].station == 0 || myball[j].station== L) {
				myball[j].direction = -myball[j].direction;
			}
		}
		//小球动完之后是否和球发生了碰撞,若发生碰撞方向要变
		for (int j = 1; j < n; j++) {
			if (myball[j].station == myball[j - 1].station) {
				myball[j].direction = -myball[j].direction;
				myball[j-1].direction = -myball[j-1].direction;
			}
		}
	}
	//题目要求按照输入顺序来进行输出
	sort(myball, myball + n, cmpchushi);
	for (int i = 0; i < n; i++) {
		cout << myball[i].station << " ";

	}
	system("pause");
} 

买菜

/*做题思路:先在纸上把例子画出来,找规律,发现有四种符合要求的情况
如果单独列出的话会比较麻烦,而且容易出错,我开始这么做的是部分正确,等号部分处理有问题
若利用以下规律会很简单
发现重合的时间段满足a<=d&&c<=b
然后时间间隔等于min(b,d)-max(a,c);
用暴力破解的方法,列举所有的可能,因为数据量小而且测试例子不一定按照对应的顺序输入
所有要遍历一遍来匹配相近的时间段
*/

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
struct Time {
	int begin;
	int end;
};
int main()
{
	Time H[100];
	Time W[100];
	int n;
	cin >> n;
	int sum = 0;
	int jianje = 0;
	for (int i = 0; i < n; i++) {
		cin >> H[i].begin;
		cin >> H[i].end;
	}
	for(int i=0;i<n;i++){
		cin >> W[i].begin;
		cin >> W[i].end;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (H[i].begin <= W[j].end&&H[i].end >= W[j].begin) {
				jianje = min(H[i].end, W[j].end) - max(H[i].begin, W[j].begin);
				sum += jianje;
			}

		}
	
	}
	cout << sum;
	system("pause");
} 

卖菜

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int *firstDay = new int[n];
	int *secondDay = new int[n];
	for (int i = 0; i < n; i++) {
		cin >> firstDay[i];
		secondDay[i] = 0;
	}
	secondDay[0] = (firstDay[0] + firstDay[1]) / 2;
	secondDay[n-1]= (firstDay[n-1] + firstDay[n-2]) / 2;
	for (int i = 1; i < n-1; i++) {
		secondDay[i] = (firstDay[i - 1] + firstDay[i] + firstDay[i + 1]) / 3;
	}
	for (int i = 0; i < n; i++) {
		cout << secondDay[i] << " ";
	}
	system("pause");
}

小中大

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int *array = new int[n];
	for (int i = 0; i < n; i++) {
		cin >> array[i];
	}
	int a, c;
	double b;
	a = max(array[0], array[n - 1]);
	c = min(array[0], array[n - 1]);
	if (n % 2 == 0) {
		b = (array[n / 2] + array[n / 2 - 1]) / 2;
	}
	else
	{
		b = array[n / 2];
	}
	cout << a << " ";
	if ((b - (int)b) > 0) {
		cout << setiosflags(ios::fixed) << setprecision(1) << b << " ";
	}
	else
	{
		cout << (int)b << " ";
	}

	cout << c;
	system("pause");
}

二十四点

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
int jieguo() {
	int jieguo = 0;
	char str[7];
	int a[7];
	for (int i = 0; i < 7; i++) {
		cin >> str[i];
		if (i % 2 == 0) {
			a[i] = (int)str[i] - 48;
		}
	}
	for (int i = 1; i < 7; i += 2) {
		if (str[i]=='x') {
			jieguo = a[i - 1] * a[i + 1];
			a[i - 1] = a[i + 1] = jieguo;
		}
		if(str[i]=='/')
		{   jieguo = a[i - 1] / a[i + 1];
			a[i - 1] = a[i + 1] = jieguo;
		}
	}
	if (str[1] == '+' && (str[3] == 'x' || str[3] == '/') && (str[5] == 'x' || str[5] == '/')) {
		jieguo = a[0] + a[6];
		//cout << jieguo<<"a";
		return jieguo;
	}
	else if (str[1] == '-' && (str[3] == 'x' || str[3] == '/') && (str[5] == 'x' || str[5] == '/')) {
		jieguo = a[0] - a[6];
		//cout << jieguo<<"b";
		return jieguo;
	}
	else {
		for (int i = 1; i < 7; i += 2) {
			if (str[i] == '+') {
				jieguo = a[i - 1] + a[i + 1];
				a[i - 1] = a[i + 1] = jieguo;
			}
			if (str[i] == '-')
			{
				jieguo = a[i - 1] - a[i + 1];
				a[i - 1] = a[i + 1] = jieguo;
			}
		}
	}
	return jieguo;
}
int main()
{
	int n;
	cin >> n;
	string *a = new string[n];
	int j = 0;
	for (int i = 0; i < n; i++) {
		j = jieguo();
		if (j == 24) {
			a[i] = "Yes";
		}
		else
		{
			a[i] = "No";
		}
	}
	for (int i = 0; i < n; i++) {
		cout << a[i] << endl;
	}
	
	system("pause");
}

小明种苹果

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
struct shu {
	int geshu;
	int bianhao;
};
bool cmp(shu a, shu b) {
	return a.geshu > b.geshu;
}
int main()
{
	int N, M;
	cin >> N >> M;
	shu shua[100];
	int a[100][100];
	int sum = 0;
	for (int i = 0; i < N; i++) {
		//赋值之前一定要先初始化
		//因为a[i][j]是待输入的不确定的所以直接相加是不对的
		shua[i].geshu = 0;
		for (int j = 0; j < M + 1; j++) {
			cin >> a[i][j];
			sum += a[i][j];
			if (j != 0) {
				
				shua[i].geshu += a[i][j];
			}
		}
		shua[i].bianhao = i;
		shua[i].geshu = -shua[i].geshu;
	}
	sort(shua, shua+N, cmp);
	cout << sum << " " << shua[0].bianhao+1 << " " <<shua[0].geshu ;
	system("pause");

	
}

小明种苹果(续)

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;


int main()
{
	int geshu=0;
	int n;
	cin >> n;
	int cishu;
	int a[100][100];
	bool *b = new bool[n + 2];
	int sum = 0;
	int t = 0;
	for (int i = 0; i < n; i++) {
		cin >> cishu;
		cin >> a[i][0];
		t = a[i][0];
		for (int j = 1; j < cishu; j++) {
			cin >> a[i][j];
			if (a[i][j] < 0)
				t += a[i][j];
			if (a[i][j] > 0) {
				if (t > a[i][j])
				{
					b[i + 1] = true;
					geshu++;
				}
				t = a[i][j];
			}
		}
		sum += t;
	}
	b[0] = b[n];
	b[n + 1] = b[1];
	int zushu = 0;
	if (n == 3) {
		if (b[1] == true && b[2] == true && b[3] == true) {
			zushu++;
		}
	}
	else {
		for (int i = 1; i <= n; i++) {
			if (b[i] == true && b[i + 1] == true && b[i - 1] == true) {
				zushu++;
			}

		}
	}
	cout << sum << " " << geshu << " " << zushu;
	system("pause");	
}

出现次数最多的数

#include <iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stdio.h>
#include<utility>
using namespace std;
int main()
{
	int b[1000] = { 0 };
	int n;
	cin >> n;
	int shu;
	for (int i = 0; i < n; i++) {
		cin >> shu;
		b[shu]++;
	}
	//输出次数最多且索引最小的
	int max = 0;
	int index = 0;
	for (int i = 0; i < 1000; i++) {
		if (b[i] > max&&b[i] != 0) {
			max = b[i];
			index = i;
		}

}
	cout << index;
	system("pause");

	
}
//四舍五入保留一位小数cout << setiosflags(ios::fixed) << setprecision(1) << b << " ";

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源来源于网络,如有侵权,私信立删。 CSP-J/S第一轮时间 CSP-J/S第一轮分为:CSP-J1和CSP-S1。和NOIP第一轮时间一样,CSP-J/S第一轮认证在10月份第三个星期六进行。 CSP-J/S第一轮考察重点 第一轮认证为笔试或机试,主要测试选手有关计算机方面的基本知识,第一轮为资格测试。从2020年开始,全部为机试。 CSP-J/S第一轮报名方式 参加CSP-S/J两组两轮认证均须在网上注册报名,注册网站为http://rg.noi.cn。参加认证者必须如实填写个人信息报名,包括但不限于姓名、身份证号、出生日期、性别、就学(学籍学校)/就职单位等,信息一旦注册,不得修改,如有错误,责任自负。 CSP-J/S第一轮认证考点 省认证点由CCF授权的CSP非专业级别省认证组织单位设置。 1、第一轮认证点:由省认证组织单位总负责人设置,每个认证点人数不应少于20人。 2、未经批准的认证点,其认证成绩不予承认。 认证费用 第一轮CSP-S/J组:50元/人(该费用不包括食宿及交通费)。 CSP-J/S第一轮认证试题组成 CSP-J/S第一轮在10月份第三个星期六进行,其中CSP-J1认证时间为周六9:30-11:30,CSP-S1认证时间为周六14:30-16:30。考试时长为2小时,内容为笔试或机试(从2020年开始全部为机试),满分100分。 试题由三部分组成: ① 选择题(共15题,每题2分,共计30分) CSP-S1的前10道题为单选题,后10道题为不定项选择题(只有全部选对才得分,否则不得分);CSP-J1的前15道题都是单选题。 ② 程序阅读理解题(共3题,共计40分) 题目给出一段程序(不一定有关于程序功能的说明),考生通过阅读理解该段程序进行答题,分为选择题和判断题。 ③ 程序完善题(共3题,共计30分) 题目给出一段关于程序功能的文字说明,然后给出一段程序代码,在代码中略去了若干个语句或语句的一部分并在这些位置给出空格,要求考生根据程序的功能说明和代码的上下文,选择对应答案 。三题皆为选择题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值