csp算法题解

#include <iostream> 
#include <vector>
#include <algorithm>
using namespace std;

int  main() {


	int N = 0;
	cin >> N;
	vector<int> myvec;
	/*int *a = (int *)malloc(sizeof(int) * n);*/
	vector<int> result;
	char flag;
	int temp=0;
	while (cin >> temp)
	{
		myvec.push_back(temp);
		if (getchar() == '\n')
			break;
	}

	int value=0;
	
	for (int i = 0; i < myvec.size(); i++)
	{
		for (int j = i + 1; j < myvec.size(); j++)
		{
			value = myvec[i] - myvec[j];
			if (value < 0) {
				value = -value;
			
			}
			result.push_back(value);
			
		}
	
	}
	sort(result.begin(), result.end());
	
	cout <<result[0] << endl;

	system("pause");
	return 0;


}

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

int main() {
	map<int, int> a;
	int n = 0, k = 0;
	cin >> n >> k;
	vector<int> vec;
	int count = n;
	int num = 1;
	int temp = num;
	for (int i = 1; i <= n; i++)
		a[i] = 1;
	
	int c=1;
	while (count != 1) {
		temp = num;
		if (temp%k == 0) {
			count--;
			while (true)
			{
				if (a[c] != 0) {
					a[c] = 0;
					break;
				}
				c++;
				if (c > n) {
					c = 1;
				
				}
				

			}
			
			c++;
			if (c > n) {
				c = 1;

			}
			num++;
			continue;
		}


		while (temp > 10) {
			temp = temp % 10;
		
		}
		if (temp == k) {
			count--;

			while (true)
			{
				if (a[c] != 0) {
					a[c] = 0;
					break;
				}
				c++;
				if (c > n) {
					c = 1;

				}


			}


			c++;
			if (c > n) {
				c = 1;

			}
			num++;
			continue;
		}
		while (a[c] == 0) {
			c++;
			if (c > n) {
				c = 1;
			}
		}


		c++;
		if (c > n) {
			c = 1;
		}
		num++;

	
	
	}
	for (int j = 1; j <= a.size(); j++)
	{
		if (a[j] != 0) {
			cout << j << endl;
		}
	}

	

	
	return 0;
}
问题描述
  给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。
输入格式
  输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
  输入的第二行有n个整数s 1, s 2, …, s n (1 ≤ s i ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
输出格式
  输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
样例输入
6
10 1 10 20 30 20
样例输出
10
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
using namespace std;
int main()
{
	int n;
	cin >> n;
	map<int, int> f;
	f[0];//默认值为0
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		f[t]++;
	}
	int ans, m = 0;
	for (map<int, int>::iterator it = f.begin(); it != f.end(); it++)
	{
		if (it->second > m)//first:key;second:value
		{
			m = it->second;
			ans = it->first;
		}
	}
	cout << ans << endl;
	return 0;
}

问题描述
  每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔之后的五位数字代表该书在出版社的编号;最后一位为识别码。
  识别码的计算方法如下:
  首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,…,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。
  编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出是正确的ISBN号码。
输入格式
  输入只有一行,是一个字符序列,表示一本书的ISBN号码(保证输入符合ISBN号码的格式要求)。
输出格式
  输出一行,假如输入的ISBN号码的识别码正确,那么输出“Right”,否则,按照规定的格式,输出正确的ISBN号码(包括分隔符“-”)。
样例输入
0-670-82162-4
样例输出
Right
样例输入
0-670-82162-0
样例输出
0-670-82162-4


#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <map>
using namespace std;
int a[10];
int main()
{
	string s;//输入字符串
	cin >> s;
	a[0] = s[0] - '0';//字符串转换为整数,字符串减法为ASCLL码相减,之差为整型
	a[1] = s[2] - '0';
	a[2] = s[3] - '0';
	a[3] = s[4] - '0';
	a[4] = s[6] - '0';
	a[5] = s[7] - '0';
	a[6] = s[8] - '0';
	a[7] = s[9] - '0';
	a[8] = s[10] - '0';
	a[9] = s[12] - '0';
	int sum = 0;
	for (int i = 0, j = 1; i < 9; i++, j++)
	{
		sum += a[i] * j;
	}
	int code = sum % 11;
	char c = code == 10 ? 'X' : '0' + code;
	if (s[12] == c)
	{
		cout << "Right" << endl;
	}
	else
	{
		s[12] = c;
		cout << s << endl;
	}
	return 0;
}
问题描述
  小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别为a1a2, …, an。小明想分给每个朋友至少重量为k的蛋糕。小明的朋友们已经排好队准备领蛋糕,对于每个朋友,小明总是先将自己手中编号最小的蛋糕分给他,当这个朋友所分得蛋糕的重量不到k时,再继续将剩下的蛋糕中编号最小的给他,直到小明的蛋糕分完或者这个朋友分到的蛋糕的总重量大于等于k
  请问当小明的蛋糕分完时,总共有多少个朋友分到了蛋糕。
输入格式
  输入的第一行包含了两个整数nk,意义如上所述。
  第二行包含n个正整数,依次表示a1a2, …, an
输出格式
  输出一个整数,表示有多少个朋友分到了蛋糕。
样例输入
6 9
2 6 5 6 3 5
样例输出
3
样例说明
  第一个朋友分到了前3块蛋糕,第二个朋友分到了第4、5块蛋糕,第三个朋友分到了最后一块蛋糕。
评测用例规模与约定
  对于所有评测用例,1 ≤ n ≤ 1000,1 ≤ k ≤ 10000,1 ≤ ai ≤ 1000。

#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int main() {
	int m = 0, n = 0, temp=0,count=0,sum=0;
	vector<int> a;
	
	cin >> m >> n;

		for (int i = 1; i <= m; i++)
		{
			cin >> temp;
			a.push_back(temp);
		}
		for (int i = 0; i<m; i++)
		{
			sum += a[i];
			if (sum >= n) {
				count++;
				sum = 0;

			}
			else
			{
				if (i == m - 1) {
					count++;

				}

			}

		}
		
		cout << count << endl;

	

	


	system("pause");
	return 0;
}

学生排队

问题描述
  体育老师小明要将自己班上的学生按顺序排队。他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面,然后进行多次调整。一次调整小明可能让一位同学出队,向前或者向后移动一段距离后再插入队列。
  例如,下面给出了一组移动的例子,例子中学生的人数为8人。
  0)初始队列中学生的学号依次为1, 2, 3, 4, 5, 6, 7, 8;
  1)第一次调整,命令为“3号同学向后移动2”,表示3号同学出队,向后移动2名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 5, 3, 6, 7, 8;
  2)第二次调整,命令为“8号同学向前移动3”,表示8号同学出队,向前移动3名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 5, 8, 3, 6, 7;
  3)第三次调整,命令为“3号同学向前移动2”,表示3号同学出队,向前移动2名同学的距离,再插入到队列中,新队列中学生的学号依次为1, 2, 4, 3, 5, 8, 6, 7。
  小明记录了所有调整的过程,请问,最终从前向后所有学生的学号依次是多少?
  请特别注意,上述移动过程中所涉及的号码指的是学号,而不是在队伍中的位置。在向后移动时,移动的距离不超过对应同学后面的人数,如果向后移动的距离正好等于对应同学后面的人数则该同学会移动到队列的最后面。在向前移动时,移动的距离不超过对应同学前面的人数,如果向前移动的距离正好等于对应同学前面的人数则该同学会移动到队列的最前面。
输入格式
  输入的第一行包含一个整数 n,表示学生的数量,学生的学号由1到 n编号。
  第二行包含一个整数 m,表示调整的次数。
  接下来m行,每行两个整数p, q,如果q为正,表示学号为p的同学向后移动q,如果q为负,表示学号为p的同学向前移动-q。
输出格式
  输出一行,包含n个整数,相邻两个整数之间由一个空格分隔,表示最终从前向后所有学生的学号。
样例输入
8
3
3 2
8 -3
3 -2
样例输出
1 2 4 3 5 8 6 7
评测用例规模与约定
  对于所有评测用例,1 ≤  n ≤ 1000,1 ≤  m ≤ 1000,所有移动均合法。

#include <iostream>
#include <vector>
#include <map>
#include <stdio.h>
#include <algorithm>
using namespace std;


typedef pair<int, int> PAIR;


int temp = 0;
void swapnum(int &a,int &b) {
	temp = a;
	a = b;
	b = temp;


}


int main() {
	vector<int> num;
	vector<vector<int> > word;
	vector<int> word1;
	int first=0, second = 0;
	int n = 0, m = 0, flag = 0;;
	cin >> n>>m;
	for (int i=0; i <= n; i++)
		num.push_back(i);
	cin.ignore();
	while (flag<m)
	{
		cin >> first >> second;
		word1.push_back(first);
		word1.push_back(second);
		word.push_back(word1);
		flag++; word1.clear();
	}
	int k = 0;
	 
	for (int i=0; i < word.size(); i++) {
		k = 0;
		while (word[i][0] != num[k]) {
			k++;

		}
		
		if (word[i][1] > 0) {
			for (int j = 1; j <= word[i][1]; j++) {
				
				swapnum(num[k], num[k + 1]);
				k++;
			}
		

		}
		if (word[i][1] < 0) {
			for (int j = 1; j <= abs(word[i][1]); j++) {

				swapnum(num[k], num[k - 1]);
				k--;
			}


		}
		
	}

	//vector<PAIR> name_score_vec(num.begin(), num.end());
	//sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value);

	//map<int, int>::iterator ite = num.begin();
	for (int i = 1; i < num.size(); i++)
	{
		cout << num[i] << " ";
		
	}
	
	
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值