Problem A: 輸出連續(xù)的整數(shù)序列 之一
Description
輸出指定區(qū)間內(nèi)的所有整數(shù)滋尉。
Input
輸入只有1行,即N罢荡,N是一個(gè)int類型的數(shù)據(jù)。
Output
如果N>0涕烧,則輸出[1,N]區(qū)間內(nèi)的所有整數(shù);如果N =0汗洒,則輸出0议纯;如果N<0,則輸出[N,-1]內(nèi)的所有整數(shù)仲翎。
如果輸出的整數(shù)多于1個(gè)痹扇,則兩兩之間用一個(gè)空格隔開铛漓。
Sample Input
9
Sample Output
1 2 3 4 5 6 7 8 9
AC代碼
#include<stdio.h>
int main() {
int n;
int flag = 0;
scanf("%d", &n);
if (n == 0) {
printf("%d", 0);
} else if (n > 0) {
for (int i = 1; i <= n; i++) {
if (flag == 0) {
printf("%d", i);
flag = 1;
} else {
printf(" %d", i);
}
}
} else if (n < 0) {
for (int i = n; i <= -1; i++) {
if (flag == 0) {
printf("%d", i);
flag = 1;
} else {
printf(" %d", i);
}
}
}
return 0;
}
Problem B: 輸出連續(xù)的整數(shù)序列 之二
Description
輸出若干個(gè)連續(xù)的整數(shù)序列溯香。
Input
輸入有多行。第一行是N>0浓恶,表示后面有N行輸入玫坛。
之后每行輸入包含2個(gè)數(shù)據(jù)P和Q,兩者之間用空格隔開包晰,且均在int類型的表示范圍內(nèi)湿镀。
Output
輸出N個(gè)連續(xù)的整數(shù)序列,序列兩兩之間用一個(gè)空行隔開伐憾。
其中第i個(gè)序列是在P和Q之間的整數(shù)勉痴,包括P和Q,且當(dāng)輸出的數(shù)據(jù)多于1個(gè)時(shí)树肃,兩兩之間用一個(gè)空格隔開蒸矛。
Sample Input
3
1 100
10 -10
10 10
Sample Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10
10
HINT
當(dāng)P或Q是int類型的能夠表示的最大值或者最小值時(shí),應(yīng)該如何處理胸嘴?
AC代碼
#include <stdio.h>
int main() {
int z, x, i, N, temp, j;
scanf("%d", &N);
for(j = 0; j < N; ++j) {
scanf("%d%d",&z,&x);
if(z > x) {
temp=z;
z=x;
x=temp;
}
for(i=z; i<x; ++i) {
printf("%d ", i);
}
printf("%d\n", x);
printf("\n");
}
return 0;
}
Problem C: 是元音字母嗎雏掠?
Description
輸入一個(gè)英文字母,判斷是否是元音字母劣像。元音字母是:a,e,i,o u,A,E,I,O,U
Input
輸入一個(gè)英文字母
Output
是元音字母,輸出“yes”,否則輸出“no”,行尾沒有回車乡话。
Sample Input
A
Sample Output
yes
AC代碼(不要吐槽格式)
#include<stdio.h>
int main() {
char c;
scanf("%c", &c);
switch(c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf("yes");
break;
default:
printf("no");
break;
}
return 0;
}
Problem D: 序數(shù)的后綴
Description
英文中經(jīng)常用阿拉伯?dāng)?shù)字加上字母后綴表示“第幾“這樣的序數(shù)詞。比如耳奕,”第10次會(huì)面“通常寫成”10th meeting“绑青。
后綴來源于英文的序數(shù)詞:第1的英文是first,寫成”1st‘屋群;第2的英文是second时迫,寫成“2nd”;第3的英文是third谓晌,寫成“3rd”掠拳,第4是fourth,寫成“4th”纸肉,以后的數(shù)字都加“th”溺欧。
在這里規(guī)定喊熟,所有后綴為1的數(shù)字都寫成“st”結(jié)尾,后綴為2的數(shù)字寫成“nd”結(jié)尾姐刁,后綴為3的英文寫成“rd”結(jié)尾芥牌,其他的寫成“th”結(jié)尾。
Input
輸入為多個(gè)很小的正整數(shù)聂使,當(dāng)輸入為0時(shí)表示輸入結(jié)束壁拉。
Output
輸出為多行,每行對(duì)應(yīng)一個(gè)輸入數(shù)字的序數(shù)表示柏靶。
Sample Input
1 2 3 4 5 10 11 12 13 14 0
Sample Output
1st
2nd
3rd
4th
5th
10th
11st
12nd
13rd
14th
HINT
用switch語句似乎更容易些弃理。
AC代碼
然而并沒有用switch。
#include <stdio.h>
int main() {
int m, n;
while (1) {
scanf("%d", &n);
if (n == 0) {
break;
}
if (n <= 10) {
if (n == 1) {
printf("%dst\n", n);
} else if (n == 2) {
printf("%dnd\n", n);
} else if (n == 3) {
printf("%drd\n", n);
} else {
printf("%dth\n", n);
}
} else {
m = n % 10;
if (m == 1) {
printf("%dst\n", n);
} else if (m == 2) {
printf("%dnd\n", n);
} else if (m == 3) {
printf("%drd\n", n);
} else {
printf("%dth\n", n);
}
}
}
}
Problem E: 簡(jiǎn)單的數(shù)值統(tǒng)計(jì)
Description
現(xiàn)有一堆非零整數(shù)屎蜓,要求統(tǒng)計(jì)其中正數(shù)痘昌、負(fù)數(shù)的個(gè)數(shù)以及它們的平均值。
Input
輸入一系列整數(shù)炬转,僅有最后一個(gè)數(shù)字是0辆苔,表示輸入的結(jié)束。所有數(shù)據(jù)以及它們的和都在int的表示范圍之內(nèi)扼劈。
Output
輸出有2行驻啤。如果有負(fù)數(shù),第一行輸出負(fù)數(shù)的個(gè)數(shù)和平均值荐吵,否則第一行輸出0骑冗;如果有正數(shù),第二行輸出正數(shù)的個(gè)數(shù)以及平均值捍靠,否則第二行輸出0沐旨。每行輸出如果有2個(gè)數(shù),則用空格隔開榨婆。平均值只保留2位小數(shù)磁携。
Sample Input
1 2 3 4 -1 -2 -3 -4 0
Sample Output
4 -2.50
4 2.50
AC代碼
#include <stdio.h>
int main() {
int number;
int countPositive=0,countNegative=0;
int sumPositive=0,sumNegative=0;
float avgPositive,avgNegative;
scanf("%d",&number);
while(number != 0) {
if(number < 0) {
sumPositive += number;
countPositive++;
}
else {
sumNegative += number;
countNegative++;
}
scanf("%d",&number);
}
avgPositive = (sumPositive * 1.0) / countPositive;
avgNegative = (sumNegative*1.0) / countNegative;
if(countPositive != 0)
printf("%d %.2f\n", countPositive, avgPositive);
else
printf("%d\n", countPositive);
if(countNegative != 0)
printf("%d %.2f\n", countNegative, avgNegative);
else
printf("%d\n", countNegative);
return 0;
}
Problem F: 求累加和
Description
編程求min~max的累加和(含min和max),其中max>=min>0良风。
Input
輸入為多行谊迄。第一行是一個(gè)整數(shù)N>0,表示后面有N個(gè)測(cè)試用例烟央。后面有N行统诺,每行包含2個(gè)整數(shù),分別是min和max疑俭。
Output
輸出為N行粮呢,每個(gè)測(cè)試用例的計(jì)算結(jié)果占據(jù)一行。每行的格式為:
case i:sum=s.
其中i表示測(cè)試用例的編號(hào)(從1開始),s是該測(cè)試用例對(duì)應(yīng)的累加和(設(shè)不超過int的表示范圍)啄寡。
Sample Input
2
1 10
1 100
Sample Output
case 1:sum=55.
case 2:sum=5050.
AC代碼
#include<stdio.h>
int main() {
int N, i, j, sum, a, b;
scanf("%d", &N);
for(i = 0; i < N; ++i) {
scanf("%d%d", &a, &b);
sum = 0;
for(j = a; j <= b; ++j) {
sum += j;
}
printf("case %d:sum=%d.\n", i + 1, sum);
}
return 0;
}