7-1 考試周 (5分)
考試周快到了,浙江大學的電子屏又調皮了…… 本題請你幫小編寫一個自動倒計時的程序,對給定的日期(例如“臘八”就對應 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分)
給定兩個絕對值不超過 109 的整數 A 和 B,計算它們的和 A+B 是非常簡單的事,但要將和這個數字按照中國古風從上到下豎着輸出,就不是那麼簡單了吧?
輸入格式:
輸入在一行中給出 2 個絕對值不超過 109 的整數 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;
}