520 钻石争霸赛

7-1 考试周 (5分)

ksz.jpg

考试周快到了,浙江大学的电子屏又调皮了…… 本题请你帮小编写一个自动倒计时的程序,对给定的日期(例如“腊八”就对应 8)和倒计时天数(例如电子屏上的“四天之后”就对应 4),自动调整公式里的分母(例如 8/2=4 里面的那个 2)。

输入格式:

输入在一行中给出两个正整数:A 是给定的日期,不超过 30;B 是倒计时天数,不超过 10。

输出格式:

在一行中输出公式 A/X=B,其中 X 是满足等式的数字,输出时保留小数点后 1 位即可。

输入样例:

8 3

输出样例:

8/2.7=3

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	int A, B;
	cin>>A>>B;
	printf("%d/%.1f=%d",A,1.0*A/B,B);
	return 0;
}


 


7-2 真的恭喜你 (10分)

当别人告诉你自己考了 x 分的时候,你要回答说:“恭喜你考了 x 分!”比如小明告诉你他考了90分,你就用汉语拼音打出来 gong xi ni kao le 90 fen!

但是如果小明没考好,比如只考了 20 分,你也“恭喜”人家就不对了。这时候你应该安慰他说:“考了 20 分别泄气!”用汉语拼音写出来就是 kao le 20 fen bie xie qi!

输入格式:

输入在一行里给出一位小朋友的分数。这个分数是一个 0 到 100 之间的整数。

输出格式:

在一行中输出你对这位小朋友说的话。如果人家考到不低于 90 分,就说 gong xi ni kao le X fen!;如果不到 90 分,就说 kao le X fen bie xie qi!。其中 X 是小朋友输入的分数。

输入样例 1:

95

输出样例 1:

gong xi ni kao le 95 fen!

输入样例 2:

89

输出样例 2:

kao le 89 fen bie xie qi!

 

 AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	int s;
	cin>>s;
	if(s>=90){
		cout<<"gong xi ni kao le "<<s<<" fen!";
	} else {
		cout<<"kao le "<<s<<" fen bie xie qi!";
	}
	return 0;
}

 


7-3 平均成绩 (10分)

给定一所学校学生的体育成绩,请你统计全班的平均成绩和男生、女生的平均成绩。

输入格式:

输入首先在第一行中给出一个正整数 N(≤10000),即全校学生人数。最后 N 行,每行按照格式 性别 成绩 给出一位学生的信息。其中 性别 为 1 表示男生,0 表示女生;成绩 是一个 0 到 100 之间的整数。

输出格式:

在一行中依次输出全班的平均成绩、男生的平均成绩、女生的平均成绩。输出小数点后1位,数字间有一个空格分隔。

注意:如果全是男生或全是女生,则缺少的性别就没有办法计算平均分,相应的位置应该输出一个 X 表示没有。

输入样例 1:

5
1 97
0 91
0 98
1 95
1 90

输出样例 1:

94.2 94.0 94.5

输入样例 2:

5
0 97
0 91
0 98
0 95
0 90

输出样例 2:

94.2 X 94.2

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	int n, sum1=0, sum2=0, i=0, sex, s, n1=0, n2=0;
	cin>>n;
	while(++i<=n && cin>>sex>>s) {
		if(sex==1) {
			sum1+=s;
			n1++;
		} else {
			sum2+=s;
			n2++;
		}
	}
	
	//printf("%.1f %.1f %.1f",1.0*(sum1+sum2)/(n1+n2),1.0*sum1/n1,1.0*sum2/n2);
	if(n1==0) {
		printf("%.1f X %.1f",1.0*(sum1+sum2)/(n1+n2),1.0*sum2/n2);
	}else if(n2==0){
		printf("%.1f %.1f X",1.0*(sum1+sum2)/(n1+n2),1.0*sum1/n1);
	}else{
		printf("%.1f %.1f %.1f",1.0*(sum1+sum2)/(n1+n2),1.0*sum1/n1,1.0*sum2/n2);
	}
	
	return 0;
}

 


 

7-4 古风A+B (15分)

给定两个绝对值不超过 10​9​​ 的整数 A 和 B,计算它们的和 A+B 是非常简单的事,但要将和这个数字按照中国古风从上到下竖着输出,就不是那么简单了吧?

输入格式:

输入在一行中给出 2 个绝对值不超过 10​9​​ 的整数 A 和 B,其间以空格分隔。

输出格式:

将 A+B 数值的每一位从上到下竖着输出。如果是负数,那么负号占第 1 行,其它数字不用输出符号。

输入样例:

-6666 233

输出样例:

-
6
4
3
3

思路:一开始想用itoa函数进行直接将整数转化为相应的字符串,这样会快很多,但发现竟然在PTA上用不了==,只好自己再写了实现这个功能的代码。
 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

int main()
{
	int A,B,flag=0,len=0;
	char str[100];
	cin>>A>>B;
	int C = A+B;
	if (C<0) {
		flag=1;
		C=-C;
	}
	//itoa(C,str,10);
	do{
		str[len++]=C%10+48;
		C/=10;
	}while(C);
	//int len = strlen(str);
	if(flag){
		cout<<"-"<<endl;
	}
	for(int i=len-1;i>=0;i--)
	cout<<str[i]<<endl;
	return 0;
}

 


7-5 猜近似数字 (15分)

甲想好了一个 n 位数字让乙来猜,只要猜的数字位数正确,至多有 1 位与谜底不同,且不同的数字相差不超过 1,就算猜对了。例如谜底是 67,若乙猜 66、68、77、57,就都算对了;猜 167 就不能算对。

输入格式:

输入首先在第一行给出一个不超过 1000 位数的、最高位不是 0 的正整数,是甲给出的谜底。随后若干行,每行给出一个乙猜的数字,都是最高位不是 0 的正整数。直到出现 -1 表示输入结束,这个数字不要做任何处理。题目保证乙至少猜了一次。

输出格式:

对每一个乙猜的数字,如果猜对了就输出 Yes,否则输出 No

输入样例:

12345678909876
2345678909876
12345678900876
12345678809876
12345678909888
1234567890987
-1

输出样例:

No
No
Yes
No
No

AC代码 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	char str[10010];
	cin.getline(str,10010);
	int weishu=strlen(str);
	char temp[10010]={'\0'};
	while(cin>>temp && temp[0]!='-') {
		int flag=1;
		int len=strlen(temp);
		if(len!=weishu) {
			flag=0;
			cout<<"No"<<endl;
			continue;
		}
		
		int error=0;
		for(int i=0;i<len;i++)
		{
			if(temp[i]!=str[i]){
				if(abs(temp[i]-str[i])<=1) error++;
				else flag=0;
			}
			if(error>1) {
				flag=0;
				break;
			}
		}
		
		if(flag) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章