CSP 历年第一二题汇总

早年大部分使用java写的,提交是需要将public class xx {}改为 public class main{},否则编译无法通过。

目前全部的前两题都已经解答完毕,出了202012月份的第二题只有70以外,全是满分通过。

202012-1

#include<algorithm>
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int n;
struct node{
	int w;
	int source;
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//安全指数 y=relu(和(score*w))i=1~n) relu(x)=max(0.x) 
int main(void) {
	cin>>n;
	struct node* nodes=(struct node* )malloc (n*sizeof(struct node));
	
	int i=0;
	int sum=0;
	for(i=0;i<n;i++){
		cin>>nodes[i].w>>nodes[i].source;
	}
	for(i=0;i<n;i++){
		sum +=(nodes[i].w*nodes[i].source);
	}
	cout<<max(0,sum);
	return 0;
}

202012-2

70分,超时

#include <iostream>
using namespace std;
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*y>=w,安全,否则,危险*/
 //predictθ(y)  表示根据阈值 θ 将安全指数 y 转化为的具体预测结果
 //函数值为0如果y< θ,否则为1 
 //result为真实值 
 //对m位同学进行预测 
 int m;
struct node{
	int y;
	int result;
};
 int predict(const int a,const int w){
 	if(a<w)
 		return 0;
 	else 
 		return 1;
 }
 int check(const int a,const int b){
 	if(a==b)
 		return 1;
 	else 
 		return 0;
 }
int main(int argc, char** argv) {
	cin>>m;
	struct node* nodes=(struct node*)malloc(m*sizeof(struct node));
	int i;
	for(i=0;i<m;i++)
		cin>>nodes[i].y>>nodes[i].result;
	int w,k,j=0;
	int real_w=0;
	int tmpsum=0;
	int sum=0;
	for(j=0;j<m;j++){
		tmpsum=0;
		w=nodes[j].y;	//w由y选取 
		for(k=0;k<m;k++){
		//求当前w对应的w*的值
		 tmpsum +=(check(predict(nodes[k].y,w),nodes[k].result));
		}
		if(tmpsum>sum){	//即便相等也要替换,为了保证取最大的w 
			sum=tmpsum;
			real_w=w;
		}
		if(tmpsum==sum)
			real_w=max(real_w,w);
	}
	cout<<real_w;
	return 0;
}

202009-1

#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
//n,X,Y表示检测点数和市民所在位置
int n, x, y;
int checkx[202];	//保存检测点的位置
int checky[202];
struct distant {
	int dis;	//距离
	int order;	//序号
}dist[202];
//输出距离该市民最近的三个点的编号,从1开始
bool cmp(const distant a, const distant b) {
	if (a.dis == b.dis)
		return a.order < b.order;		//!!!
	return a.dis < b.dis;
 }
int main(void) {
	cin >> n >> x >> y;
	int i;
	for (i = 0; i < n; i++)
		cin >> checkx[i] >> checky[i];	
	for (i = 0; i < n; i++) {
		dist[i].dis = pow(abs(x - checkx[i]), 2) + pow(abs(y - checky[i]), 2);//可以不开根
		dist[i].order = i + 1;
	}
	sort(dist, dist + n,cmp);
	for (i = 0; i < 2; i++)
		cout << dist[i].order << endl;
	cout << dist[2].order;
	return 0;
}

202009-2

/*
高危区域为一个矩形,含边界,左下角和右上角为(xl,yd)(xr,yu)
提供居民的活动坐标,若在区域内为经过,若有>=k个在内为逗留
给定高危区域的范围和n位居民过去t个时刻的位置记录,
试统计其中经过高危区域的人数和曾在高危区域逗留的人数。
n<=20,k<=t<=1000
*/
//若始终在危险区域内不动也记为逗留
#define _CRT_SECURE_NO_WARNINGS
#include<string>
#include<stdio.h>
int n, k, t;
struct coordinate {
	int x;
	int y;
};
int xl, yd, xr, yu;
int main(void) {
	scanf("%d%d%d%d%d%d%d",&n,&k,&t,&xl, &yd, &xr,&yu);
	//连续!!!>=k个坐标位于区间内为逗留
	//有一个在就是经过
	int count = 0, count_in = 0, count_danger = 0;
	int i,j,flag,flag1;
	for (i = 0; i < n; i++) {
		count = 0;
		flag = 0;
		flag1 = 1;
		struct coordinate pos[1002];
		for (j = 0; j < t; j++) {
			scanf("%d%d", &pos[j].x, &pos[j].y);
			if (pos[j].x >= xl && pos[j].y >= yd && pos[j].x <= xr && pos[j].y <= yu) {
				count++;
				flag = 1;
				if (count >= k && flag1) {
					count_danger++;	//高危
					flag1 = 0;
				}
			}
			else
				count = 0;
		}
		if (flag)
			count_in++;	//经过
	}
	printf("%d\n%d\n", count_in, count_danger);
}


202006-1

/*
9 3
1 1 A
1 0 A
1 -1 A
2 2 B
2 3 B
0 1 A
3 1 B
1 3 B
2 0 A
0 2 -3
-3 0 2 
-3 1 1
*/
/*if ((!(ttmp ^ tmp&0x80000000)&& (tmp_type == ttmp_type))||((ttmp ^ tmp & 0x80000000) && (tmp_type - ttmp_type)!=0))*/
//点分为A,B类
//找到一条直线a+bx+cy=0,由a,b,c三个参数确定且,b,c不同为0
//给定一条直线,判断是否可以将训练数据中的A,B两类点完美分开
#include<iostream>
using namespace std;
int n, m;	//表示点和查询的直线的个数
//输出共m行,第j行表示对应第j条的查询结果,若可以输出YES,否则输出NO
struct node {
	int x;
	int y;
	char type;
}nodes[1001];
struct line {
	long long int a;
	long long int b;
	long long int c;
}lines[21];

int main(void) {
	cin >> n >> m;
	int i, j,isok=1;
	for (i = 0; i < n; i++) {
		cin >> nodes[i].x >> nodes[i].y >> nodes[i].type;
	}
	for (i = 0; i < m; i++)
		cin >> lines[i].a >> lines[i].b >> lines[i].c;
	for (i = 0; i < m; i++) {
		long long int tmp = lines[i].a + lines[i].b * nodes[0].x + lines[i].c * nodes[0].y;
		char tmp_type = nodes[0].type;
		isok = 1;
		for (j = 1; j < n; j++) {
			long long int ttmp = lines[i].a + lines[i].b * nodes[j].x + lines[i].c * nodes[j].y;
			char ttmp_type = nodes[j].type;
			if (((tmp*ttmp>0)&& (tmp_type == ttmp_type))||((ttmp *tmp<0) && ((tmp_type - ttmp_type)!=0)))	//如果和nodes[0]位于同一侧且类型相or不是同一侧而且类型不同
				;
			else {
				isok = 0;
				break;
			}
		}
		if(isok)
				cout << "Yes" << endl;
		else
				cout << "No" << endl;
	}
}

202006-2

/*
用(index,value)表示稀疏向量中的值,index从1开始
对两个用稀疏表表示的两个向量u,v,求其内积
u*v=ui*vi(i=1~n)
*/
/*
10 3 4
4 5
7 -3
10 1
1 10
4 20
5 30
7 40
*/
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int n, a, b;	//维数,两个向量所含非零值的个数
struct inode {
	int index;
	int value;
};
int main(void) {
	cin >> n >> a >> b;
	struct inode* u = (struct inode*)malloc(sizeof(struct inode) * a);
	struct inode* v = (struct inode*)malloc(sizeof(struct inode) * b);
	int i,j=0;
	for (i = 0; i < a; i++)
		cin >> u[i].index >> u[i].value;
	for (i = 0; i < b; i++)
		cin >> v[i].index >> v[i].value;
	//int minnum = min(a, b)
	long long int sum = 0;
	if (v[b - 1].index < u[0].index || v[0].index>u[a - 1].index) {
		cout << sum << endl;
		return 0;
	}

	for (i = 0; i < a; i++) {
		for ( ; j < b; j++) {
			
			if (v[j].index < u[i].index)
				;
			else if (v[j].index == u[i].index)
				sum += (u[i].value * v[j].value);
			else
				break;
		}
	}
	cout << sum << endl;
	return 0;
}

201912-1

//四个人玩报数游戏(跳过7的倍数和含7的数),输出每个人最后在报出n个数游戏结束后跳过多少个数
#include<iostream>
using namespace std;
int n;	//n<=666,n是不包过跳过的数的数,因为不知道最后能报到多大的数,所以不采用暴力的方法
bool test(int i) {
	while (i) {
		if (i % 10 == 7)	//个位为7
			return true;
		else 
			i /= 10;	
	}
	return false;
}
int main() {
	cin >> n;
	int i, count = 0, a = 0, b = 0, c = 0, d = 0;	//计数器,abcd分比为甲乙丙丁四个人跳过的数量
	for (i = 1; count < n; i++) {					//!!注意这里是<不是小于等于,因为count是从零开始
		if (i % 7 == 0 || test(i)) {
			//如果i是7的倍数或者i的十位为7或者i的个位为7
			if ((i - 1) % 4 == 0)
				a++;
			else if ((i - 2) % 4 == 0)
				b++;
			else if ((i - 3) % 4 == 0)
				c++;
			else
				d++;
		}
		else
			count++;
	}
	cout << a << endl << b << endl << c << endl << d << endl;
}

201912-2

//创建回收站,要求该处有垃圾且上下左右四个位置有垃圾
//评分按照对角垃圾的数目
//输出每个分数的选址个数
/*
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1
*/
#include<iostream>
using namespace std;
int n;	//表示垃圾点个数,n<=10^3,
int pos[5];
struct coordinate {
	int x;
	int y;//坐标数值<=10^9
}node[1001];
bool exit_laji(int x, int y) {
	int i ;
	for (i = 0; i < n; i++) {
		if (node[i].x == x && node[i].y == y)
			return true;
	}
	return false;
}
bool is_huishouzhan(int x, int y) {
	if (exit_laji(x, y) && exit_laji(x - 1, y) && exit_laji(x + 1, y) && exit_laji(x, y - 1) && exit_laji(x, y + 1))
		return true;
	else
		return false;

}
int main() {
	cin >> n;
	int i,count=0;
	for (i = 0; i < n; i++) 
		cin >> node[i].x >> node[i].y;
	for (i = 0; i < n; i++) {
		int x = node[i].x, y = node[i].y;
		count = 0;
		if (is_huishouzhan(x, y)) {
			if (exit_laji(x - 1, y - 1))
				count++;
			if (exit_laji(x - 1, y + 1))
				count++;
			if (exit_laji(x + 1, y - 1))
				count++;
			if (exit_laji(x + 1, y + 1))
				count++;
				pos[count]++;
		}
		
	}
	for (i = 0; i < 5; i++)
		cout << pos[i] << endl;

}

201909-1

int N, M;//N,M分别代表苹果树的数目,疏果的次数
//每一行的第一个输入表示原始苹果树上的苹果数量,和每一轮疏果减少的苹果的数量(非正数)
//输出最后剩下的总的苹果数 被疏果最多的树的编号及其数量
/*
3 3
73 -8 -6 -4
76 -5 -10 -8
80 -6 -15 0
*/
int apple;
#include<iostream>
using namespace std;
#include<math.h>
int main() {
	cin >> N >> M;
	int i,j;
	int sum = 0,cut_num=0,cut_sum,max=0,max_order=1;
	for (i = 0; i < N; i++) {
		cin >> apple;
		cut_sum = 0;
		for (j = 0; j < M; j++) {
			cin >> cut_num;
			cut_sum += abs(cut_num);	//被疏果的的数量
		}
		if (cut_sum > max) {
			max = cut_sum;
			max_order = i + 1;
		}
		sum += (apple - cut_sum);
	}
	cout << sum << ' ' << max_order << ' ' << max << endl;
}

201909-2

/*
4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
*/
/*
5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0
*/
#include<math.h>
#include<iostream>
using namespace std;
int order[1001];
int main() {
	int n,m;
	cin >> n;
	int i,j,k=0;
	int initial_num,cut_num,cut_sum,left_sum=0;
	int d=0, e=0;
	int flag1 = 0;
	for (i = 0; i < n; i++) {
		cin >> m>> initial_num;
		cut_sum = 0;
		flag1 = 0;
		for (j = 0; j < m-1; j++) {
			cin >> cut_num;
			if (cut_num <= 0)
				cut_sum -= cut_num;
			else if (initial_num - cut_sum > cut_num) {	
				if (!flag1) {		//第一次发现掉落才算d++,因为一棵树可能多次掉落
					d++;
					flag1 = 1;
					order[k++] = i + 1;	//记录发生掉落的树的编号,这个编号应该是按从小到到大的顺序排列的
				}
				cut_sum = initial_num - cut_num;		//修正损失的苹果的数量
			}
		}
		left_sum += (initial_num - cut_sum);
	}          
	if (d >= 3) {		//不要n>3,如果写了&&n>3,只有90分,不写100分。猜测答案将(1,2,3)(2,3,1)(3,2,1)视为三种不同的情况
		if (order[0] == 1 && order[1] == 2 && order[d - 1] == n)
			e++;
		if (order[0] == 1 && order[d - 1] == n && order[d - 2] == n - 1)
			e++;
	}
	for (i = 0; i < d - 2; i++) {
		if (order[i + 1] == order[i] + 1 && order[i + 2] == order[i + 1] + 1)
			e++;
	}
	cout << left_sum << ' ' << d << ' ' << e;
}

201903-1

//输入有序序列,输出最大值,中位数,最小值(中位数可能为小数,四舍五入保留一位小数的结果)
//double ceil(double x):返回大于x的最小整数
//double flooe(double x):返回小于等于x的最小整数
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	int* num = (int*)malloc(sizeof(int) * n);
	int i,min,max;
	double middle;
	for (i = 0; i < n; i++)
		cin >> num[i];
	if (n % 2 == 0)
		middle = ((double)(num[n / 2 - 1] + num[n / 2])) / 2;
	else
		middle = num[n / 2];
	if (num[0] <= num[n - 1]) {
		//升序
		min = num[0];
		max = num[n - 1];
	}
	else {
		min = num[n-1];
		max = num[0];
	}
	printf("%d ", max);
	double tmp = middle;
	if (middle < ceil(tmp))	//middle是小数
		printf("%.1f ", middle);
	else
		printf("%.0f ", middle);
	printf("%d", min);
}

201903-2

//输入一个包含三个运算符,数字1-9的四则运算表达式判断解是否为24
//突然想起来数据结构上有这样的算法,使用栈实现,一个数字栈,一个运算符栈
#include<iostream>
using namespace std;
#include<stack>
#include<stdlib.h>
#include<stdio.h>
struct node {
	int a, b, c, d;
	char op1, op2, op3;
	bool flag = false;
}nodes[101];
char judge_op(char op1,char op2) {
	//定义运算符之间的优先级,op1和op2是相继出现的运算符,如果op1>op2,返回true
	//op1是栈顶元素,op2是当前输入的运算符
	if (op1 == '+' || op1 == '-') {

		if (op2 == '+' || op2 == '-')
			return '>';
		else
			return '<';
	}
	else
		return '>';
	
}
int operate(int a, int b, char op) {
	//定义操作函数执行操作
	switch (op) {
	case '+':
		return a + b;
	case '-':
		return a - b;
	case 'x':	//是小写字母x不是*!!
		return a * b;
	case '/':
		return a / b;
	}
}

int main() {
	int i, n;
	cin >> n;
	int ans1,ans2,ans;
	
	for (i = 0; i < n; i++) {
		cin >> nodes[i].a >> nodes[i].op1 >> nodes[i].b >> nodes[i].op2 >> nodes[i].c >> nodes[i].op3 >> nodes[i].d;
		//a op1 b op2 c op3 d
		int flag1 = 0;
		if (judge_op(nodes[i].op1, nodes[i].op2) == '>') {
			ans1 = operate(nodes[i].a, nodes[i].b, nodes[i].op1);
			nodes[i].op1 = '#';	//标志已经完成op1运算
		}
		else if (judge_op(nodes[i].op2, nodes[i].op3) == '>') {
			ans1 = operate(nodes[i].b, nodes[i].c, nodes[i].op2);
			nodes[i].op2 = '#';	//标志已经完成op2运算
		}
		else {
			ans1 = operate(nodes[i].c, nodes[i].d, nodes[i].op3);
			nodes[i].op3 = '#';	//标志已经完成op3运算
		}
		//9+3+4*3-->12+4*3
		if (nodes[i].op1 == '#') {
			//ans1 op2 c op3 d
			if (judge_op(nodes[i].op2, nodes[i].op3) == '>') {
				ans1 = operate(ans1, nodes[i].c, nodes[i].op2);			//ans1 op3 d
				nodes[i].op2 = '#';
			}
			else {
				ans2 = operate(nodes[i].c, nodes[i].d, nodes[i].op3);	//ans1 op2 ans2
				nodes[i].op3 = '#';
				flag1 = 1;		//区分ans2 op2 ans1
			}
		}
		else if (nodes[i].op2 == '#') { 
			//a op1 ans1 op3 d
			if (judge_op(nodes[i].op1, nodes[i].op3) == '>') {
				ans1 = operate(nodes[i].a, ans1, nodes[i].op1);			//ans1 op3 d
				nodes[i].op1 = '#';
			}
			else {
				ans2 = operate(ans1, nodes[i].d, nodes[i].op3);			//a op1 ans2
				nodes[i].op3 = '#';
			}
		}
		else if(nodes[i].op3 == '#') {
			//a op1 b op2 ans1
			if (judge_op(nodes[i].op1, nodes[i].op2) == '>') {
				ans2 = operate(nodes[i].a, nodes[i].b, nodes[i].op1);		//ans2 op2 ans1
				nodes[i].op1 = '#';
			}
			else {
				ans2 = operate(nodes[i].b, ans1, nodes[i].op2);			//a op1 ans2
				nodes[i].op2 = '#';
			}
		}
		if (nodes[i].op3!= '#')
			ans = operate(ans1, nodes[i].d, nodes[i].op3);
		else if (nodes[i].op1!= '#')
			ans = operate(nodes[i].a, ans2, nodes[i].op1);
		else
			ans = flag1 ? operate(ans1, ans2, nodes[i].op2):operate(ans2, ans1, nodes[i].op2);
		if (ans == 24)
			nodes[i].flag = true;
		//cout << ans << endl;
	}
	for (i = 0; i < n; i++) {
		if (nodes[i].flag)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
		
}

这里我本来是想采用数据结构书上使用的栈来写的,写出来后超时了,应该是因为反复入栈出栈导致的,功能上应该没什么问题。给出代码:

//输入一个包含三个运算符,数字1-9的四则运算表达式判断解是否为24
//突然想起来数据结构上有这样的算法,使用栈实现,一个数字栈,一个运算符栈
#include<iostream>
using namespace std;
#include<stack>
#include<stdlib.h>
#include<stdio.h>
bool a[101];
char judge_op(char op1,char op2) {
	//定义运算符之间的优先级,op1和op2是相继出现的运算符,如果op1>op2,返回true
	//op1是栈顶元素,op2是当前输入的运算符
	if (op1 == '+' || op1 == '-') {

		if (op2 == '+' || op2 == '-'||op2=='#')
			return '>';
		else
			return '<';
	}
	else if (op1 == '*' || op1 == '/')
		return '>';
	else {
		if (op2 == '#')
			return '=';
		else
			return '<';
	}
}
int operate(int a, int b, char op) {
	//定义操作函数执行操作
	switch (op) {
	case '+':
		return a + b;
	case '-':
		return a - b;
	case 'x':
		return a * b;
	case '/':
		return a / b;
	}
}

int main() {
	stack<int> s;
	stack<char> sc;
	int n;
	cin >> n;
	int i = 0, ans, j;
	for (j = 0; j < n; j++) {
		string str;		//字符串
		cin >> str;
		str += '#';		//添加结尾标志#
		i = 0;
		char c;
		sc.push('#');	//添加开始标志,优先级最低
		while (str[i] != '#' || sc.top() != '#') {
			if (str[i] >= '1' && str[i] <= '9') {	//是数字,直接压栈
				s.push(str[i] - '0');
				i++;
			}
			else {
				if ((c = judge_op(sc.top(), str[i])) == '>') {
					//如果当前运算符比栈顶运算符的优先级小,先执行栈顶运算符的操作
					int a = s.top(); s.pop();
					int b = s.top(); s.pop();
					char op = sc.top(); sc.pop();
					s.push(operate(b, a, op));		//b才是先压入栈的元素  b op a,将结果压栈
				}
				else if (c == '<') {
					//当前运算符的优先级比栈顶运算符的大,压栈
					sc.push(str[i++]);
				}
			}
		}
		if (s.top() == 24)
			a[j] = true;
		else
			a[j] = false;
		s.pop();
		sc.pop();	//下一次判别表达式前清空
	}
	for (j = 0; j < n; j++) {
		if (a[j])
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
}

201812-1

//红灯r秒,绿灯g秒,黄灯y秒,红绿黄依次循环显示,倒计时l(>0)秒为距离下一次的秒数
//输入中 0代表经过道路耗时t秒,1,2,3分别代表红黄绿灯,显示倒计时t

#include<iostream>
using namespace std;
int main() {
	int r, y, g, n,k;
	cin >> r >> y >> g>>n;
	int i,type,time;
	int t=0;
	for (i = 0; i < n; i++) {
		cin >>type >> time;
		if (type == 0 ||type==1)	//道路或红灯
			t += time;
		else if (type == 2) 
			t = t + time + r;		//如果是黄灯,之后还需要等待一个红灯
		
	}
	cout << t;
}

201812-2

这里采用的数学思想。利用循环的思想,对每一个道路的红绿灯计算经过时间t之后会处于什么状态(红/黄/绿)。
以当前道路灯出发时刻为绿灯为例,可以画出图:
在这里插入图片描述
前几次提交出现只有60分,仔细检查代码感觉没有什么错误,这时候注意数据规模,道路花费的时间t小于10^6, 而前6组数据n<=10^3, 所以总的时间不会超过int的规模(2.x*10^9),但总的n<=10的五次方,这就可能出现定义int型的时间t溢出。所以将时间t改为long long int之后成功AC。

//和第一题不同的是k=1,2,3时代表的时出发时刻此时的红绿灯状态
//输出放学所用时间
//n<=10^5
#include<iostream>
using namespace std;
struct node {
	int type=0;
	int time=0;
};
int r, g ,y;
struct node JudgeLight(long long int t, struct node a) {
	//判断出发时刻时a的灯,在经过t时间后会变成什么状态的灯,且距离下一个灯还剩下多少秒
	struct node tmp;
	int t1=t%(r+y+g);
	if (a.type == 1) {
		//red
		if (t1<a.time){
			tmp.type = 1;
			tmp.time = a.time - t1;
		}
		else if (t1 >= a.time + y + g) {
			tmp.type = 1;
			tmp.time = r + y + g - t1 + a.time;
		}
		else if (t1 >= a.time && t1 < a.time + g) {
				tmp.type = 3;
				tmp.time = a.time + g - t1;
			}
		else {
			tmp.type = 2;
			tmp.time = a.time + g + y - t1;
		}
	}
	else if (a.type == 3) {
		//green
		if (t1 < a.time) {
			tmp.type = 3;
			tmp.time = a.time - t1;
		}
		else if (t1 >= a.time + y + r) {
			tmp.type = 3;
			tmp.time = r + g + y - t1 + a.time;
		}
		else if (t1 >= a.time && t1 < a.time + y) {
			tmp.type = 2;
			tmp.time = a.time + y - t1;
		}
		else {
			tmp.type = 1;
			tmp.time = a.time + y + r - t1;
		}
	}
	else {
		//yellow
		if (t1 < a.time) {
			tmp.type = 2;
			tmp.time = a.time - t1;
		}
		else if (t1 >= a.time + r + g) {
			tmp.type = 2;
			tmp.time = r + g + y - t1 + a.time;
		}
		else if (t1 >= a.time &&t1<a.time+r) {
			tmp.type = 1;
			tmp.time = a.time + r - t1;
		}
		else {
			tmp.type = 3;
			tmp.time = a.time + r + g - t1;
		}
	}
	return tmp;
}

long long int HaveTime(int type, long long int t, int start_time) {
	//计算时间
	if (type == 1)	//红灯
		t += start_time;
	else if (type == 2)
		t = t + start_time + r;		//如果是黄灯,之后还需要等待一个红灯
	return t;
}
int main() {
	cin >> r >> y >> g;
	int n;
	cin >> n;
	struct node tmp,ttmp;
	int i = 0;
	long long int t = 0;
	for (i = 0; i < n; i++) {
		cin >> tmp.type >> tmp.time;
		if (tmp.type == 0)
			t += tmp.time;
		else {
			ttmp = JudgeLight(t, tmp);
			t = HaveTime(ttmp.type, t, ttmp.time);
		}
		
	}
	cout << t;
}

201809-1

//商店取自己和相邻商店的平均值作为标价
#include<iostream>
using namespace std;
int a[1001], b[1001];
int main() {
	int n;
	cin >> n;
	int i;
	for (i = 0; i < n; i++) 
		cin >> a[i];
	b[0] = (a[0] + a[1]) / 2;
	b[n - 1] = (a[n - 1] + a[n - 2]) / 2;
	for (i = 1; i < n-1; i++) {
		b[i] = (a[i - 1] + a[i] + a[i + 1]) / 3;
	}
	for (i = 0; i < n; i++)
		cout << b[i] << ' ';

}

201809-2

//小H和小W都有n个时间区间在装车,求他们的公共区间得到装车时的聊天时间‘’
//n<=2000,时间区间端点<=10^6
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
struct time {
	int s;
	int f;
}h[2001], w[2001];
int n;
int main() {
	cin >> n;
	int i,j;
	int t = 0;
	for (i = 0; i < n; i++)
		cin >> h[i].s >> h[i].f;
	for (i = 0; i < n; i++)
		cin >> w[i].s >> w[i].f;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			if (w[j].f <= h[i].s)
				;
			else if (w[j].s < h[i].s && w[j].f >= h[i].s)	//w的j时间区间和h的i时间区间有交集
				t += (min(w[j].f, h[i].f) - h[i].s);
			else if (w[j].s < h[i].f && w[j].s >= h[i].s) //w的j时间区间和h的i时间区间有交集
				t += (min(w[j].f, h[i].f) - w[j].s);
			else
				break;		//w[j].s>=h[i].f
			
		}
	}
	cout << t;
}

201803-1

//0没有跳到方块上,结束游戏,1跳到方块上不在中心,得1分
//2跳到中心,如果上次得分为1或者这是本轮第一次跳跃则此次得分为2,否则此次得分比上次多两分
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main() {
	int cur,score=0,front=0;
	cin >> cur;
	while (cur != 0) {
		if (cur == 1) {
			front = 1;
			score += 1;
		}
		else if ((!front && cur == 2) || front == 1) {
			score += 2;
			front = 2;
		}
		else 
			score += (front += 2);
		cin >> cur;
	}
	cout << score;
}
//1 1 2 2 2 1 1 2 2 0

201803-2

//长度为L(偶数)的线段,左端点在原点,n个小球在偶数坐标处,速度为一个单位长度每秒,向右移动
//碰撞或碰到线段端点时会反向等速移动
//输出t秒后各个小球的位置
//不会有三个小球同时碰撞,小球到达端点和碰撞的时刻为整数,位置也是整数
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n, l, t;
struct node {
	int direction;	//0表示向左移动,1表示向右移动
	int postion;	//当前时刻所处的坐标
	int order;
}a[102];
int JudgePostion(int t,struct node (*tmp)[102]) {
	//判断所有小球经过t秒后的位置和方向,忽略可能发生的碰撞改变方向
	int i;
	for (i = 1; i <= n; i++) 
		(*tmp)[i].postion = ((*tmp)[i].direction) ? ((*tmp)[i].postion + t) : ((*tmp)[i].postion - t);
	return 1;
}

bool JudgePengZhuang(struct node a,struct node b) {
	//判断a和b两个小球是否会发生碰撞
	//设定端点0的方向向右,端点L的方向向左
	if (a.postion == b.postion)
		return true;	//同向不可能发生碰撞
	else
		return false;
}
bool cmp(struct node a, struct node b) {
	return a.postion < b.postion;
}
bool cmpb(struct node a, struct node b) {
	return a.order < b.order;
}
int main() {
	cin >> n >> l >> t;
	int i,j;
	a[0].direction = 1; a[0].postion = 0; a[0].order = 0;
	a[n + 1].direction = 0; a[n + 1].postion = l; a[n+1].order = n + 1;;
	for (i = 1; i <= n; i++) {
		cin >> a[i].postion;
		a[i].direction = 1;
		a[i].order=i;
	}
	sort(a, a + n + 1, cmp);		//将输入的球的坐标按从小到大排列

	for (i = 0; i <t; i++) {
		JudgePostion(1, &a);
		for (j = 1; j <= n ; j++) {
			if (a[j].direction==1&&JudgePengZhuang(a[j], a[j + 1])){	//a[j]向右发生碰撞
				a[j].direction = 0;
				a[j + 1].direction = (j == n) ? 0 : 1;		//端点方向不变
			}
			else if (a[j].direction==0&&JudgePengZhuang(a[j], a[j - 1])) {	//a[j]向左发生碰撞
				a[j].direction = 1;
				a[j - 1].direction = (j == 1) ? 1 : 0;
			}
		}
	}
	sort(a, a + n + 1, cmpb);	//必须恢复原来输入的顺序,不然输出的顺序是按照最终的位置从小到大排列的
	for (i = 1; i <= n; i++)
		cout << a[i].postion << ' ';
}

201712-1

使用库函数sort排序,和min_element()寻找一维数组的最小值。与之相对应的还有max_element函数寻找最大值。
这写函数默认都是从小到大排序。
如果自己定义一个cmp函数为从大到小,调用max_element和min_element会得到相反的结果。返回的是指针。需要*使用。

//给定n个数,请找出其中相差(差的绝对值)最小的两个数,输出它们的差值的绝对值
#include<iostream>
#include<algorithm>
using namespace std;
int a[1001],b[1000];
int main() {
	int n;
	cin >> n;
	int i;
	for (i = 0; i < n; i++) 
		cin >> a[i];
	sort(a, a + n);
	for (i = 0; i < n - 1; i++) 
		b[i] = abs(a[i + 1] - a[i]);
	cout << *min_element(b, b + n - 1);
}

201712-2

//n个小朋友顺时针编号1-n
//报数,若报出的数是k的倍数或者末位为k,则该小朋友出局,当只剩下一个小朋友时,该小朋友胜出
//输出获胜小朋友编号
#include<iostream>
using namespace std;
int a[1001];		//初始化为0
int n, k;
bool WheatherOut(int i) {
	if (i % k == 0 || i % 10 == k)
		return true;
	else
		return false;
}
int main() {
	cin >> n >> k;
	int i = 1, j = 0, count = 0;
	while (count != n - 1) {
		while (a[j])
			j = (j + 1) % n ;	//跳过已经出场的同学
		if (WheatherOut(i)) {
			count++;
			a[j] = 1;
		}
		i++;
		j = (j + 1) %n ;
	}
	for (i = 1; i <= n; i++) {
		if (!a[i]) {
			cout << i+1 ;
			return 0;
		}
	}
}

201709-1

//有N元买酱油,每瓶酱油10元,每买三瓶送一瓶或者五瓶送两瓶,问最多几瓶
//实质上就是看N如何拆分,贪心策略应该是先尽可能多选买五送二,然后买三送一,最后单独买一
#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	int c5, c3, c1;
	c5 = n / 50;
	c3 = (n - c5 * 50) / 30;
	c1 = (n - c5 * 50 - c3 * 30) / 10;
	int sum = c5 * 7 + c3 * 4 + c1;
	cout << sum;
}

201709-2

这里最重要的就是sort函数的参数cmp函数,要能正确体现归还的顺序。
解题的思路其实根据题样例解释就可以得出来。

/*解题思路
将所有关键时刻从小到达排序,遍历排序后的时刻,如果是开始时间,对应钥匙标志为1表示被取走
如果是结束时间,对应钥匙标志为0表示归还
还需要一个数组记录钥匙的位置,如果是取走,对应编号的钥匙标志为-1,如果归还,在数组中寻找第一个出现-1位置,替换为钥匙的编号
*/
#include<algorithm>
#include<iostream>
using namespace std;
int n, k;	//n串钥匙,k个老师
int key[1001];		//存储的当前钥匙的位置,为-1表示被取走,为数字表示对应的钥匙编号
struct time {
	int t;	//时刻
	int flag;	//标志,为1表示开始时间,为0表示下课时间
	int key_order;	//这个时刻对应的钥匙编号
}times[2001];
bool cmp(struct time a, struct time b) {
	if (a.t == b.t) {
		if (a.flag == b.flag && a.flag == 0)
			return a.key_order < b.key_order;	//如果同一时刻有多位老师同时归还,按从小到大的顺序归还
		else
			return a.flag < b.flag;				//如果同一时刻既有还又有取,先还再取
	}
	else
		return a.t < b.t;
}
int main() {
	cin >> n >> k;
	int i,j,time;
	for (i = 0; i < 2*k; i +=2) {	
		//i=0,2,4,...2k-2记录开始时间,i=1,3,5,...2k-1记录下课时间
		cin >> times[i].key_order;
		times[i + 1].key_order = times[i].key_order;
		cin >> times[i].t;
		cin >> time;
		times[i + 1].t = times[i].t + time;
		times[i].flag = 1; times[i + 1].flag = 0;
	}
	for (i = 1; i <= n; i++)
		key[i] = i;
	//排序
	sort(times, times + 2 * k, cmp);
	//遍历每一个关键时刻
	for (i = 0; i < 2 * k; i++) {		
		if (times[i].flag == 1) {
			//如果是开始时间,钥匙被取走
			for (j = 1; j <= n; j++)
				if (key[j] == times[i].key_order)
					break;
			key[j] = -1;
		}
		else {
			//是结束时间,钥匙归还,从左到右寻找第一个标志为-1的
			for (j = 1; j <= n; j++) {
				if (key[j] == -1)
					break;
			}
			key[j] = times[i].key_order;
		}
	}
	for (j = 1; j <= n; j++)
		cout << key[j] << ' ';
}

201703-1

//分蛋糕,对每个朋友将手中最小的蛋糕给它直到超过k
//输出多少人分到了蛋糕
#include<iostream>
using namespace std;
int a[1001];
int main() {
	int n, k;
	cin >> n >> k;
	int i;
	int count = 0,sum=0;
	for (i = 0; i < n; i++) {
		cin >> a[i];
		sum += a[i];
		if (sum >= k) {
			count++;
			sum = 0;
		}
	}
	if (sum != 0)	//最后几块每满足>=k,但是也给了
		count++;
	cout << count;
}

201703-2

//首先按照学号从小到大排列,然后做出调整,负数为向后移动,正数为向前移动
//学号为p的同学移动q
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;	//学生数量1-n,和调整次数m
int pos[1001];	//位置
struct node {
	int id;
	int move;
}change[1001];
int main() {
	cin >> n>>m;
	int i,j,k;
	for (i = 1; i <= n; i++)
		pos[i] = i;
	for (i = 0; i < m; i++) 
		cin >> change[i].id >> change[i].move;
	for (i = 0; i < m; i++){
		if (change[i].move > 0) {
			//向后移动
			for (j = 1; j <= n; j++) {
				if (pos[j] == change[i].id)
					break;
			}
			int tmp = pos[j];
			for (k = 0; k < change[i].move; k++,j++)
				pos[j] = pos[j + 1];
			pos[j] = tmp;
		}
		else {
			//向前移动
			for (j = 1; j <= n; j++) {
				if (pos[j] == change[i].id)
					break;
			}
			int tmp = pos[j];
			for (k = 0; k < -change[i].move; k++, j--)
				pos[j] = pos[j - 1];
			pos[j] = tmp;
		}
	}
	for (i = 1; i <= n; i++)
		cout << pos[i] << ' ';
}

201612-1


import java.util.Arrays;
import java.util.Scanner;

//2016-12-1
public class TheMiddleNum {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int num=in.nextInt();
		int[] nums=new int[num];
		for(int i=0;i<num;i++)
			nums[i]=in.nextInt();
		Arrays.sort(nums);
		int LG=0;
		int LT=0;
		int j=(num%2==0)?num/2-1:num/2;//确定好中间数位于中间后,一定要分奇数和偶数
		if(num==1)
		{
			System.out.println(nums[0]);
		}
		else {
		for(int i=j+1;i<num;i++)//包括一些数值也要以J为标准
		{
			if(nums[i]>nums[j])
				LG++;
		}
		for(int i=0;i<j;i++)
		{
			if(nums[i]<nums[j])
				LT++;
		}
		if(LT==LG)
			System.out.println(nums[j]);
		else
			System.out.println(-1);
	}}
}

201612-2

import java.util.Scanner;

//2016-12-2
public class SalaryCount {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		int a;
		int s=0;
		if(t>3500)
		{
			a=t-3500;	//由税收前的工资的范围推算出税后工资的范围
			if(t<=4955)
				s=(t-105)*100/97;
			else if(t<=7655)
				s=(t-455)*100/90;
			else if(t<=11255)
				s=(t-1255)*100/80;
			else if(t<=30755)
				s=(t-1880)*100/75;
			else if(t<=44755)
				s=(t-3805)*100/70;
			else if(t<=61005)
				s=(t-6730)*100/65;
			else 
				s=(t-15080)*100/55;
		}
		else
			s=t;
		System.out.println(s);
		
	}
}

201609-1

import java.util.Scanner;

public class Thebiggestdifferencr {
//2016-9-1
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int i=0;
		int[] nums=new int[n];
		for(i=0;i<n;i++)
			nums[i]=in.nextInt();
		int max=0;
		for(i=0;i<n-1;i++)
		{
			if(Math.abs(nums[i+1]-nums[i])>max)
				max=Math.abs(nums[i+1]-nums[i]);
		}
		System.out.println(max);
	}
}


201609-2

import java.util.Scanner;
//2016-9-2
public class BuyTrainTickets2 {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int[] nums=new int[n];
		int i;
		for(i=0;i<n;i++)
			nums[i]=in.nextInt();
		int[][] seats=new int[20][5];
		int j;
		for(i=0;i<20;i++)
			for(j=0;j<5;j++)
				seats[i][j]=0;
		int count=0;int t=0;int k=0;
		int flag=0;
		for(i=0;i<n;i++)
		{
			flag=0;				//一定要在每一次判断某次买票时将flag置为0,否则后面的if语句等于白写
			for(j=0;j<20;j++)		//首先判断能不能相邻购买
			{
				for(k=0;k<5;k++)
				{
					count=0;
					while(k<5&&seats[j][k]==0&&count<nums[i])		//注意区分相邻和不相邻购买,相邻意味着每次换行都要将计数器置零
					{
						k++;
						count++;
					}
					if(count==nums[i])
					{
						for(t=j*5+k-count;t<j*5+k;t++)
						{
							seats[j][t-j*5]=1;
							System.out.print((t+1)+" ");
						}
						System.out.println();
						flag=1;					//如果买到相邻,置1
						break;
					}
					
				}
				if(count<nums[i])	;
				else	break;
			}
			if(flag==0)
			{
				count=0;
				for(j=0;j<20&&count<nums[i];j++)
				{
					for(k=0;k<5&&count<nums[i];k++)
					{
						if(seats[j][k]==0)
						{
							seats[j][k]=1;
							System.out.print((j*5+k+1)+" ");
							count++;
						}
					}
				}
				System.out.println();
				flag=0;
			}
		}
	}
}

201604-1

import java.util.Scanner;
//2016-4-1
public class InterflectionPoint {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int num = in.nextInt();
		int[] nums = new int[num];
		int count = 0;
		for (int i = 0; i < num; i++)
			nums[i] = in.nextInt();
		/*下面注释的方法不知道为什么只有40分*/
		/*int drop = 0;
		int up = 0;
		for (int i = 0; i < num - 1; i++) {
			if (up == 0) {
				if (nums[i + 1] < nums[i]) {
					drop = 1;
					continue;
				}
				if(drop==1) count++;
				drop = 0;
			}
			if (drop == 0) {
				if (nums[i + 1] > nums[i]) {
					up = 1;
					continue;
				}
				if(up==1)	count++;
				up = 0;
			}
		}*/
		if(num==1)	count=0;
		for(int i=1;i<num-1;i++)
		{
			if((nums[i]>nums[i-1]&&nums[i]>nums[i+1])||(nums[i]<nums[i-1]&&nums[i]<nums[i+1]))
				count++;
		}
		System.out.println(count);
	}
}

201604-2

这里对于下落的板块只需要存储为1的方块的行列信息,在下降的过程中,以下落的4*4方格图的最后一行为基准,可能经过初始的方格图的第一行到第18行(虽然整个初始板块只有15行,但是可能输入的下落板块1方格全在第一行,所以对最后一行而言会多出三行),同理,对于第一行会从初始方格图的上四行的位置开始直到第15行。所以需要对初始的方格版图进行扩充,由15行变为22行,顶部添加四行,设置为-1,底部添加三行设置为-2。

for循环的关键在于到达下边界和板块中某一个方块的下边缘与方格图上的方块上边缘重合。模拟降落的过程,以下落的板块图的最后一行为基准,外层循环为3-21代表最后一行经过的方格图的行,内层循环遍历下落的板块图(因为前面保存的是行列信息,所以只要循环四次),即每下落到某一行,遍历4个方格查看是否满足停止下落的要求。

关于到达方格图的下边界:如果输入的板块图的4个方格中位于最下方的方格在下降过程中到达某一行时其下一行的位置为-2(这里需要设置为-2是为了区分顶部的-1),则可判断为到达下边界。

关于板块中某一个方块的下边缘与方格图上的方块上边缘重合:只需要判断
输入的板块图的4个方格中任何一个方格存在在下降过程中到达某一行时其下一行的方格为1。

一旦满足停止条件,此时i记录了对应输入版图最后一行停在哪一行的信息,对此将四个方格填充即可。

而下降过程中的方格图a[b[j].x-3+i][b[j].y+line-1]就是输入板块中行为b[j].x,列为b[j].y的方格。

//下落的图案包含四个方块
//输入--给定初始板块和下落板块,和一个数字
//数字表示图案最左边开始的时候是再方格图的哪一列中
//当板块中的某一个方块的下边缘与方格图上的方块上边缘重合或者到达下边界时,板块不在移动
/*解题思路
首先注意方块是指的为1的方格
当板块每下落一行,遍历板块中的每一个方块,如果其下方位置的方块为1,停止下落
停止下落后,补充原始板块输入板块的图案
*/
#include<iostream>
#include<algorithm>
using namespace std;
struct pos {
	int x;
	int y;
}b[4];	//只记录为1的方块的行列信号
int a[22][10];
int main() {
	int i, j,k,n=0;
	int line;
	for (i = 4; i < 19; i++) {
		for (j = 0; j < 10; j++) {
			cin >> a[i][j];
		}
	}
	for (i = 0; i < 4; i++)
		for (j = 0; j < 10; j++)
			a[i][j] = -1;			//顶部预留4行-1
	for (i = 19; i < 22; i++)
		for (j = 0; j < 10; j++)
			a[i][j] = -2;			//底部预留3行-2
	n = 0;
	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			cin >> k;
			if (k == 1) {
				b[n].x = i;
				b[n++].y = j;
			}
		}
	}
	cin >> line;
	int flag = 0;
	for (i = 3 ; i < 22; i++) { 
		for (j = 0; j < 4; j++) {
			if (a[b[3].x-3+i+1][b[3].y+line-1]==-2|| a[b[j].x-3+i+1][b[j].y + line-1] == 1) {
				//如果到达下边界或者到达某个方块的上边缘(到达下方边界应该由版图中最下方的方块决定)
				flag = 1;
				break;
			}
		}
		if (flag)
			break;
	}
	if (flag) {						//填充板块图案
		for (k = 0; k < 4; k++)
			a[b[k].x-3+i][b[k].y+line-1] = 1;		//退出上面的循环后,i表示4行4列的板块最下边缘下落到的初始板块对应的行
	}
	for (i = 4; i < 19; i++) {
		for (j = 0; j < 10; j++)
			cout << a[i][j] << ' ';
		cout << "\n";
	}
}

201512-1

import java.util.Scanner;
//2015-12-1
public class Addfornum {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int num=in.nextInt();
		int ans=0;
		do
		{
			ans +=(num%10);
			num/=10;
		}while(num!=0);
		 System.out.println(ans);
	}
}

201512-2

import java.util.Scanner;

public class Gameforxiaochu {
//2015-12-2
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int m=in.nextInt();
		int[][] nums=new int[n][m];
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				nums[i][j]=in.nextInt();
		int count=0;
		Miss[] mis=new Miss[n*m];
		for(int i=0;i<m*n;i++)
		{
			mis[i]=new Miss();
			mis[i].x=mis[i].y=mis[i].flag=mis[i].count=0;
		}
		int t=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m-2;j++)
			{
				int elem=nums[i][j];	//设置监视哨,同时作为相同数字出现的第一个位置
				count=0;
				for(int k=j+1;k<m;k++)
				{
					if(nums[i][k]!=elem)
						break;
					else
						count++;
				}
				if(count>=2)	
				{
					mis[t].x=i;mis[t].y=j;
					mis[t].flag=1;
					mis[t].count=count;
					t++;
				}
			}
		}
		for(int j=0;j<m;j++)
		{
			for(int i=0;i<n-2;i++)
			{
				int elem=nums[i][j];	//设置监视哨,同时作为相同数字出现的第一个位置
				count=0;
				for(int k=i+1;k<n;k++)
				{
					if(nums[k][j]!=elem)
						break;
					else
						count++;
				}
				if(count>=2)	
				{
					mis[t].x=i;mis[t].y=j;
					mis[t].flag=2;
					mis[t].count=count;
					t++;
				}
			}
		}
		t=0;
		for(int i=0;i<m*n;i++)
		{
			if(mis[i].flag==0)
				continue;
			else if(mis[i].flag==1)
			{
				int x=mis[i].x;
				for(int j=mis[i].y;j<=mis[i].y+mis[i].count;j++)
					nums[x][j]=0;

			}
			else
			{
				int y=mis[i].y;
				for(int k=mis[i].x;k<=mis[i].x+mis[i].count;k++)
					nums[k][y]=0;
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
				System.out.print(nums[i][j]+" ");
			System.out.println();
		}
	}
}
class Miss
{
	int flag;		//标志是行是列
	int x,y;	//位置
	int count=0;
}

201509-1

import java.util.Scanner;

public class Arrayfenduan {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int count = in.nextInt();
		int[] nums = new int[count];
		for (int i = 0; i < count; i++)
			nums[i] = in.nextInt();
		int j = 0;
		int k;
		for (int i = 0; i < count; i = k) {

			for (k = i + 1; k < count; k++) {
				if (nums[k] == nums[i])
					;
				else {
					j++;
					break;
				}
			}

		}
		System.out.println(j + 1);
	}
}

201509-2

纯粹简单的数序题,注意粗心。

#include<iostream>
using namespace std;
struct node {
	int month;
	int day;
};
int JudgeRunYear(int year) {
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		return 1;
	else
		return 0;
}
int main() {
	int y, d;
	int month, day;
	cin >> y >> d;
	int isrun = JudgeRunYear(y);
	if (d <= 31) {
		month = 1; day = d;
	}
	else if (d <= 31+28 + isrun) {
		month = 2; day = d - 31;
	}
	else if (d <= 31* 2 + 28 + isrun) {
		month = 3; day = d - 31 - 28 - isrun;
	}
	else if (d <= 31 * 2 + +30+28 + isrun) {
		month = 4; day = d - 31 * 2 -28 - isrun;
	}
	else if (d <= 31 * 3 +30 +28 + isrun) {
		month = 5; day = d - 31 * 2 - 30 - 28 - isrun;
	}
	else if (d <= 31 * 3 + 30*2 + 28 + isrun) {
		month = 6; day = d - 31 * 3 - 30 - 28 - isrun;
	}
	else if (d <= 31 * 4 + 30 *2+ 28 + isrun) {
		month = 7; day = d - 31 * 3 - 30 * 2 - 28 - isrun;
	}
	else if (d <= 31 *5  + 30*2 + 28 + isrun) {
		month = 8; day = d - 31 * 4 - 30 * 2 - 28 - isrun;
	}
	else if (d <= 31 * 5 + 30*3 + 28 + isrun) {
		month = 9; day = d - 31 * 5 - 30 * 2 - 28 - isrun;
	}
	else if (d <= 31 * 6 + 30*3 + 28 + isrun) {
		month = 10; day = d - 31 * 5 - 30 * 3 - 28- isrun;
	}
	else if (d <= 31 * 6 + 30 * 4 + 28 + isrun) {
		month = 11; day = d - 31 * 6 - 30 * 3 - 28 - isrun;
	}
	else if (d <= 31 * 7 + 30 * 4 + 28 + isrun) {
		month = 12; day = d -31 * 6 - 30 * 4 - 28 - isrun;
	}
	cout << month << endl;
	cout<< day;
}

201503-1

本质上就是从上到下按行读变为从右到左按列读。

//对输入的矩阵逆时针旋转九十度输出
#include<iostream>
using namespace std;
int a[1000][1000];
int main() {
	int m, n;	//n行m列
	cin >> n >> m;
	int i,j;
	int tmp;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			cin >> a[i][j];
	for (j = m - 1; j >= 0; j--) {
		for (i = 0; i < n; i++)
			cout << a[i][j] << ' ';
		cout << "\n";
	}
		
}

201503-2

import java.util.Scanner;
//设置一个flag标志器,如果flag为0表示该数字为最先出现的数字或者只出现一次的数字
public class ArrayNum {
//2015-3-2
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		NUM[] nums=new NUM[n];
		for(int i=0;i<n;i++)
		{
			nums[i]=new NUM();
			nums[i].num=in.nextInt();
			nums[i].count=1;
			nums[i].flag=0;
		}
		for(int  i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				//如果存在相等,将后面的数字的flag标志为1表示已经数过且后面输出的时候应当忽略
				if(nums[j].num==nums[i].num&&nums[i].flag==0&&nums[j].flag==0)
					{
						nums[i].count++;
						nums[j].flag=1;
					}
			}
		}
		NUM tmp=new NUM();
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				//冒泡排序,对count进行排序
				if(nums[j].flag==0&&nums[i].flag==0)
				{
					if(nums[j].count>nums[i].count)
					{
						tmp=nums[i];
						nums[i]=nums[j];
						nums[j]=tmp;
					}
					else if(nums[j].count==nums[i].count)
					{
						if(nums[j].num<nums[i].num)
						{
							tmp=nums[i];
							nums[i]=nums[j];
							nums[j]=tmp;
						}
					}
				}
			}
		}
		for(int i=0;i<n;i++)
		{
			if(nums[i].flag==0)
				System.out.println(nums[i].num+" "+nums[i].count);
		}
	}
}
//选择采用类而不是创建一个新的数组在一定情况下可以节省内存
class NUM
{
	int num;
	int count;
	int flag;
}

201412-1

import java.util.Scanner;

public class menjin {
//2014-12-1
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int count=in.nextInt();
		int[] nums=new int[count];
		int[] temp=new int[count];
		for(int i=0;i<count;i++)
			temp[i]=1;
		for(int i=0;i<count;i++)
		{
			nums[i]=in.nextInt();
		}
		for(int i=0;i<count;i++)
		{
			int flag=0;
			if(temp[i]==1)
			{
				for(int j=i+1,k=2;j<count;j++)
				{
					if(nums[j]==nums[i])
					{
						temp[j]=k;
						flag=1;
						k++;
					}	
				}
			}
			
		}
		for(int num:temp)
			System.out.print(num+" ");
	}
}

201412-2

import java.util.Scanner;

public class Zsearch {
//2014-12-2
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int count=in.nextInt();
		int[][] nums=new int[count][count];
		for(int i=0;i<count;i++)
			for(int j=0;j<count;j++)
				nums[i][j]=in.nextInt();
		int[] temp=new int[count*count];
		int k=0;
		//对角线左侧部分
		for(int i=0;i<count;i++)
			for(int j=0;j<count-i;j++)
			{
				int tmp=((i+j+1)%2==1)?j+1:i+1;
				k=(i+j+1)*(i+j)/2+tmp-1;
				temp[k]=nums[i][j];
			}
		//对角线右侧部分
		for(int i=0;i<count;i++)
			for(int j=count-1;j>=count-i;j--)
			{
				
				int tmp=(2*count-i-j-2)%2==1?(count-i):(count-j);		//这里一定要搞清楚到底差几个
				int t=(2*count-i-j-1)*(2*count-i-j-2)/2+tmp;
				k=count*count-t;
				temp[k]=nums[i][j];
			}
		for(int i=0;i<count*count;i++)
			System.out.print(temp[i]+" ");
	}
}

201409-1

import java.util.Scanner;
//2014-09-1
//寻找数组中的相邻数对
public class theProximityNum {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int num=in.nextInt();
		int[] nums=new int[num];
		for(int i=0;i<num;i++)
			nums[i]=in.nextInt();
		int count = 0;
		for(int i=0;i<num;i++)
			for(int j=i+1;j<num;j++)
			{
				if(Math.abs(nums[i]-nums[j])==1)
					count++;
			}
		System.out.println(count);
	}
}

201409-2

import java.util.Scanner;
//2014-09-2
//求矩形的总面积
//采用总面积减去未涂色的格子的个数
public class drawPicture {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int num=in.nextInt();
		Pos[] pos=new Pos[num];
		for(int i=0;i<num;i++)
		{

			pos[i]=new Pos();
			pos[i].x1=in.nextInt();
			pos[i].y1=in.nextInt();
			pos[i].x2=in.nextInt();
			pos[i].y2=in.nextInt();
			pos[i].isdraw=0;
		}
		int xmin=pos[0].x1,xmax=pos[0].x2,ymin=pos[0].y1,ymax=pos[0].y2;
		for(int i=0;i<num;i++)
		{
			if(pos[i].x1<=xmin)
				xmin=pos[i].x1;
			if(pos[i].x2>=xmax)
				xmax=pos[i].x2;
			if(pos[i].y1<=ymin)
				ymin=pos[i].y1;
			if(pos[i].y2>=ymax)
				ymax=pos[i].y2;
		}
		int large=(xmax-xmin)*(ymax-ymin);
		int count=0;
		for(int i=xmin;i<xmax;i++)	//这里不能取等号,因为x2进行了+1
		{
			for(int j=ymin;j<ymax;j++)
			{
				Pos unit=new Pos();		//unit用来表示一个格子
				unit.x1=i;unit.x2=i+1;
				unit.y1=j;unit.y2=j+1;
				unit.isdraw=0;
				for(int k=0;k<num;k++)
				{
					if(unit.x1>=pos[k].x1&&unit.x2<=pos[k].x2&&unit.y1>=pos[k].y1&&unit.y2<=pos[k].y2)
						unit.isdraw=1;			//若落在涂色范围内标志该单元格已涂
				}
				if(unit.isdraw==0)			//如果遍历所有的矩形后都没有被涂色
					count++;
			}
		}
		System.out.println(large-count);

			
		
	}
}
class Pos
{
	int x1,y1;
	int x2,y2;
	int isdraw;
}

201409-3

import java.util.Scanner;
//2014-09-3
//indexOf 比较子串
//toLowerCase 转换小写
public class Stringmatch {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		String str=in.nextLine();
		int op=in.nextInt();
		int count=in.nextInt();
		String[] s=new String[count];
		for(int i=0;i<count;i++)
			s[i]=in.next();		//不要用nextline nextline不会清除字符串前面的还在输入流中的空格
		switch (op)
		{
		case 1:
			for(int i=0;i<count;i++)
			{
				if(s[i].indexOf(str)!=-1)
					System.out.println(s[i]);
			}
			break;
		case 0:
			String lowstr=str.toLowerCase();
			for(int i=0;i<count;i++)
			{
				if(s[i].toLowerCase().indexOf(lowstr)!=-1)
					System.out.println(s[i]);
			}
			break;
		}
			
	}
}

201403-1

import java.util.Scanner;

public class theOpositeNumber {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int count=in.nextInt();
		int[] nums=new int[count];
		int[] tmp=new int[1000];
		int i=0;
		for(i=0;i<count;i++)
		{
			nums[i]=in.nextInt();
			int index=Math.abs(nums[i]);
			tmp[index]++;
		}
		int j=0;
		for(i=0;i<1000;i++)
		{
			if(tmp[i]==2)
				j++;
		}
			System.out.print(j);
				
	}
}

201403-2

#include <stdio.h>
#include <stdlib.h>

 int main(void)
 {
    typedef struct Pos
    {
    int x1,y1;
	int x2,y2;
	int num;
	struct Pos* next;
    }Pos;
    typedef struct ClickPos
    {
     int x,y;
    }ClickPos;
     int N,M;
     scanf("%d",&N);
     scanf("%d",&M);
     Pos* head=(Pos*)malloc(sizeof(Pos));
     head->next=NULL;
     Pos* p;
     ClickPos click[M];
     for(int i=0;i<N;i++)
     {
         p=(Pos*)malloc(sizeof(Pos));
         scanf("%d",&p->x1);
         scanf("%d",&p->y1);
         scanf("%d",&p->x2);
         scanf("%d",&p->y2);
         p->num=i+1;
         p->next=head->next;
         head->next=p;
     }
     for(int i=0;i<M;i++)
     {
         scanf("%d",&click[i].x);
         scanf("%d",&click[i].y);
     }
     Pos* pre=head;
     p=head->next;
     for(int j=0;j<M;j++)
		{
			p=head->next;pre=head;
			while(p)
			{
				if(click[j].x>=p->x1&&click[j].x<=p->x2&&click[j].y>=p->y1&&click[j].y<=p->y2)
				{
					printf("%d\n",p->num);
					if(pre==head)
                        break;
                    else
                    {
                        pre->next=p->next;
                        p->next=head->next;
                        head->next=p;
                        break;
                    }

				}
				pre=p;p=p->next;
			}
			if(!p)
                printf("IGNORED\n");

		}
		return 0;
 }


201312-1

import java.util.Arrays;
import java.util.Scanner;
//this class aims to find the frequent num in the array,from csp 2013-12-1
public class maxoccur {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		int i=in.nextInt();
		int[] nums=new int[i];
		for(int j=0;j<i;j++)
			nums[j]=in.nextInt();
		Arrays.sort(nums);
		int maxnum=Arrays.stream(nums).max().getAsInt();
		maxnum=Math.max(maxnum, i)+1;
		int[] count=new int[maxnum];
		for(int j=0;j<i;j++)
		{
			int index=nums[j];
			count[index]++;
		}
				
			int max=0;
			int index = 0;
			for(int j=0;j<maxnum;j++)
			{
				if(max<count[j])
				{
					max=count[j];
					index=j;
				}
			}
			System.out.print(index);
			
		
		
	}
}


201312-2

import java.util.Scanner;
/*每一本正式出版的图书都有一个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号码。*/
public class ISBN {
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		String str=in.nextLine();
		int[] code=new int[10];
		int j=0;
		for(int i=0;i<13;i++)
		{
			char c=str.charAt(i);
			if(c!='-'&&c!='X')
				code[j++]=c-'0';
		}
		if(j==9)		//注意如果余数不是0,但是输入的是X,下标为9就为初始值0
			code[j]=-1;
		int sign=0;
		for(int i=0;i<9;i++)
			sign=sign+code[i]*(i+1);
		sign=sign%11;
		if(sign==code[9])
			System.out.print("Right");
		else
		{
			if(str.charAt(12)=='X'&&sign==10)		//注意这里还必须满足sign是10
				System.out.print("Right");
			else
			{
			for(int i=0;i<12;i++)
				System.out.print(str.charAt(i));
			if(sign!=10)
				System.out.print(sign);
			else
				System.out.print("X");
			}
		}
			
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值