您好,欢迎来到华佗健康网。
搜索
您的当前位置:首页c语言题目汇总

c语言题目汇总

来源:华佗健康网
 Description

给定一个球体的直径(非负数),求它的表面积和体积。

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(mmin=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(0Output

输出字母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(0Output

对每组输入,输出她们走的路长。

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(xmin=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;iscanf(\"%d\

for (i=1;ifor(j=0;jif(a[j]>a[j+1])

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开始,满足0Output

输出为多组,每组输出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{ scanf(\"%d%d\

for(i=0;ifor(j=0;jscanf(\"%d\j]);

61

for(i=0;i{ for(j=0;jprintf(\"%d \j][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,0Output

若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; iscanf(\"%d\

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;若s1s2,返回值是正数。若返回值不为0,返回的数值始终是s1和s2中第一个不相同位置的字符ASCII码值之差。

函数的调用格式见“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;

for(i=0;i108

{

if(s2[i]==s1[i])

{

if(i>=m-1)

return 0;

if(icontinue;

}

if(s2[i]!=s1[2])

return s1[i] - s2[i];

}

}

int main()

109

{

char str1[MAX_STR_LEN], str2[MAX_STR_LEN];

while( (gets(str1) != NULL) && (gets(str2) != NULL) )

printf(\"%d\\n\

return 0;

}

Description

现有一个非减序排序的一维数组,其中有若干元素是重复的。编程将重复元素删除掉若干个,仅保留1个,使数组中没有重复元素。

Input

输入有多行。第一行M>0,表示之后有M行输入。

之后的M行输入是若干个一维数组。每行的第一个数据0Output

110

输出有M行,每行输出与上述M组输入一一对应,是其输入数组去重后的结果。输出的数据需按照递增序排列,且每两个数组元素之间用一个空格隔开。

Sample Input

2

10 0 0 0 0 0 1 3 3 4 4

10 1 2 3 4 5 6 7 8 9 10

Sample Output

0 1 3 4

1 2 3 4 5 6 7 8 9 10

#include <>

int main()

{

int M,N,i,j,a[1001],b[1001],h,t;

scanf(\"%d\\n\

111

for(i=1;i<=M;i++)

{

scanf(\"%d\

for(j=0;jscanf(\" %d\j]);

for(j=1;j{

for(h=0;hif(a[h]>a[h+1])

{

t=a[h];

a[h]=a[h+1];

a[h+1]=t;

112

}

}

int l=0,u=0;

b[l]=a[0];

for(j=1;j<=N;j++)

{

if(b[l]==a[j])

{continue;

}

else

{

l++;

b[l]=a[j];

113

}

}

for(j=0;j{

if(j==0)

printf(\"%d\j]);

else

printf(\" %d\

}

printf(\"\\n\");

}

}

Description

114

辗转相除法,也称欧几里得算法,是求最大公约数的算法。辗转相除法首次出现于欧几里得的《几何原本》(第VII卷,命题i和ii)中,而在中国则可以追溯至东汉出现的《九章算术》。

两个整数的最大公约数(亦称公约数)是能够同时整除它们的最大的正整数。辗转相除法基于如下原理:两个整数的最大公约数等于其中较小的数和两数的差的最大公约数。根据这个原理,不难得出用辗转相除法求最大公约数的递归定义:

下面,给出两个正整数A和B,求他们的最大公约数((A,B))和最小公倍数(lcm(A,B))。

-----------------------------------------------------------------------------

Invalid Word(禁用单词)错误:在解决这个题目时,某些关键词是不允许被使用的。如果提交的程序中包含了下列的关键词之一,就会产生这个错误。

被禁用的关键字:循环语句for、while,甚至包括分支语句的switch、case、goto、break。

被禁用的头文件:、。

Input

115

输入为两个整数A和B,满足0Output

输出两数,分别为(A,B)和lcm(A,B),用一个空格分隔。

Sample Input

24 36

Sample Output

12 72

#include <>

int (int a,int b)

{

if(a-b==0)

return b;

if(a-b>0)

116

return (b,a-b);

if(a-b<0)

return (b,b-a);

}

int lcm(int a,int b)

{

int c;

c=a*b/((a,b));

return c;

}

int main()

{

int A,B;

117

scanf(\"%d %d\

printf(\"%d %d\

}

Description

输入2个整数,按照指定的格式输出这两个数。

Input

两个整数0<=a,b<=1000。a与b之间用一个空格隔开。

Output

输出有3行,第一行是:“Octal Decimal Hexadecimal”,每个单词之间用空格隔开。第二行和第三行分别是输入的两个整数a、b的八进制数、十进制数和十六进制数。输出时,每个数字左对齐,且八进制、十进制和十六进制数据分别与第一行中的字母O、D、H对齐。

Sample Input

13 456

Sample Output

118

Octal Decimal Hexadecimal

15 13 d

710 456 1c8

#include<>

int main()

{

int p,i;

scanf(\"%d%d\

printf(\"Octal Decimal Hexadecimal\\n\");

printf(\"%-5o %-7d %x\\n\

printf(\"%-5o %-7d %x\\n\

}

Description

119

各种程序设计语言里常见的取整函数有四个,分别是fix()、floor()、ceil()、round()。它们的功能细微之处各不相同,但有一点功能是共同的,就是把一个可能带小数点的浮点数转换成一个整数。

-----------------------------------------------------------------------------

编写函数myFloor()和myCeil():

原型:int myFloor(double data);

功能:下取整,返回不大于data的最大整数。

原型:int myCeil(double data);

功能:上取整,返回不小于data的最小整数。

函数的调用格式见“Append Code”。

-----------------------------------------------------------------------------

Invalid Word(禁用单词)错误:在解决这个题目时,某些关键词是不允许被使用的。如果提交的程序中包含了下列的关键词之一,就会产生这个错误。

被禁用的头文件:和。

Input

120

输入有多行,每行是一个可能带小数点的浮点数m。

Output

输出为多行,与上述输入一一对应。每行输出两个整数:m上取整和下取整的结果,两者之间用一个空格分隔开。

Sample Input

2

Sample Output

1 2

-2 -1

2 2

#include<>

int myFloor(double data)

{

if((int)data!=data)

121

{

if(data>=0)

return data;

else

return data-1;

}

else

return data;

}

int myCeil(double data)

{

if((int)data!=data)

{

122

if(data>=0)

return data+1;

else

return data;

}

else

return data;

}

int main()

{

double data;

while(scanf(\"%lf\

printf(\"%d %d\\n\

123

return 0;

}

Description

求三个整数的最大值。

-----------------------------------------------------------------------------

编写一个函数maxValue()求三个整数的最大值。其原型为:

int maxValue(int a,int b,int c);

功能:函数的三个参数传入a,b,c的值,返回其中最大值。

函数的调用格式见“Append Code”。

Input

输入三个int类型的整数,两两之间用空格隔开。

Output

输出三个整数的最大值。

124

Sample Input

1 2 3

Sample Output

3

HINT

参看系统首页上的“Append Code”使用说明,讨论版(Web Board)上也有。

#include <>

int maxValue(int a,int b,int c)

{

int x;

x=a>ba:b;

if(xx=c;

125

return x;

}

int main()

{

int x, y, z;

scanf(\"%d %d %d\

printf(\"%d\

return 0;

}

Description

在数组a[]中查找某个值val。

-----------------------------------------------------------------------------

结合“Append Code”中的代码,编写以下函数:

126

原型:int getarray(int a[]);

功能:遵循样例输入的格式读取一个数组存放在a[]里,返回输入的数组元素个数。

原型:int find(int a[], int n, int val);

功能:在有n个元素的数组a[]中查找值为val的元素。若找到,返回第一个值为val的元素下标,否则返回-1。

函数的调用格式见“Append Code”。

Input

输入的第一行为一个整数M(M>0),后面有M个测试样例。

每个测试样例有两行输入;第一行的第一个整数为N(N<=1000),后接一个长度为N的数组a[];第二行为一个整数值val。

Output

输出有M行。每行输出一个测试样例的结果:若val在数组中,则输出第一个值为val的数组元素下标;否则输出“NOT FOUND”。

Sample Input

4

127

3 1 2 3

1

5 10 15 20 30 50

50

4 100 200 300 400

500

0

0

Sample Output

0

4

NOT FOUND

NOT FOUND

128

HINT

“Append Code”中用到的头文件、全局变量或宏的定义应自行补充。

#include<>

int find(int a[],int n, int val)

{

int i;

for(i=0;i{

if(val==a[i])

return i;

}

return -1;

}

129

#define MAX_SIZE 1007

int getarray(int a[])

{

int size,i;

scanf(\"%d\

for (i=0;iscanf(\"%d\

return size;

}

int main()

{

int cases, i;

int arr[MAX_SIZE], size;

130

int val, found = 0;

scanf(\"%d\

for(i = 1; i <= cases; i++)

{

size = getarray(arr);

scanf(\"%d\

found = find(arr, size, val);

if(found == -1)

{

printf(\"NOT FOUND\\n\");

continue;

}

printf(\"%d\\n\

131

}

return 0;

}

Description

从键盘上输入0~100之间的三个数,按从小到大的顺序输出。

-----------------------------------------------------------------------------

编写一个函数compare()用来比较三个数的大小,按它们的大小排好位置:

原型:int compare(int *min, int *mid, int *max); ()

int compare(int &min, int &mid, int &max); ()

功能:这里用到传地址,比较三个参数的数值大小,最小值的存入min,最大值存入max,mid是存中间那个值。

函数的调用格式见“Append Code”。

Input

输入只有一行,为三个整数。

132

Output

按从小到大输出这三个数。

Sample Input

15 10 20

Sample Output

10 15 20

HINT

传地址可以使得在函数中改变实参的值,学习怎么用指针吧。

#include <>

int compare(int *a, int *b, int *c)

{

int d;

if (*a>*c)

133

{d=*a;

*a=*c;

*c=d;}

if (*a>*b)

{d=*a;

*a=*b;

*b=d;}

if (*b>*c)

{d=*b;

*b=*c;

*c=d;}

return 0;

}

134

int main()

{

int a, b, c;

scanf(\"%d %d %d\

compare(&a, &b, &c);

printf(\"%d %d %d\

return 0;

}

Description

给出三个数a,b,c,最大值是最小值是

-----------------------------------------------------------------------------

编写以下两个函数:

get_num()的功能是读取输入的三个整数a,b,c;

135

max_min()的功能是求出a,b,c的最大值和最小值。

以上函数的调用格式见“Append Code”。这里不给出函数原型,请通过main()函数自行确定。

Input

输入的第一个整数n,表示有n组测试数据,每组3个整数:a,b,c。a,b,c都在int类型范围内。

Output

每组测试数据对应输出一行:为a,b,c的最大值和最小值,格式见sample。

Sample Input

5

20 15 10

10 15 20

100 100 0

0 1 -1

136

0 0 0

Sample Output

case 1 : 20, 10

case 2 : 20, 10

case 3 : 100, 0

case 4 : 1, -1

case 5 : 0, 0

#include <>

void get_num(int *a,int *b,int *c)

{scanf(\"%d%d%d\

}

void max_min(int *mmax,int *mmin,int a,int b, int c)

{ if(a>=b)

137

{*mmax=a;

*mmin=b;}

else{*mmax=b;

*mmin=a;}

if(*mmax<=c)

*mmax=c;

if (*mmin>=c)

*mmin=c;

}

int main()

{

int cases, i;

int mmax, mmin, a, b, c;

138

scanf(\"%d\

for(i = 1; i <= cases; i++)

{

get_num(&a, &b, &c);

max_min(&mmax, &mmin, a, b, c);

printf(\"case %d : %d, %d\\n\

}

}

Description

给出两整数min和max,求min到max之间的数的累加和。

-----------------------------------------------------------------------------

编写一个函数add()。函数原型为

int add(int n);

139

功能:每次调用都把参数n的值累加起来,并返回。

函数add()的调用格式见“Append Code”。

Input

输入为两个整数min和max,且max>=min。

Output

min和max之间所有整数的累加和,包括min和max。

Sample Input

1 10

Sample Output

55

HINT

add()函数里要用静态变量来存储累加和。当然用全局变量也行,不过还是练练怎么用静态变量吧。

#include<>

140

int add(int n)

{static int s=0;

s+=n;

return s;

}

int main()

{

int min, max, sum;

scanf(\"%d%d\

while(min <= max)

sum = add(min++);

printf(\"%d\

return 0;

141

}

Description

手机大家都有吧最上面的电量指示图都知道吧现在呢,就需要你来编一个程序,根据用户给出的电量,模拟一个电量示意图。

原理很简单:对于给定的一个0~100之内的整数N(N就是电量的百分比,0%表示没电了,100%表示满电),用N个“|”来表示电量。

Input

输入有多行。第一行是一个非负整数N,表示后面有N行输入。之后的N行,每一行是一个介于[0,100]的整数K,表示要显示的电量。

Output

输出有N行,每行输出与上述输入一一对应。输出的格式为:

case : |||||||||......

其中表示用例编号(从1开始),”:”前、后各有一个空格,“|”的个数与每行输入相同。

Sample Input

142

4

0

1

20

3

Sample Output

case 1 :

case 2 : |

case 3 : ||||||||||||||||||||

case 4 : |||

#include<>

int main()

{

143

int N,k,i,j,count=0;

scanf(\"%d\

for(i=0;i{

scanf(\"%d\

count++;

printf(\"case %d : \

for(j=0;jprintf(\"|\");

printf(\"\\n\");

}

}

Description

144

编写一个程序,求矩阵中各列元素值相加的平均值。其中,矩阵的元素都是很小的整数,且各列元素之和的数值不会超出int类型的表示范围。

-----------------------------------------------------------------------------

编写三个函数完成程序:

原型:int get_matrix(int mtx[][], int m, int n);

功能:按照输入格式,读取一个m行n列的矩阵存放在二维数组mtx[][]中。

原型:int put_array(double arr[], int n);

功能:按照输出格式,输出有n个元素的一维数组arr[]。

原型:int count_average(double arr[], int mtx[][], int m, int n);

功能:计算m行n列的矩阵mtx[][]各列的平均值,存放到数组arr[]中。

函数的调用格式见“Append Code”。

Input

输入为多行。第一行K>0,表示有K个测试用例。

之后K个测试用例中,首先是两个整数0<=M,N<=100,表示该测试用例的矩阵是

145

一个M行N列的矩阵。之后是一个M行N列的整数组成的矩阵。

Output

输出有K行,每个测试用例的结果占一行。每行的格式为:

case i:d1 d2 ... dj

其中i表示测试用例的编号(从1开始),d1、d2、....、dj表示相应测试用例的各列元素相加的平均值,两两之间用空格隔开。

每个平均值用%lg输出即可。

Sample Input

4

3 3

1 2 3

1 2 3

1 2 3

2 3

146

1 1 1

1 1 1

1 1

1

5 1

3

4

5

6

7

Sample Output

case 1:1 2 3

case 2:1 1 1

147

case 3:1

case 4:5

#include <>

#define MAX_SIZE 101

int get_matrix(int mtx[101][101], int m, int n)

{

int i,j;

for(i=0;i<=m-1;i++)

for(j=0;j<=n-1;j++)

scanf(\"%d\j]);

}

int put_array(double arr[], int n)

{

148

int i,k=0;

for(i=0;i<=n-1;i++)

{

if(k++)

printf(\" %lg\

else

printf(\"%lg\

}

printf(\"\\n\");

}

int count_average(double arr[101], int mtx[101][101], int m, int n)

{

int i,j;

149

for(i=0;i<101;i++)

arr[i]=0;

for(i=0;i<=n-1;i++)

{

for(j=0;j<=m-1;j++)

arr[i]=mtx[j][i]+arr[i];

}

for(i=0;i<=n-1;i++)

arr[i]=arr[i]/m;

}

int main()

{

int i, cases;

150

double average[MAX_SIZE];

int m, n, matrix[MAX_SIZE][MAX_SIZE];

scanf(\"%d\

for(i = 1; i <= cases; i++)

{

scanf(\"%d%d\

get_matrix(matrix, m, n);

count_average(average, matrix, m, n);

printf(\"case %d:\

put_array(average, n);

}

return 0;

}

151

[Submit][Status][Web Board]

Description

将输入的一个字符串s逆序输出。

-----------------------------------------------------------------------------

编写一个函数str_rev()求一个串的逆序串:

原型:char * str_rev(char * t, char * s);

功能:把串s逆序复制到串t中,返回值是逆序串t。

函数的调用格式见“Append Code”。

Input

输入为一个串s。输入最少为一个字符,最多不会超过100个字符。

Output

串s的逆序。

输出两遍,一遍是测试返回值,一遍是测试str。

152

Sample Input

ABCDE

Sample Output

EDCBA

EDCBA

HINT

str_rev()的返回值参考标准库函数strcpy()、目标串的指针。

#include <>

#include <>

#include <>

#define MAX_STR_LEN 101

char * str_rev(char * t, char * s)

{

153

strstr()、strchr()的设计思路:返回指向 int n,l,j;

l=strlen(s);

for(n=l-1;n>=0;n=n-1)

{

j=*s++;

t[n]=j;

t[l]='\\0';

}

return t;

}

int main()

{

char s[MAX_STR_LEN], str[MAX_STR_LEN], *p;

1

gets(s);

p = str_rev(str, s);

puts(p);

puts(str);

return 0;

}

Description

将输入的一个字符串s逆序输出。

-----------------------------------------------------------------------------

编写一个函数revs()把一个串在原地(原存储位置上)倒转顺序:

原型:char * revs(char * s);

功能:要逆序的字符串s作为参数传入,revs(s)函数调用后,s中存储的串为原串的逆序。

函数的调用格式见“Append Code”。

155

Input

输入为一个串s。输入最少为一个字符,最多不会超过100个字符。

Output

串s的逆序。

Sample Input

ABCDE

Sample Output

EDCBA

HINT

本题中,函数原型中的“char * s”可以认为等同于“char s[]”可以忽略,因为实际并没有用到,或者用“return s”实现即可。

#include <>

#include <>

#define MAX_STR_LEN 105

156

而返回值的“char *”, char * revs(char * s)

{

int K,j,n,t;

n=strlen(s);

for(K=0,j=n-1;K<=j;K++,j--)

{

t=*(s+K);

*(s+K)=*(s+j);

*(s+j)=t;

}

}

int main()

{

157

char s[MAX_STR_LEN];

gets(s);

revs(s);

puts(s);

return 0;

}

Description

求k个数的平均值。

Input

输入分为两部分:第一个数是k,然后输入k个较小的整数。

Output

输出为这k个整数的平均值,保留3位小数。

Sample Input

158

3

1 2 3

Sample Output

#include <>

int main()

{ int n,a[100000],i;

double d,s=0;

scanf(\"%d\\n\

for(i=0;i{scanf(\"%d\

s=s+a[i];}

d=s/n;

printf(\"%.3lf\

159

}

Description

对于给出的一个由非负整数组成的一个序列,求其中的第i个数是多少。

编写下面的函数:

void getData(int data[],int i,int n,int *p);

参数:data[]是需要在其中查找的数组,i是需要查找的数据的序号,n是data[]的长度,p用于返回data中的第i个值。如果i不是一个合法的序号(合法的序号从0开始计数),则*p的值被置为-1,否则置为data[]中的第i个数。

Input

输入有3行:第一行是一个整数101>N>0,表示数组有N个数;第二行含有N个非负整数 ,是数组的元素;第三行是一个int类型范围内的整数,表示要查找的元素的序号。

Output

如果找到了,输出:

data[] = #.

其中、#分别是下标(即输入的序号)和相应的值。

160

否则,输出:

The index is overflow.

Sample Input

10

1 2 3 4 5 6 7 8 9 10

1

Sample Output

data[1] = 2.

#include <>

void getData(int data[],int i,int n,int *p)

{

if(i>=0&&i<=n-1)

*p=data[i];

161

else *p=-1;

}

int main()

{

int data[100],N,order,i,goal;

scanf(\"%d\

for (i=0;iscanf(\"%d\

scanf(\"%d\

getData(data,order,N,&goal);

if (goal>-1)

printf(\"data[%d] = %d.\\n\

else

162

puts(\"The index is overflow.\");

return 0;

}

Description

我们知道,5/3=1+3/5,称等号后面的式子为最简分式。也就是说,在分式中,分子一定要小于分母,而且分子、分母是互质的。Now,请写一个程序,对于用户输入的一个合法分式,输出其约简之后的结果。

——————————————————————————————————————————————————————————

编写如下两个函数:

1. int (int m,int n); 用于求两个正整数m和n的最大公约数 ,返回值即m和n的最大公约数。

2. void simplify(int m,int n,int *in,int *num, int *den);对m/n进行约简。m、n分别表示分式的分子和分母,而且是互质的。in、num和den分别用于返回约简后的整数部分、分子部分和分母部分。

Input

163

输入有多行,每行是“m/n”的格式表示的一个分式,其中m和n都是int类型范围内的正整数。

输入至EOF结束。

Output

输出的行数与输入一致,且每行输出与上述输入一一对应。

如果约简后,分子部分是0,则只输出整数部分。

如果整数部分是0,则按照“j/k”的格式输出;

如果整数部分不是0,则按照“i+j/k” 的格式输出约简后的结果。

其中i、j和k分别是约简后的整数部分、分子部分和分母部分。

Sample Input

100/56

8/4

3/5

Sample Output

1

1+11/14

2

3/5

#include <>

int (int m,int n)

{

int i,d;

if(m>=n) d=n;

else d=m;

for(i=d;i>=1;i--)

{

if(m%i==0&&n%i==0)

break;

165

}

return i;

}

void simplify(int m,int n,int *in,int *num, int *den)

{

if(m%n==0)

{*num=0;

*in=m/n;}

else if(m{

*in=0;

*num=m;

*den=n;

166

}

else{*in=m/n;

*num=m-(m/n)*n;

*den=n;}}

int main()

{

int m,n,g,in,den,num;

while (scanf(\"%d/%d\

{

g = (m,n);

m /= g;

n /= g;

simplify(m,n,&in,&num,&den);

167

if (num == 0)

printf(\"%d\\n\

else if (in == 0)

printf(\"%d/%d\\n\

else

printf(\"%d+%d/%d\\n\

}

return 0;

}

Description

给出部分程序如下,在横线内填入合适的内容使这个程序按规定格式输出:

#include <>

int main()

168

{

int x,y;

float f ;

scanf(\"%d%d%f\

printf(\" \ );

}

Input

分别输入两个整数和一个实数

Output

按如下格式输出(其中实数保留两位小数输出,行尾没有回车):

x=10,y=20,f=

Sample Input

10 20

169

Sample Output

x=10,y=20,f=

#include <>

int main()

{

int x,y;

float f ;

scanf(\"%d%d%f\

printf(\"x=%d,y=%d,f=%.2f\\n\

}

Description

本程序的功能是输出学生的信息(包括专业,姓名和总分),请填空。#include<>

170

#define N 3

typedef struct

{

char major[50];

char name[50];

int score[3];

} STU;

void printInfo( {

int i,totalScore;

for(i=0;i{

totalScore= 171

)

;

printf(\"%s,%s:%d.\\n\ );

}

}

int main()

{

int i;

STU stus[N];

for (i=0;i{

scanf(\"%s %s\

scanf(\"%d%d%d\

}

172

printInfo(stus);

return 0;

}

Input

输入N名学生的专业,姓名和三门课程成绩

Output

输出N名学生的专业,姓名和总成绩

Sample Input

se zhangsan 90 88

se lisi 67 66 65

se wangwu 78 77 79

Sample Output

se,zhangsan:267.

173

se,lisi:198.

se,wangwu:234.

#include<>

#define N 3

typedef struct

{

char major[50]; char name[50]; int score[3];

} STU;

void printInfo( {

int i,totalScore; 174

)

STU *stus

for(i=0;i{

totalScore=

stus[i].score[0]+stus[i].score[1]+stus[i].score[2]; ;

printf(\"%s,%s:%d.\\n\

stus[i].major,stus[i].name,totalScore );

}

}

int main()

{

int i;

STU stus[N];

for (i=0;i175

{

scanf(\"%s %s\

scanf(\"%d%d%d\

}

printInfo(stus);

return 0;

}

Description

以下程序的功能是统计字符串中大写字母和小写字母的个数。请填空。

#include<>

void fun(char *s,int *a,int *b)

{ int i;

for(i=0; ;i++)

176

{ if( )

(*a)++;

if(s[i]>='a'&&s[i]<='z')

;

}

}

int main( )

{

char s[101];

int upper=0,lower=0;

gets(s);

fun(s,&upper,&lower);

177

printf(\"upper=%d,lower=%d\

}

Input

输入一个字符串,不超过100个字符。

Output

输出大写字母和小写字母的个数。格式见Sample Output。

Sample Input

abcABC1234ABC

Sample Output

upper=6,lower=3

#include<>

void fun(char *s,int *a,int *b)

{ int j;

178

for(j=0; s[j]!=0;j++)

{ if( s[j]>='A'&&s[j]<='Z')

(*a)++;

if(s[j]>='a'&&s[j]<='z')

(*b)++ ;

}

}

int main( )

{

char s[101];

int upper=0,lower=0;

gets(s);

179

fun(s,&upper,&lower);

printf(\"upper=%d,lower=%d\

}

Description

输出由数字字符组成的图形。

Input

输入为一个正整数N,不超过int类型的表示范围。

Output

输出由N行数字字符组成的等腰三角形,其中第i(0(1)如果0(2)如果i>9,则输出i的个位数。

Sample Input

5

180

Sample Output

1

222

33333

4444444

5

#include <>

int main()

{ int n,i,j,k;

scanf(\"%d\

for(i=1;i<=n;i++)

{for(j=1;j<=n-i;j++)

printf(\" \");

181

for(j=1;j<=2*i-1;j++)

{k=i-i/10*10;

printf(\"%d\

printf(\"\\n\");}

}

Description

现有一个不超过N个元素的数组,其中没有重复数据出现。求该数组中的最小值以及最小值所在的下标。

-----------------------------------------------------------------------------

结合“Append Code”中的代码,编写以下函数:

原型:int get_array(int a[], int n);

功能:遵循样例输入的格式读取n个整数存放在a[]里,返回值为实际输入的元素个数。

原型:int min_index(int a[], int n);

功能:返回有n个元素的数组a[]中的所有元素的最小值的索引(下标)。

182

函数的调用格式见“Append Code”。

Input

第一个输入为正整数N<=1000,后面输入N个整数。

Output

输出只有一行。格式为:

minimum number is x, whose position is y.

其中x是数组中的最小元素值,y是x在数组中的下标。

Sample Input

10

1 2 3 4 5 6 7 8 9 -1

Sample Output

minimum number is -1, whose position is 9.

HINT

183

“Append Code”中用到的头文件、全局变量或宏的定义应自行补充

#include <>

#define MAX_SIZE 1007

int get_array(int a[], int n)

{

int i;

for(i=0;i{

scanf(\"%d\

}

return i;

}

int min_index(int a[], int n)

184

{

int i,j,min;

for(i=0;i{

if(i==0)

{

min=a[i];

}

else

{

if(min>a[i])

min=a[i];

}

185

}

for(j=0;j{

if(a[j]==min)

{

return j;

}

}

}

int main()

{

int array[MAX_SIZE], size, index;

scanf(\"%d\

186

get_array(array, size);

index = min_index(array, size);

printf(\"minimum number is %d, whose position is %d.\

return 0;

}

Description

计算平面上两点之间的直线距离。

-----------------------------------------------------------------------------

编写两个函数input_point()和distance()完成程序:

原型:void input_point(POINT *pt);

功能:按格式输入一个点的坐标,存入pt所指的内存中。

原型:double distance(POINT p1, POINT p2);

功能:返回点p1到p2的直线距离。

187

函数的调用格式见“Append Code”,结构体“POINT”的类型定义根据“Append Code”自行给出。

Input

输入有2行,每行有2个实数,分别代表两个点的坐标。

Output

输出两个点之间的距离,用%g格式输出即可。

Sample Input

Sample Output

#include <>

#include <>

typedef struct point

{

double x;

double y;

188

}POINT;

void input_point(POINT *pt)

{

scanf(\"%lf%lf\

}

double distance(POINT p1,POINT p2)

{

return sqrt(pow(

int main()

{

POINT p,q;

double dis;

input_point(&p);

1

input_point(&q);

dis=distance(p,q);

printf(\"%g\

return 0;

}

Description

计算两整数x和y(0Input

输入只有一行,格式见sample。

Output

输出为多行,按顺序每行输出x,y的和、差、积、商、余数、x的平方和y的三次方,格式见sample

Sample Input

x = 11, y = 3

190

Sample Output

x + y : 14

x - y : 8

x * y : 33

x / y quotient: 3, remainder: 2

x ^ 2 : 121

y ^ 3 : 27

#include <>

int main ()

{ int x,y;

scanf(\"x = %d, y = %d\

printf(\"x + y : %d\\n\

printf(\"x - y : %d\\n\

191

printf(\"x * y : %d\\n\

printf(\"x / y quotient: %d, remainder: %d\\n\

printf(\"x ^ 2 : %d\\n\

printf(\"y ^ 3 : %d\

return 0;

}

Description

输入一个整数,判读它是奇数还是偶数。

Input

输入只有一行,为一个100以内的正整数。

Output

输出为一行。

若输入为偶数则输出“even”,奇数输出“odd”。

192

Sample Input

30

Sample Output

even

#include<>

int main()

{

int n;

scanf(\"%d\

if((n%2)!=0)

printf(\"odd\\n\");

else

printf(\"even\\n\");

193

}

Description

Xiao_ming有两个哥哥,大哥叫Da_min,二哥叫Er_min。三兄弟放学回家,父母分别跟他们打招呼。

Input

Output

请输出:

Hello Da_min,

Hello Er_min,

Hello Xiao_ming!

Sample Input

Sample Output

Hello Da_min,

194

Hello Er_min,

Hello Xiao_ming!

#include<>

void main()

{

printf(\"Hello Da_min,\\nHello Er_min,\\nHello Xiao_ming!\\n\");

}

195

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.com 版权所有 湘ICP备2023021991号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务