部分ACM题目与问题详解.doc
word1001 Sum Problem21089 A+B for Input-Output Practice (I)41090 A+B for Input-Output Practice (II)61091 A+B for Input-Output Practice (III)81092 A+B for Input-Output Practice (IV)91093 A+B for Input-Output Practice (V)111094 A+B for Input-Output Practice (VI)121095 A+B for Input-Output Practice (VII)131096 A+B for Input-Output Practice (VIII)142000 ASCII码排序162001计算两点间的距离172002计算球体积192003求绝对值202004成绩转换212005第几天?222006求奇数的乘积242007平方和与立方和262008数值统计272009求数列的和282010水仙花数292011多项式求和312012素数判定332014青年歌手大奖赛_评委会打分342015偶数求和362016数据的交换输出382017字符串统计402019数列有序!412020绝对值排序432021发工资咯:452033人见人爱A+B462039三角形482040亲和数4991 / 911001 Sum ProblemProblem DescriptionHey, wele to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + . + n. InputThe input will consist of a series of integers n, one integer per line. OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer. Sample Input1100 Sample Output15050 AuthorDOOM III解答:#include<stdio.h>main() int n,i,sum; sum=0; while(scanf("%d",&n)!=-1) sum=0; for(i=0;i<=n;i+)sum+=i; printf("%dnn",sum);1089 A+B for Input-Output Practice (I)Problem DescriptionYour task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners. You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim. InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. Sample Input1 510 20 Sample Output630 Authorlcy RemendJGShining解答:#include<stdio.h> main() int a,b; while(scanf("%d%d",&a,&b)!=EOF)printf("%dn",a+b);1090 A+B for Input-Output Practice (II)Problem DescriptionYour task is to Calculate a + b. InputInput contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line. OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. Sample Input21 510 20 Sample Output630 Authorlcy RemendJGShining解答:#include<stdio.h>#define M 1000void main() int a ,b,n,jM,i;/printf("please input n:n"); scanf("%d",&n);for(i=0;i<n;i+) scanf("%d%d",&a,&b); /printf("%d %d",a,b); ji=a+b; i=0; while(i<n) printf("%d",ji); i+; printf("n"); 1091A+B for Input-Output Practice (III)Problem DescriptionYour task is to Calculate a + b. InputInput contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed. OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. Sample Input1 510 200 0 Sample Output630 Authorlcy RemendJGShining解答:#include<stdio.h> main() int a,b; scanf("%d %d",&a,&b); while(!(a=0&&b=0) printf("%dn",a+b);scanf("%d %d",&a,&b); 1092A+B for Input-Output Practice (IV)Problem DescriptionYour task is to Calculate the sum of some integers. InputInput contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed. OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input. Sample Input4 1 2 3 45 1 2 3 4 50 Sample Output1015 Authorlcy RemendJGShining 解答:#include <stdio.h>int main() int n,sum,i,t; while(scanf("%d",&n)!=EOF&&n!=0) sum=0; for(i=0;i<n;i+) scanf("%d",&t); sum=sum+t; printf("%dn",sum); 1093 A+B for Input-Output Practice (V)Problem DescriptionYour task is to calculate the sum of some integers. InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input. Sample Input24 1 2 3 45 1 2 3 4 5 Sample Output1015 Authorlcy解答:#include<stdio.h>main() int n,a,b,i,j,sum; sum=0; while(scanf("%dn",&n)!=-1) for(i=0;i<n;i+) scanf("%d",&b); for(j=0;j<b;j+) scanf("%d",&a);sum+=a; printf("%dn",sum);sum=0; 1094 A+B for Input-Output Practice (VI)Problem DescriptionYour task is to calculate the sum of some integers. InputInput contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line. OutputFor each test case you should output the sum of N integers in one line, and with one line of output for each line in input. Sample Input4 1 2 3 45 1 2 3 4 5 Sample Output1015 Authorlcy RemendJGShining解答:#include<stdio.h>main() int n,a,b,i,j,sum;sum=0; while(scanf("%dn",&n)!=-1) for(j=0;j<n;j+) scanf("%d",&a); sum+=a; printf("%dn",sum); sum=0; Copy to Clipboard Save to File1095A+B for Input-Output Practice (VII)Problem DescriptionYour task is to Calculate a + b. InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. OutputFor each pair of input integers a and b you should output the sum of a and b, and followed by a blank line. Sample Input1 510 20 Sample Output630 Authorlcy RemendJGShining解答:#include<stdio.h> main() int a,b; while(scanf("%d%d",&a,&b)!=EOF)printf("%dnn",a+b);1096 A+B for Input-Output Practice (VIII)Problem DescriptionYour task is to calculate the sum of some integers. InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. OutputFor each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs. Sample Input34 1 2 3 45 1 2 3 4 53 1 2 3 Sample Output10156 Authorlcy RemendJGShining解答:int main() int a,b,i,j,l1000,k; scanf("%d",&i); getchar(); for(j=1;j<=i;j+) lj=0; for(j=1;j<=i;j+) scanf("%d",&a); getchar(); for(k=1;k<=a;k+) scanf("%d",&b); getchar(); lj+=b; for(j=1;j<=i-1;j+) printf("%dnn",lj); printf("%dn",li);2000 ASCII码排序Problem Description输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。 Input输入数据有多组,每组占一行,有三个字符组成,之间无空格。 Output对于每组输入数据,输出一行,字符中间用一个空格分开。 Sample Inputqweasdzxc Sample Outpute q wa d sc x z Authorlcy SourceC语言程序设计练习一 RemendJGShining解答:#include<stdio.h>main() char a,b,c,d; while(scanf("%c %c %c",&a,&b,&c)!=EOF) getchar(); if(a>=b) if(c>=a)printf("%c %c %cn",b,a,c);else if(b>=c) printf("%c %c %cn",c,b,a); else if(b<c) printf("%c %c %cn",b,c,a); else if(c>=b) printf("%c %c %cn",a,b,c); else if(c>=a) printf("%c %c %cn",a,c,b); else if(a>c) printf("%c %c %cn",c,a,b); 2001计算两点间的距离Problem Description输入两点坐标X1,Y1,X2,Y2,计算并输出两点间的距离。 Input输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。 Output对于每组输入数据,输出一行,结果保存两位小数。 Sample Input0 0 0 10 1 1 0 Sample Output Authorlcy SourceC语言程序设计练习一 RemendJGShining 解答:#include<stdio.h>#include<math.h>main() double a,b,c,d,s; while(scanf("%lf %lf %lf %lf",&a,&b,&c,&d)!=EOF) s=sqrt(a-c)*(a-c)+(b-d)*(b-d); printf("%.2lfn",s); 2002计算球体积Problem Description根据输入的半径值,计算球的体积。 Input输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。 Output输出对应的球的体积,对于每组输入数据,输出一行,计算结果保存三位小数。 Sample Input1 Sample OutputHint Authorlcy SourceC语言程序设计练习一 RemendJGShining解答:#include<stdio.h>main() double a,v; while(scanf("%lf",&a)!=EOF) v=4*PI*a*a*a/3; printf("%.3lfn",v); 2003求绝对值Problem Description数的绝对值。 Input输入数据有多组,每组占一行,每行包含一个实数。 Output对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保存两位小数。 Sample Input123 Sample Output Authorlcy SourceC语言程序设计练习一 RemendJGShining解答:#include<stdio.h>main() double a; while(scanf("%lf",&a)!=EOF) if(a<0) a=-a; printf("%.2lfn",a); 2004成绩转换Problem Description输入一个百分制的成绩t,将其转换成对应的等级,具体转换规如此如下:90100为A;8089为B;7079为C;6069为D;059为E; Input输入数据有多组,每组占一行,由一个整数组成。 Output对于每组输入数据,输出一行。如果输入数据不在0100围,请输出一行:“Score is error!。 Sample Input5667100123 Sample OutputEDAScore is error! Authorlcy SourceC语言程序设计练习一 RemendJGShining解答:#include <stdio.h>int main() int n; while(scanf("%d",&n)!=EOF) if(n>100|n<0)printf("Score is error!n"); else if(n>=90)printf("An"); else if(n>=80)printf("Bn"); else if(n>=70)printf("Cn"); else if(n>=60)printf("Dn"); else printf("En"); return 0;2005第几天?Problem Description给定一个日期,输出这个日期是该年的第几天。 Input输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。 Output对于每组输入数据,输出一行,表示该日期是该年的第几天。 Sample Input1985/1/202006/3/12 Sample Output2071 Authorlcy SourceC语言程序设计练习一 RemendJGShining解答:#include<stdio.h>main() int a,b,c,d,e,f,g; while(scanf("%d/%d/%d",&a,&b,&c)!=EOF) if(b=1) d=c; else if(b=2) d=31+c; else if(b=3) d=31+28+c; else if(b=4) d=31+28+31+c; else if(b=5) d=31+31+28+30+c; else if(b=6) d=31+28+31+30+31+c; else if(b=7) d=31+28+31+30+31+30+c; else if(b=8) d=31+28+31+30+31+30+31+c; else if(b=9) d=31+28+31+30+31+30+31+31+c; else if(b=10) d=31+28+31+30+31+30+31+31+30+c; else if(b=11) d=31+28+31+30+31+30+31+31+30+31+c; else if(b=12) d=31+28+31+30+31+30+31+31+30+31+c+30; e=a%100; f=a%400; g=a%4; if(e=0) if(f=0) d=1+d; else d=d; else if(g=0) d=d+1; else d=d; printf("%dn",d); 2006求奇数的乘积Problem Description给你n个整数,求他们中所有奇数的乘积。 Input输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。 Output输出每组数中的所有奇数的乘积,对于测试实例,输出一行。 Sample Input3 1 2 34 2 3 4 5 Sample Output315 Authorlcy SourceC语言程序设计练习一 RemendJGShining解答:#include<stdio.h>main() int n,s,i,a; while(scanf("%d",&n)!=EOF) s=1; for(i=0;i<n;i+) scanf("%d",&a); if(a%2=1) s=s*a; else ; printf("%dn",s); 2007平方和与立方和Problem Description给定一段连续的整数,求出他们中所有偶数的平方和以与所有奇数的立方和。 Input输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。 Output对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以与所有奇数的立方和。你可以认为32位整数足以保存结果。 Sample Input1 32 5 Sample Output4 2820 152 Authorlcy SourceC语言程序设计练习一 RemendJGShining解答:#include<stdio.h>int main() int sum1,sum2,n,i,m,t; while(scanf("%d%d",&m,&n)!=EOF) sum1=sum2=0; if(m>n)t=m;m=n;n=t;for(i=m;i<=n;i+) if(i%2=0) sum1+=(i*i); else sum2+=(i*i*i); printf("%d %dn",sum1,sum2); return 0;2008数值统计Problem Description统计给定的n个数中,负数、零和正数的个数。 Input输入数据有多组,每组占一行,每行的第一个数是整数nn<100,表示需要统计的数值的个数,然后是n个实数;如果n=0,如此表示输入完毕,该行不做处理。 Output对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。 Sample Input6 0 1 2 3 -1 00 Sample Output1 2 30 0 5 Authorlcy SourceC语言程序设计练习二 RemendJGShining解答:#include<stdio.h>int main() int n,i,b1,b2,b3; double a101; while(scanf("%d",&n)!=EOF && n!=0) for(i=0;i<n;i+) scanf("%lf",&ai); b1=b2=b3=0; for(i=0;i<n;i+) if(ai<0) b1+; else if(ai=0) b2+; else b3+; printf("%d %d %dn",b1,b2,b3); 2009求数列的和Problem Description数列的定义如下:数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。 Input输入数据有多组,每组占一行,由两个整数nn<10000和m(m<1000)组成,n和m的含义如前所述。 Output对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保存2位小数。 Sample Input81 42 2 Sample Output Authorlcy SourceC语言程序设计练习二 RemendJGShining解答:#include<stdio.h>#include<math.h>main() double n,m,s,w,i; while(scanf("%lf%lf",&n,&m)!=EOF) s=n; for(i=1;i<m;i+) n=sqrt(n); s=s+n; printf("%.2lfn",s);2010水仙花数Problem Description春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33。现在要求输出所有在m和n围的水仙花数。 Input输入数据有多组,每组占一行,包括两个整数m和n100<=m