给定一个球体的直径(非负数),求它的表面积和体积。
Input
输入只有一个非负实数,表示球体的直径。
Output
输出有2行,格式见样例。其中等号(“=”)前后各有一个空格,输出的结果保留6位小数。
Sample Input
Sample Output
area =
volume =
HINT
在中,定义了一个常量M_PI,它就是圆周率。如果你的结果不对,试着包含这个头文件并使用常量M_PI代替你程序中的圆周率。
1
使用中M_PI的程序在OJ上无法编译通过,请自行定义M_PI为,或者用三角函数计算M_PI的值,如:4*atan,-2*asin(-1)。
#define M
#include <>
int main(){
double s, v, d;
scanf(\"%lf\
s =M*d*d;
v =M/3*d*d*d/2;
printf(\"area = %lf\\n\
printf(\"volume = %lf\\n\
return 0;
}
Description
2
如果一个三位十进制数等于其各位数字的立方和,则称这个数为水仙花数。如:13+53+33=153。
Input
一个整数x,100<=x<=999。
Output
x是水仙花数,则输出“YES”,否则为“Sample Input
153
Sample Output
YES
#include <>
int main() {
int a,b,c,d;
scanf(\"%d\
3
NO”。
b=a/100;
c=(a-b*100)/10;
d=a-b*100-c*10;
a==b*b*b+c*c*c+d*d*d
printf(\"YES\"):printf(\"NO\");
return 0;
}
Description
给定3个正整数a、b和c,按照如下规则求和:
如果这个数字是偶数,则累加到和中;
如果这个数字是奇数,则将大于该数的最小偶数累加到和中。
Input
三个正整数,均在100以内。
4
Output
一个和。
Sample Input
2 3 5
Sample Output
12
HINT
如果不会使用分支语句, 可使用条件运算符判断到底将哪个数累积到和中。
#include <>
int main() {
int x,y,z,h;
scanf(\"%d %d %d\
x= x%2==0 x:x+1;
5
y= y%2==0 y:y+1;
z= z%2==0 z:z+1;
h=x+y+z;
printf(\"%d\
}
Description
经过四年的学习,你决定报考我国著名的“285”高校之一的北青大学,经过认真的复习,残酷的考试,终于知晓了自己的考试成绩,也知道了北青大学的录取分数线,请你编程判断,自己过线了吗
Input
输入有2行,第一行有4个正整数,分别表示三门课程的分数线以及总分分数线。第二行有3个非负整数,分别表示你考的三门课程的成绩。
Output
如果你的三门课程成绩都不低于相应课程的分数线,且你的考试总分也不低于北青大学的总分分数线要求,则输出“congratulations”,否则输出“sorry”。
6
Sample Input
70 80 70 240
80 80 82
Sample Output
congratulations
HINT
如果你不会使用分支语句,同样可以使用条件运算符实现该程序。
#include<>
int main()
{
int a,b,c,d,e,f,g,s;
scanf(\"%d%d%d%d\\n\
scanf(\"%d%d%d\
7
s=e+f+g;
if (e>=a&&f>=b&&g>=c&&s>=d)
printf(\"congratulations\");
else
printf(\"sorry\");
}
Description
计算a+b,0<=a,b<1000。
Input
输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。
Output
每行输出一个a+b的值,顺序与输入对应。
Sample Input
8
1 2
10 20
Sample Output
3
30
HINT
OJ系统上测试输入结束符为EOF(End Of File),其值为-1。用scanf()把文件所有内容读完后,会读到EOF,所以可以用来判断输入是否完成,测试时可以用Ctrl+Z产生EOF。本题解法参看FAQ。
#include <>
int main ()
{
int a,b;
while (scanf(\"%d\\n%d\
9
printf (\"%d\\n\
}
Description
计算a+b,0<=a,b<1000。
Input
输入的第一行是一个整数N,后面有N对整数a和b,每对a和b占一行,a,b用空格分开。
Output
每行输出一个a+b的和,顺序与输入对应。
Sample Input
2
1 2
10 20
Sample Output
10
3
30
#include <>
int main ()
{
int a,b,c,i;
scanf (\"%d\
for(i=1;i<=c;i++)
while (scanf (\"%d\\n%d\
printf (\"%d\\n\
}
Description
找出n个数中最大的数和最小的数,并将它们的值输出出来。
11
Input
输入为n+1个整数,都在int类型范围内。这些数可能用若干空格或者换行符分隔开。
输入的第1个数为n,表示后续有n个数输入。从输入的第2个数开始,求出直到第n+1个数中最大的数和最小的数。
Output
输出为两行,格式见sample。
Sample Input
3 0 1 -1
Sample Output
The maximum number is 1.
The minimum number is -1.
HINT
分隔符是空格还是回车都是空白符,对scanf(\"%d\")来说没有区别;先读入n,然后用for循环就很容易控制读入n个数的过程。
12
#include <>
int main()
{
int i,n,m,max,min;
scanf (\"%d%d\
min=max;
for(i=1;i scanf (\"%d\ if (m>max) max=m; else if(m 13 } printf(\"The maximum number is %d.\\nThe minimum number is %d.\ } Description 需要判断给定的一个整数是否同时满足如下三个条件: 1. 它是一个完全平方数。 2. 它是一个偶数。 3. 它是一个正数。 注:若一个数能表示成某个自然数的平方的形式,则称这个数为完全平方数。例如: 0,1,4,9,16,25,36,49,,81,100,121,144,169,196,225,256,2,324,361,400,441,484,529 Input 一个int范围内的整数。 Output 14 如果输入的数字满足条件,则输出yes,否则输出no。 Sample Input 100 Sample Output yes HINT 注意逻辑表达式判断三个条件的顺序。 如果你不会使用分支语句,同样可以使用条件运算符实现该程序。 库函数sqrt()可以用于求一个数的平方根。 #include <> #include <> int main() { 15 int n,c; scanf (\"%d\ c=sqrt(n); if(n==c*c&&n%2==0&&n>0) printf(\"yes\"); else printf(\"no\");} Description 把百分制的考试成绩转换成五级制的成绩: 90~100:Excellent 80~:Good 70~79:Average 60~69:Pass 0~59:Failing 16 不在0~100之间的输入是非法数据,输出“Error”。 Input 输入多行,每行一个整数。 Output 输入所对应的成绩等级。 Sample Input -1 81 92 35 68 72 100 17 Sample Output Error Good Excellent Failing Pass Average Excellent HINT 用switch语句解决这个问题比较方便。 #include <> int main() { 18 int n; while (scanf (\"%d\ { if (n==100) printf (\"Excellent\\n\"); else if(n<0||n>100) printf (\"Error\\n\"); else switch (n/10) { case 9:printf (\"Excellent\\n\");break; case 8:printf (\"Good\\n\");break; case 7:printf (\"Average\\n\");break; 19 case 6:printf (\"Pass\\n\");break; default :printf (\"Failing\\n\"); } } } Description 输入一个英文字母,判断是否是元音字母。元音字母是:a,e,i,o u,A,E,I,O,U Input 输入一个英文字母 Output 是元音字母,输出“yes”,否则输出“no”,行尾没有回车。 Sample Input A 20 Sample Output yes #include<> void main() { char c; scanf(\"%c\ if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||c=='o'||c=='u') printf(\"yes\"); else printf(\"no\"); t} Description 对于输入的3个整数,按照从小到大的顺序输出。 Input 21 输入3个int类型内的整数,两两之间用一个空格隔开。 Output 按照从小到大的顺序输出上述三个数,两两之间用一个空格隔开。 Sample Input 2 1 3 Sample Output 1 2 3 #include<> void main() { int a,b,c,t; scanf(\"%d%d%d\ if(a>b) 22 {t=a; a=b; b=t;} if(a>c) {t=a; a=c; c=t;} if(b>c) {t=b; b=c; c=t;} printf(\"%d %d %d\\n\ Description 23 输入若干个整数,求其中正数、负数的个数。 Input 输入分为2行:第一行是一个数字N>0,表示下面有N个整数。第2行是N个整数,都是int类型的。 Output 输出所输入的N个整数的正数个数和负数个数,并用空格分开2个输出。 Sample Input 10 1 2 3 4 5 -1 -2 -3 -4 -5 Sample Output 5 5 #include<> int main() { 24 int a,i,c,d=0,e=0; scanf(\"%d\ for (i=1;i<=a;i++) {scanf(\"%d/n\ if(c>0) d=d+1; else if(c<0) e=e+1; else e=e; } printf(\"%d %d\ return 0; } Description 学校举行运动会,如果全体学生按照3人一队列队,则多了1个人;如果按照4人一队列队,则多了2个人;如果按照5人一队排队,则多了3个人。请问这个学校有多少学 25 生 Input 一个int类型的正整数N,是学生人数的上界,即:该校学生数不超过N。 Output 所有可能的学生数,每个数占一行。 Sample Input 200 Sample Output 58 118 178 #include<> int main() 26 { int a,i=-2; scanf(\"%d\ while (i<=a-60) {i+=60; printf(\"%d\\n\ } return 0; } Description 编写一个完整的程序,运行时向用户提问\"你考试考了多少分(0-100)\"接受输入后判断其等级并显示出来等级: 优:90<=分数<=100 良:80<=分数<90 27 中:60<=分数<80 差:0<=分数<60 Input 输入任意一个整数分数值,显示等级; 再输入任意一个整数分数值,显示等级; ....直到测试数据较充分,可输入-1止。 Output 对任意输入的分数值,输出对应的等级,直到输入的数为-1时才退出运行. Sample Input 102 100 90 80 70 60 28 50 0 -80 -1 Sample Output grad must between 0 and 100 优 优 良 中 中 差 差 29 grad must between 0 and 100 grad must between 0 and 100 #include <> int main() { int n; while (scanf (\"%d\ { if (n==100) printf (\"优\\n\"); else if(n<0||n>100) printf (\"grad must between 0 and 100\\n\"); else 30 switch (n/10) { case 9:printf (\"优\\n\");break; case 8:printf (\"良\\n\");break; case 7:printf (\"中\\n\");break; case 6:printf (\"中\\n\");break; default :printf (\"差\\n\"); } } } Description 计算a+b,0<=a,b<1000。 Input 31 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。当测试样为0 0时表示输入结束,0 0不参与运算。 Output 每行输出一个a+b的值,顺序与输入对应。 Sample Input 1 2 10 20 0 0 Sample Output 3 30 #include <> int main() { int a[1000],b[1000],n,i; 32 for(i=0;;i++) { scanf(\"%d %d\ if(a[i]==0&&b[i]==0) break; } for(n=0;nprintf(\"%d\\n\ return 0; } Description 计算a+b,0<=a,b<1000。 Input 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。 Output 每行输出一个a+b的值,顺序与输入对应。每个格式样例之间用一个空行分隔开。 Sample Input 33 1 2 10 20 15 35 Sample Output 3 30 50 #include <> int main() { int a,b; while(scanf(\"%d %d\ printf(\"%d\\n\\n\ Description 34 素数是只能被1和自身整除的正整数,根据数学定义1不是素数。素数也叫质数。 Input 输入为两个整数m和n,满足0<=m<=n<=100。 Output 从大到小输出m~n之间的所有素数,一个素数一行。如果m~n之间没有素数,则不输出任何数。 输出的所有数在两行“=====”之间。 Sample Input 2 12 Sample Output ===== 11 7 5 35 3 2 ===== #include<> #include<> int main() { int i,a,b,j,t; scanf(\"%d %d\ printf(\"=====\\n\"); for(i=b;i>=a;i--) { t=0; 36 for(j=2;j<=sqrt(i);j++) if(i%j==0) t=1; if(t==0&&i>1) printf(\"%d\\n\ } printf(\"=====\"); } Description 给出三个整数,代表三条边的长度,判断这三条边的长度是否能构成一个三角形 Input 第一行是n(1<=n<=100),表示有n组测试数据 接下来有n行,每一行包含三个整数,表示三个边长(1<=边长<=100) 37 Output 如果三条边能构成三角形,输出YES,否则输出NO Sample Input 3 1 2 3 2 8 7 20 20 1 Sample Output NO YES YES #include <> int main() 38 { int n,a[100],b[100],c[100],i; scanf(\"%d\ for(i=1;i<=n;i++) { scanf(\"%d %d %d\ if(a[i]+b[i]>c[i]&&a[i]+c[i]>b[i]&&b[i]+c[i]>a[i]) printf(\"YES\\n\"); else printf(\"NO\\n\"); } return 0; } Description 39 Tom和Jack是密码学爱好者,他们在聊天时经常使用一些暗语。他们使用的一种最简单的暗语是:将要说的每句话里面的英文字母变成这个字母之后的某个字母。现在要求你写一个程序,将一个字母变成它之后的某个字母。 Input 输入有2个:一个大写字母c和一个正整数d(0 输出字母c之后的第d个字母。大小写与c一致。如果c之后的某个字母已经超出'Z',则再从字母'A'开始计数。 如:c='A',d=3,则输出应为:D。 若:c='Y',d=3,则输出应为:B。 Sample Input A 3 Sample Output D 40 #include <> int main() { char a; int b; scanf (\"%c %d\ if (a>=65&&a<=90) { for (;a+b>90;) { b=b-26; continue; } 41 } if (a>=97&&a<=122) { for (;a+b>122;) { b=b-26; continue; } } printf (\"%c\ return 0; } Description 42 按字母顺序输出两个字母st和ed之间的所有字母,但不包括st和ed。不输出逆序。 Input 两个字母st和ed,都是大写字母,用一个空格分开。 Output 在一行内按顺序输出st和ed之间的所有字母,但不包括st和ed。 例如: 输入为A和E,则输出为BCD; 输入为A和B,则什么字母也不输出,只有一个空行; 输入为E和A,也是什么字母也不输出, 只有一个空行。 最后要输出一行(行尾不回车): ***END*** Sample Input A C 43 Sample Output B ***END*** #include <> int main() { char a,b,i; scanf (\"%c %c\ for (i=a+1;iprintf (\"%c\ printf (\"\\n***END***\"); return 0; } 44 Description Saya和Kudo一起去购物。假定她们逛的街是一条直线,而商铺是这条直线上的一些点。她们将车停在该直线最左端的店铺处,然后从左向右开始逛每一个店铺,然后从最右边的店铺再返回到停车处。你的任务是计算她们走了多少路。 Input 输入有多组。每一组的第一行是N(0 对每组输入,输出她们走的路长。 Sample Input 4 24 13 37 6 7 30 41 14 39 42 45 0 Sample Output 152 70 #include<> int main() { int N,x,i,max,min,sum; while(scanf(\"%d\ { for(i=1;i<=N;i++) { scanf(\"%d\ if(i==1) 46 max=min=x; if(x>max) max=x; if(x sum=(max-min)*2; printf(\"%d\\n\ } } Description 编程求min~max的累加和(含min和max),其中max>=min>0。部分程序已经给出,请填充其中的空白语句,并提交填充后的完整程序。 Input 47 输入为多行。第一行是一个整数N>0,表示后面有N个测试用例。后面有N行,每行包含2个整数,分别是min和max。 Output 输出为N行,每个测试用例的计算结果占据一行。每行的格式为: case i:sum=s. 其中i表示测试用例的编号(从1开始),s是该测试用例对应的累加和(设不超过int的表示范围)。 Sample Input 3 1 10 1 100 1 1 Sample Output case 1:sum=55. 48 case 2:sum=5050. case 3:sum=1. #include <> int main() { int i,j,N,min,max,sum; scanf(\"%d\ for(i=1;i<=N;i++) { scanf(\"%d%d\ sum=0; for(j=min;j<=max;j++) sum+=j; 49 printf(\"case %d:sum=%d.\\n\ } } Description 对给出的若干整数按从小到大排序。 Input 输入的第一个数为n(n<=1000),后接n个整数。 Output 按从小到大的顺序输出这些整数,每两个整数之间用一个空格分隔开,最后一个整数后面没有空格。 Sample Input 10 3 9 1 5 2 8 5 6 7 3 Sample Output 1 2 3 3 5 5 6 7 8 9 50 HINT 排序前必须把所有的整数都存储下来。因为只有最多1000个数,1秒的时间足够任何排序算法运行处结果来。 #include <> int main() { int i,j,n,t; int a[1000]; scanf(\"%d\ for(i=0;i for (i=1;i 51 {t=a[j]; a[j]=a[j+1]; a[j+1]=t;} for(i=0;i if(i==n-1) printf(\"%d\ else printf(\"%d \ } } Description “回文(Palindrome)”是指一个串和它的倒序串完全一样,例如“文言文”、“上海在海上”、“妈妈爱我,我爱妈妈”就是回文。 52 接下来,你要编写一个程序来判断一个串是不是\"回文\"。在这里,“回文”是指一个串中大小写不敏感的英文字母(a和A是同一个字母)和数字的顺序和逆序完全相同,其他字符(如空白符、标点和其他符号等)及所在位置被忽略。 Input 输入为多行,到文件末尾结束。每行为一个串,且不会超过1000个字符,且全部由可显示的ASCII码字符组成。 Output 当一个串中的字母和数字部分能够构成一个回文,即输出“Yes.”;否则输出“No.”。 Sample Input 123 2002 +0_0+ eye hello Revilo 53 Do you know \"No X in Nixon.\" Dollars make men covetous, then covetous men make dollars. A man, a plan, a canal: Panama! Sample Output No. Yes. Yes. Yes. No. Yes. No. Yes. No. Yes. #include<> #include<> #include<> int main() { char stra[1000]; int i,j,k,n,flag; while(gets(stra)!=0) flag=0; n=strlen(stra); 55 { for(j=0,i=0;j if(isalnum(stra[j])!=0) { stra[i]=stra[j]; if('a'<=stra[i]&&stra[i]<='z') {stra[i]=stra[i]-32; } i++; } } 56 stra[i]=0; for(k=0;k<=(i-1)/2;k++) {if(stra[k]!=stra[i-1-k]) { flag=1; printf(\"No.\\n\"); break; } } if(flag==0) printf(\"Yes.\\n\");} } #include <> 57 #include<> int main() { int i,j,x; char a[1001],b[1001]; while(gets(a)!=NULL) { for(i=0,j=0;a[i]!='\\0';i++) { if(isalnum(a[i])!=0) b[j++]=a[i]; } 58 for(i=0,x=0;i<=(j/2);i++) { if(tolower(b[i])==tolower(b[j-1-i])) x++; } if(x==(j/2)+1) printf(\"Yes.\\n\"); else printf(\"No.\\n\"); } } Description 求一个m×n阶矩阵A的转置矩阵AT。矩阵A的每个元素都在int类型的范围之内。 59 Input 输入的第一行为一个整数M(M>0),后面有M组输入数据。每组数据以两个正整数m和n开始,满足0 输出为多组,每组输出A的转置矩阵AT。矩阵的输出为:每行两个元素之间用一个空格分开,每行最后一个元素之后为一个换行,在下一行开始输出矩阵的下一行。每两组输出之间用一个空行分隔开。 Sample Input 1 3 3 1 2 3 4 5 6 7 8 9 Sample Output 1 4 7 60 2 5 8 3 6 9 #include<> int main() { int M,m,n,i,j,k; int a[100][100]; scanf(\"%d\ for(k=0;k for(i=0;i 61 for(i=0;i printf(\"%d\ printf(\"\\n\"); } printf(\"\\n\"); } } Description 根据给出的初始数、公差和序列长度求等差序列。 Input 输入为一行,格式见sample。其中,start为初始数,step为公差,times为序列长度。满足,times>0,step不为0。 Output 把这个等差序列输出在一行里,序列两数之间用一个空格分隔。 Sample Input 62 start = 1, step = 2, times = 100 Sample Output 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 91 93 95 97 99 101 103 105 107 109 #include<> void main() { int start,step,times,i,m; scanf(\"start = %d, step = %d, times = %d\ for(i=1;i<=times;i++) { m=start+(step*(i-1)); printf(\"%d \ 63 } } Description 根据给出的初始数、公差和终止条件求等差序列。 Input 输入为一行,格式见sample。其中,start为初始数,step为公差,end为终止条件。满足,step不为0,并且start和end的大小关系与step的方向一致。end不一定是序列的最后一个数。start、step和end均为int类型的范围内的整数。 Output 把这个等差序列输出在一行里,序列两数之间用一个空格分隔。 Sample Input start = 1, step = 2, end = 200 Sample Output 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 1 191 193 195 197 199 HINT 根据start和step的大小关系,判断序列终止的条件可能不同。 #include<> void main() { int start,step,end,i,m; scanf(\"start = %d, step = %d, end = %d\ for(i=1;;i++) { m=start+(step*(i-1)); if((m<=end&&step>0)||(m>=end&&step<0)) 65 printf(\"%d \ else break; } } Description 在C语言中,将ASCII字符集中的制表符('\')、回车符('\\r')、换行符('\\n')、垂直制表符('\\v')、换页符('\\f')和空格字符(' ')称作空白符。 你的任务是读入每行字符串,去掉行首和行尾的连续空白符,但是在任意非空白符中间的空白符不要去除。 Input 输入为多行,每行为一个串(不超过100个字符),至某行输入的非空白符仅为“END”结束。 Output 输出为多行,为每行输入的去掉前后空白符的串。“END”也输出。 Sample Input 66 abcdefg XYZ abc 123 END Sample Output abcdefg XYZ abc 123 END #include<> #include<> #include<> void main() 67 { char a[100]; int i,j,k,n,m; while(gets(a)!=NULL) { n=strlen(a); for(j=0;j if(isspace(a[j])==0) { break; } } for(k=n-1;k>=0;k--) 68 { if(isspace(a[k])==0) { break; } } for(m=j,i=0;m<=k;m++,i++) a[i]=a[m]; if(a[0]=='E'&&a[1]=='N'&&a[2]=='D') { printf(\"END\\n\"); break; } a[i]='\\0'; puts(a); } } Description 69 给出三角形的三边长度,判断是什么三角形。 Input 输入为多行,每行3个很小的非负整数,表示一个三角形的三条边的长度。当输入的三角形边长为0时表示输入结束。 Output 输出为多行,每行对应一个输入的三条边。 若输入的三条边构成等边三角形,输出“a equileteral triangle”; 若输入的三条边构成等腰三角形,输出“a isosceles triangle”; 若输入的三条边构成直角三角形,输出“a right triangle”; 若输入的三条边构成三角形,但不是以上三种,则输出“a triangle”;若输入的三条边不构成,输出“not a triangle”。 Sample Input 1 1 1 1 1 2 70 2 2 3 2 3 4 3 4 5 0 0 0 Sample Output a equileteral triangle not a triangle a isosceles triangle a triangle a right triangle #include <> void main(void) { 71 int a[1000],b[1000],c[1000],i; for(i=0;;i++) {scanf(\"%d%d%d\ if(a[i]==0&&b[i]==0&&c[i]==0) break; else if(a[i]+b[i]<=c[i] || a[i]+c[i]<=b[i] || b[i]+c[i]<=a[i]) printf(\"not a triangle\\n\"); else if(a[i]==b[i] && b[i]==c[i]) printf(\"a equileteral triangle\\n\"); else if(a[i]==b[i] || b[i]==c[i] || a[i]==c[i]) printf(\"a isosceles triangle\\n\"); else if(a[i]*a[i]+b[i]*b[i]==c[i]*c[i] || a[i]*a[i]==b[i]*b[i]+c[i]*c[i] || 72 a[i]*a[i]+c[i]*c[i]==b[i]*b[i]) printf(\"a right triangle\\n\"); else printf(\"a triangle\\n\"); }} Description 向标准输出上打印一些用ASCII字符组成的图形。 Input 输入为多个整数n,0 若n为偶数,则输出一个正向的n层等腰三角形;n为奇数,则输出一个倒向的n层等腰三角形。三角形由“+”组成。任意两个图形之间有一个空行分隔,格式见sample。 Sample Input 5 4 73 0 Sample Output +++++++++ +++++++ +++++ +++ + + +++ +++++ +++++++ #include <> int main() 74 { int i,j,n; while(scanf(\"%d\ { if(n%2==0) {for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) printf(\" \"); for(j=1;j<=2*i-1;j++) printf(\"+\"); printf(\"\\n\");} printf(\"\\n\"); 75 } else {for(i=1;i<=n;i++) { for(j=1;j<=i-1;j++) printf(\" \"); for(j=1;j<=2*n-2*i+1;j++) printf(\"+\"); printf(\"\\n\");} printf(\"\\n\"); } } } 76 Description 将输入的一个字符串s中的大写字母转换为小写字母,小写字母转换为大写字母,其他字符不变。 ----------------------------------------------------------------------------- 编写一个函数str_trans(): 原型:void str_trans(char s[]); 功能:把串s中的大写字母转换为小写字母,小写字母转换为大写字母,其他字符不变。 函数的调用格式见“Append Code”。 Input 输入为一个串s。输入最少为一个字符,最多不会超过100个字符。 Output 输出为转换后的串。 Sample Input 77 abc123ABC Sample Output ABC123abc #include <> void str_trans(char s[]) { int n,i; n=strlen(s); for(i=0;i if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]+32; else if(s[i]>='a'&&s[i]<='z') 78 s[i]=s[i]-32; else continue; } } #include <> int main() { char s[101]; gets(s); str_trans(s); puts(s); return 0; } Description 79 编写一个函数: int nearest(int n); 用于求不大于n的最大偶数,并作为函数的返回值进行返回。 Input 是一个int范围内的整数n。 Output 输出不大于n的最大偶数。 Sample Input 5 Sample Output 4 #include<> int nearest(int n) 80 { if(n%2==0) return n; else return n-1; } int main() { int m,n; scanf(\"%d\ n = nearest(m); printf(\"%d\ return 0; 81 } Description 对于给定的一个平面直角坐标系中的点p,输出p关于x轴、y轴和原点对称的点的坐标。 Input 输入2个实数,分别是点p的x坐标和y坐标。 Output 输出分为3行,分别是p关于x轴、y轴和原点对称的点的x坐标和y坐标。 Sample Input Sample Output #include <> int main() {float a,b; scanf(\"%f %f\ 82 printf(\"%.1f %.1f\\n\ printf(\"%.1f %.1f\\n\ printf(\"%.1f %.1f\\n\ } Description 输入一个正整数的年份,判断是否为闰年。 ----------------------------------------------------------------------------- 编写一个函数用于判断闰年。 用C语言实现:中函数原型为 int is_leap_year(int year); 功能:若参数year是闰年返回1,否则返回0。 用C++实现:中函数原型为 bool isLeapYear(int year); 83 功能:若参数year是闰年返回true,否则返回false。 函数的调用格式见“Append Code”。 Input 输入只有一行,为一个10000以内的正整数。 Output 输出为一行。 若输入为闰年偶数则输出“Yes”,否则输出“No”。 Sample Input 2010 Sample Output No HINT 参看系统首页上的“Append Code”使用说明,讨论版(Web Board)上也有 84 #include <> int is_leap_year(int year) { if(year%4==0&&year%100!=0||year%400==0) return 1; else return 0; } int main() { int year; scanf(\"%d\ 85 is_leap_year(year) printf(\"Yes\") : printf(\"No\"); return 0; } Description 计算a+b,0<=a,b<1000。 ----------------------------------------------------------------------------- 编写一个函数put_int_sum()。函数原型为 int put_int_sum(int a, int b); 功能:函数的参数传入a和b的值,计算a+b并输出,返回值为0时读取结束(在主函数中用返回值作为判断循环是否应该结束的条件)。 函数put_int_sum()的调用格式见“Append Code”。 Input 输入有多对整数a和b组成,每对a和b占一行,a,b用空格分开。当测试样为0 0 86 时表示输入结束,0 0不参与运算 Output 每行输出一个a+b的值,顺序与输入对应。 Sample Input 1 2 10 20 0 0 Sample Output 3 30 #include <> int put_int_sum(int a, int b) {int d; 87 if(a==0&&b==0) return 0; else d=a+b; printf(\"%d\\n\ int main() { int a, b; while(scanf(\"%d%d\ if(put_int_sum(a, b) == 0) break; return 0; } Description 88 输出指定区间内的所有整数。 Input 输入只有1行,即N,N是一个int类型的数据。 Output 如果N>0,则输出[1,N]区间内的所有整数;如果N =0,则输出0;如果N<0,则输出[N,-1]内的所有整数。 如果输出的整数多于1个,则两两之间用一个空格隔开。 Sample Input 9 Sample Output 1 2 3 4 5 6 7 8 9 #include<> int main() { int N,p; scanf(\"%d\ { if(N>0) for(p=1;p<=N;p++) { if(p==1) printf(\"%d\ else printf(\" %d\ } else if(N==0) printf(\"0\"); else 90 for(p=N;p<=-1;p++) { if(p==N) printf(\"%d\ else printf(\" %d\ } } } Description 设有函数y=f(x)定义为: 给定x的值,编程求出y的值并输出。 91 ----------------------------------------------------------------------------- 编写函数func()和output(),其原型为: double func(double x); 功能:计算f(x)并返回。 int output(int n, double x) 功能:按照题意的格式输出。 函数的调用格式见“Append Code”。 Input 输入的第一个是测试样例数N,后跟N个输入为x的值。 Output 输出为N行,每行顺序与输入对应的y=f(x)的计算结果,即y的值。输出时y值保留6位小数,且不输出无意义的0。 每行的格式为: case i:y=. 92 其中i表示测试用例编号(从1开始),表示计算结果。 Sample Input 4 -3 Sample Output case 1:y=3. case 2:y=. case 3:y=. case 4:y=. #include<> #include<> double func(double x) { 93 double y; if(x<0) y=-x; else if(x>=0&&x<1) y=sin(2*x); else if(x>=1&&x<5) y=sqrt(x*x*x+x); else y=2*x+10; return y; } int output(int n, double x) { 94 double y; printf(\"case %d:y=%lg.\\n\ } int main() { int i, cases; double x; scanf(\"%d\ for(i = 1; i <= cases; i++) { scanf(\"%lf\ output(i, func(x)); } 95 return 0; } #include<> #include<> double func(double x) { double y; if(x<0) y=-x; else if(x>=0&&x<1) y=sin(2*x); else if(x>=1&&x<5) y=sqrt(x*x*x+x); 96 else y=2*x+10; return y; } int output(int n, double x) { double y; printf(\"case %d:y=%lg.\\n\ } int main() { int i, cases; double x; 97 scanf(\"%d\ for(i = 1; i <= cases; i++) { scanf(\"%lf\ output(i, func(x)); } return 0; } Description 现有一个不超过N个元素的数组,其中可能有重复数据出现。求该数组中的最大值以及最大值所在的下标。 ----------------------------------------------------------------------------- 结合“Append Code”中的代码,编写以下函数: 原型:int get_array(int a[]); 98 功能:遵循样例输入的格式读取若干整数存放在a[]里,返回值为实际输入的元素个数。 原型:int put_array(int a[], int n); 功能:按格式输出a[]中的前n个元素。 原型:int max_value(int a[], int n); 功能:返回a[]中所有元素的最大值。 原型:int max_index(int idx[], int a[], int n); 功能:将a[]中所有最大值所在位置的索引(下标)存放在idx中,返回idx[]中元素的个数。 函数的调用格式见“Append Code”。 Input 第一个输入为正整数N<=1000,后面输入N个整数。 Output 输出是一行,其格式为: maximum number is , whose positions are:* 99 其中“”代表最大值(唯一的),“*”代表最大值所在的位置。如果最大值出现多次,则需输出所有的下标,下标两两之间由半角的逗号“,”隔开。 Sample Input 9 1 2 3 9 5 8 7 8 9 Sample Output maximum number is 9, whose positions are:3,8 HINT “Append Code”中用到的头文件、全局变量或宏的定义应自行补充。 #include <> #define MAX_SIZE 1001 int get_array(int a[]) { int N,i; 100 scanf(\"%d\ for(i=0; i return N; } #include <> int put_array(int a[], int n) { int i; printf(\"%d\ for(i=1; i #include <> 101 int max_value(int a[], int n) { int i,max=-48; for(i=0; i if(a[i]>max) max=a[i]; } return max; } #include <> int max_index(int idx[], int a[], int n) { 102 int i,max=-48,j=0; for(i=0; i if(a[i]>max) max=a[i]; } memset(idx,0,sizeof(idx)); for(i=0; i if(a[i]==max) idx[j++]=i; } return j; 103 } int main() { int array[MAX_SIZE], size; int index[MAX_SIZE], idx_size; size = get_array(array); idx_size = max_index(index, array, size); printf(\"maximum number is %d, \ printf(\"whose positions are:\"); put_array(index, idx_size); return 0; } [Submit][Status][Web Board] 104 Description 字符、整数和浮点数都可以按照数值来比较大小,字符串应该怎么比较呢让我们来编写一个程序,可以比较两个字符串的大小。 ----------------------------------------------------------------------------- 编写函数str_cmp(): 原型:int str_cmp(char s1[], char s2[]); 功能:按照指定的比较规则,比较字符串s1和s2的大小。若s1==s2,返回0;若s1 函数的调用格式见“Append Code”。 ----------------------------------------------------------------------------- Invalid Word(禁用单词)错误:在解决这个题目时,某些关键词是不允许被使用的。如果提交的程序中包含了下列的关键词之一,就会产生这个错误。 被禁用的头文件:和。 Input 105 输入为多组字符串,每组有两个字符串s1和s2,分两行输入,长度不超过100个字符。 Output 输出为多行,与每组输入对应输出为一个整数n。n为s1和s2的比较结果,若s1和s2每个位置上的字符都完全一样,输出0,否则输出s1和s2的第一个不相同的字符ASCII码值之差。 Sample Input abc ABC abc abC abc abc Sample Output 65 106 32 0 HINT “Append Code”中用到的头文件、全局变量或宏的定义应自行补充。 #include <> #define MAX_STR_LEN 101 int get_array(char a[]) { int i,j=0; for(i=0;;i++) { if(a[i]!=0) j++; 107 else break; } return j; } int str_cmp(char s1[], char s2[]) { int i,m,n; m=get_array(s1); n=get_array(s2); if(n>m) m=n;
Copyright © 2019- huatuo0.com 版权所有 湘ICP备2023021991号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务