1000 A + B Problem
Problem Description
Calculate A + B.
Input
Each line will contain two integers A and B. Process to end of file.
Output
For each case, output A + B in one line.
Sample Input
1 1
Sample Output
2
Author
HDOJ
代码:
#include int a,b; while(scanf(\"%d %d\",&a,&b)!=EOF) printf(\"%d\\n\",a+b); } 1001 Sum Problem 学习指导参考 WORD格式整理版 Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input The input will consist of a series of integers n, one integer per line. Output For 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 Input 1 100 Sample Output 1 5050 Author DOOM III 解答: #include int n,i,sum; sum=0; while((scanf(\"%d\",&n)!=-1)) { sum=0; for(i=0;i<=n;i++) 学习指导参考 WORD格式整理版 sum+=i; printf(\"%d\\n\\n\",sum); } } 1002 A + B Problem II Problem Description I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000. Output For each test case, you should output two lines. The first line is \"Case #:\# means the number of the test case. The second line is the an equation \"A + B = Sum\means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases. Sample Input 2 1 2 11223344556677 998877665544332211 Sample Output Case 1: 学习指导参考 WORD格式整理版 1 + 2 = 3 Case 2: 11223344556677 + 998877665544332211 = 1111111111111111110 Author Ignatius.L 代码: #include int main(){ char str1[1001], str2[1001]; int t, i, len_str1, len_str2, len_max, num = 1, k; scanf(\"%d\", &t); getchar(); while(t--){ int a[1001] = {0}, b[1001] = {0}, c[1001] = {0}; scanf(\"%s\", str1); len_str1 = strlen(str1); for(i = 0; i <= len_str1 - 1; ++i) a[i] = str1[len_str1 - 1 - i] - '0'; scanf(\"%s\",str2); len_str2 = strlen(str2); for(i = 0; i <= len_str2 - 1; ++i) b[i] = str2[len_str2 - 1 - i] - '0'; if(len_str1 > len_str2) len_max = len_str1; else len_max = len_str2; k = 0; for(i = 0; i <= len_max - 1; ++i){ c[i] = (a[i] + b[i] + k) % 10; k = (a[i] + b[i] + k) / 10; } if(k != 0) c[len_max] = 1; printf(\"Case %d:\\n\", num); num++; printf(\"%s + %s = \", str1, str2); if(c[len_max] == 1) 学习指导参考 WORD格式整理版 printf(\"1\"); for(i = len_max - 1; i >= 0; --i){ printf(\"%d\", c[i]); } printf(\"\\n\"); if(t >= 1) printf(\"\\n\"); } return 0; } 1005 Number Sequence Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed. Output For each test case, print the value of f(n) on a single line. Sample Input 1 1 3 1 2 10 0 0 0 Sample Output 2 5 学习指导参考 WORD格式整理版 Author CHEN, Shunbao Source ZJCPC2004 Recommend JGShining 代码: #include int a,b,n,i; while(scanf(\"%d%d%d\",&a,&b,&n)&&a&&b&&n) { if(n>=3) { f[1]=1;f[2]=1; for(i=3;i<=200;i++) { f[i]=(a*f[i-1]+b*f[i-2])%7; if(f[i-1]==1&&f[i]==1) break; } i-=2; n=n%i; if(n==0) printf(\"%d\\n\",f[i]); else printf(\"%d\\n\",f[n]); } else printf(\"1\\n\"); } return 0; } 学习指导参考 WORD格式整理版 1008 Elevator Problem Description The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop. For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled. Input There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed. Output Print the total time on a single line for each test case. Sample Input 1 2 3 2 3 1 0 Sample Output 17 41 Author ZHENG, Jianqiang Source ZJCPC2004 Recommend 学习指导参考 WORD格式整理版 JGShining 代码: #include int sum,i,n; while(scanf(\"%d\",&n)&&n!=0) { for(i=1;i<=n;i++) scanf(\"%d\",&a[i]); sum=0; a[0]=0; for(i=1;i<=n;i++) { if(a[i]>a[i-1]) sum+=6*(a[i]-a[i-1]); else sum+=4*(a[i-1]-a[i]); sum+=5; } printf(\"%d\\n\",sum); } return 0; } 1009 FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain. Input The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed 学习指导参考 WORD格式整理版 by two -1's. All integers are not greater than 1000. Output For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain. Sample Input 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 Author CHEN, Yue Source ZJCPC2004 Recommend JGShining 代码: #include int i,j,m,n,temp; int J[MAX],F[MAX]; double P[MAX]; double sum,temp1; scanf(\"%d%d\ while(m!=-1&&n!=-1) { sum=0; memset(J,0,MAX*sizeof(int)); 学习指导参考 WORD格式整理版 memset(F,0,MAX*sizeof(int)); memset(P,0,MAX*sizeof(double)); for(i=0;i temp1=P[i]; P[i]=P[j]; P[j]=temp1; temp=J[i]; J[i]=J[j]; J[j]=temp; temp=F[i]; F[i]=F[j]; F[j]=temp; } } } for(i=0;i return 0; } 1021 Fibonacci Again Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). Input Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000). Output Print the word \"yes\" if 3 divide evenly into F(n). Print the word \"no\" if not. 学习指导参考 WORD格式整理版 Sample Input 0 1 2 3 4 5 Sample Output no no yes no no no Author Leojay Recommend JGShining #include long n; while(scanf(\"%ld\",&n) != EOF) if (n%8==2 || n%8==6) printf(\"yes\\n\"); else printf(\"no\\n\"); return 0; } 学习指导参考 WORD格式整理版 10 A+B for Input-Output Practice (I) Problem Description Your 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. Input The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. Output For 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 Input 1 5 10 20 Sample Output 6 30 Author lcy Recommend JGShining 解答: 学习指导参考 WORD格式整理版 #include int a,b; while(scanf(\"%d%d\",&a,&b)!=EOF) printf(\"%d\\n\",a+b); } 1090 A+B for Input-Output Practice (II) Problem Description Your task is to Calculate a + b. Input Input 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. Output For 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 Input 2 1 5 10 20 Sample Output 6 30 Author lcy 学习指导参考 WORD格式整理版 Recommend JGShining 解答: #include int a ,b,n,j[M],i; //printf(\"please input n:\\n\"); scanf(\"%d\",&n); for(i=0;i i=0; while(i printf(\"\\n\"); } } 1091 A+B for Input-Output Practice (III) Problem Description Your task is to Calculate a + b. Input 学习指导参考 WORD格式整理版 Input 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. Output For 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 Input 1 5 10 20 0 0 Sample Output 6 30 Author lcy Recommend JGShining 解答: #include int a,b; scanf(\"%d %d\",&a,&b); while(!(a==0&&b==0)) { printf(\"%d\\n\",a+b); scanf(\"%d %d\",&a,&b); } } 学习指导参考 WORD格式整理版 1092 A+B for Input-Output Practice (IV) Problem Description Your task is to Calculate the sum of some integers. Input Input 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. Output For 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 Input 4 1 2 3 4 5 1 2 3 4 5 0 Sample Output 10 15 Author lcy Recommend JGShining 解答: 学习指导参考 WORD格式整理版 #include int n,sum,i,t; while(scanf(\"%d\",&n)!=EOF&&n!=0) { sum=0; for(i=0;i printf(\"%d\\n\",sum); } } 1093 A+B for Input-Output Practice (V) Problem Description Your task is to calculate the sum of some integers. Input Input 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. Output For 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 Input 2 4 1 2 3 4 5 1 2 3 4 5 Sample Output 学习指导参考 WORD格式整理版 10 15 Author lcy 解答: #include int n,a,b,i,j,sum; sum=0; while(scanf(\"%d\\n\",&n)!=-1) { for(i=0;i printf(\"%d\\n\",sum); sum=0; } } } 1094 A+B for Input-Output Practice (VI) Problem Description Your task is to calculate the sum of some integers. Input 学习指导参考 WORD格式整理版 Input 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. Output For 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 Input 4 1 2 3 4 5 1 2 3 4 5 Sample Output 10 15 Author lcy Recommend JGShining 解答: #include int n,a,b,i,j,sum; sum=0; while(scanf(\"%d\\n\",&n)!=-1) { for(j=0;j printf(\"%d\\n\",sum); sum=0; } 学习指导参考 WORD格式整理版 } [ Copy to Clipboard ] [ Save to File] 1095 A+B for Input-Output Practice (VII) Problem Description Your task is to Calculate a + b. Input The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. Output For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line. Sample Input 1 5 10 20 Sample Output 6 30 Author lcy Recommend JGShining 解答: #include 学习指导参考 WORD格式整理版 main() { int a,b; while(scanf(\"%d%d\",&a,&b)!=EOF) printf(\"%d\\n\\n\",a+b); } 1096 A+B for Input-Output Practice (VIII) Problem Description Your task is to calculate the sum of some integers. Input Input 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. Output For 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 Input 3 4 1 2 3 4 5 1 2 3 4 5 3 1 2 3 Sample Output 10 15 6 Author lcy 学习指导参考 WORD格式整理版 Recommend JGShining 解答: int main() { int a,b,i,j,l[1000],k; scanf(\"%d\",&i); getchar(); for(j=1;j<=i;j++) l[j]=0; for(j=1;j<=i;j++) { scanf(\"%d\",&a); getchar(); for(k=1;k<=a;k++) { scanf(\"%d\",&b); getchar(); l[j]+=b; } } for(j=1;j<=i-1;j++) printf(\"%d\\n\\n\",l[j]); printf(\"%d\\n\",l[i]); } 1176 免费馅饼 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标: 学习指导参考 WORD格式整理版 为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼) Input 输入数据有多组。每组数据的第一行为以正整数n(0 每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。 提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。 Sample Input 6 5 1 4 1 6 1 7 2 7 2 8 3 0 Sample Output 4 Author lwg 代码: #include int Max(int n1,int n2,int n3) { int max; max=(n1>n2)?n1:n2; max=(max>n3)?max:n3; return max; } void res(int num) { int i,j; int n,m,count=-1; memset(arr,0,MAX*13*sizeof(int)); for(i=0;i 学习指导参考 WORD格式整理版 arr[m][n+1]++; if(count arr[i][j]+=Max(arr[i+1][j-1],arr[i+1][j],arr[i+1][j+1]); printf(\"%d\\n\} int main() { int num; scanf(\"%d\ while(num) { res(num); scanf(\"%d\ } return 0; } 1204 糖果大战 Problem Description 生日Party结束的那天晚上,剩下了一些糖果,Gandon想把所有的都统统拿走,Speakless于是说:“可以是可以,不过我们来玩24点,你不是已经拿到了一些糖果了吗?这样,如果谁赢一局,就拿走对方一颗糖,直到拿完对方所有的糖为止。”如果谁能算出来而对方算不出来,谁就赢,但是如果双方都能算出或者都不能,就算平局,不会有任何糖果的得失。 Speakless是个喜欢提前想问题的人,既然他发起了这场糖果大战,就自然很想赢啦(不然可就要精光了-_-)。现在他需要你的帮忙,给你他每局赢的概率和Gardon每局赢的概率,请你给出他可能获得这场大战胜利的概率。 Input 每行有四个数,Speakless手上的糖果数N、Gardon手上的糖果数M(0<=N,M<=50)、一局Speakless能解答出来的概率p、一个问题Gardon能解答出来的概率q(0<=p,q<=1)。 Output 每行一个数,表示Speakless能赢的概率(用百分比计算,保留到小数点后2位)。 Sample Input 50 50 0.5 0.5 10 10 0.51 0.5 50 50 0.51 0.5 学习指导参考 WORD格式整理版 Sample Output 0.50 0.60 0.88 Author Speakless Source Gardon-DYGG Contest 2 Recommend JGShining 代码: #include const double EPS = 1e-12; inline void solve(int n, int m, double p, double q) { if(n==0) printf(\"0.00\\n\"); else if(m==0) printf(\"1.00\\n\"); else if(p==0.0||q==1.0) printf(\"0.00\\n\"); else { double lamda = q*(1-p)/(p*(1-q)); if(fabs(lamda-1.0) double res = (1-pow(lamda, n))/(1-pow(lamda, m+n)); printf(\"%.2lf\\n\ } } } int main() { int n, m; double p, q; while(scanf(\"%d%d%lf%lf\ solve(n, m, p, q); } return 0; } 学习指导参考 WORD格式整理版 1213 How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers. One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table. For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least. Input The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases. Output For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks. Sample Input 2 5 3 1 2 2 3 4 5 5 1 2 5 Sample Output 2 4 Author Ignatius.L Source 杭电ACM省赛集训队选拔赛之热身赛 Recommend Eddy 学习指导参考 WORD格式整理版 代码:#include int start[MAX],end[MAX]; int res; int arr[MAX]; int len; int Mempty() { int i; for(i=0;i int inSet(int index) { int i; for(i=0;i void deal() { int i; int space=0; for(i=0;i else { start[i-space]=start[i]; end[i-space]=end[i]; } } m=m-space; } void proc() { int i; int j; while(!Mempty()) { i=0; while(i if(start[i]!=end[i]) { arr[res++]=start[i]; 学习指导参考 WORD格式整理版 arr[res++]=end[i]; } else arr[res++]=start[i]; start[i]=end[i]=0; } else { for(j=0;j if(!inSet(end[i])) arr[res++]=end[i]; start[i]=end[i]=0; i=-1; break; } if(arr[j]==end[i]) { if(!inSet(start[i])) arr[res++]=start[i]; start[i]=end[i]=0; i=-1; break; } } } i++; } len++; deal(); } if(res==n) len--; else len+=n-res-1; } int main() { int i; int num; scanf(\"%d\ while(num--) { scanf(\"%d%d\ for(i=0;i 学习指导参考 WORD格式整理版 } 1231 最大连续子序列 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个, 例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和 为20。 在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 子序列的第一个和最后一个元素。 Input 测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。 Output 对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。 Sample Input 6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0 Sample Output 20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0 Hint Hint Huge input, scanf is recommended. Source 浙大计算机研究生复试上机考试-2005年 Recommend JGShining 代码: #include 学习指导参考 WORD格式整理版 int arr[MAX]; int main() { int num,temp,i,flage; int sum,start,end,max=-32768; scanf(\"%d\ while(num!=0) { memset(arr,0,MAX*sizeof(int)); for(i=0;i for(i=0;i if(max return 0; } 1232 畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路? Input 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 注意:两个城市之间可以有多条道路相通,也就是说 3 3 学习指导参考 WORD格式整理版 1 2 1 2 2 1 这种输入也是合法的 当N为0时,输入结束,该用例不被处理。 Output 对每个测试用例,在1行里输出最少还需要建设的道路数目。 Sample Input 4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0 Sample Output 1 0 2 998 Hint Hint Huge input, scanf is recommended. Source 浙大计算机研究生复试上机考试-2005年 Recommend JGShining 代码:#include #include int arr[MAX][2]; int res[MAX]; int set; void proc() { int i,j; int rest; set=1; //用来统计集合个数 memset(res,0,(n+1)*sizeof(int)); res[arr[0][0]]=res[arr[0][1]]=1; for(i=1;i<=set;i++) { 学习指导参考 WORD格式整理版 for(j=0;j if(res[arr[j][0]]==i||res[arr[j][1]]==i) { res[arr[j][0]]=res[arr[j][1]]=i; //对数据进行集合划分 j=-1; //从第一组元素开始继续遍历 } } for(j=0;j if(j rest=0; for(i=1;i<=n;i++) if(res[i]==0) rest++; if(m==0) printf(\"%d\\n\ else if(rest==0) { if(set==1) printf(\"0\\n\"); else printf(\"%d\\n\ } else printf(\"%d\\n\} int main() { int i; scanf(\"%d\ while(n) { scanf(\"%d\ for(i=0;i 学习指导参考 WORD格式整理版 return 0; } 2000 ASCII码排序 Problem Description 输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。 Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格。 Output 对于每组输入数据,输出一行,字符中间用一个空格分开。 Sample Input qwe asd zxc Sample Output e q w a d s c x z Author lcy Source C语言程序设计练习(一) Recommend JGShining 解答: #include 学习指导参考 WORD格式整理版 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 %c\\n\",b,a,c); else if(b>=c) printf(\"%c %c %c\\n\",c,b,a); else if(b else { if(c>=b) printf(\"%c %c %c\\n\",a,b,c); else if(c>=a) printf(\"%c %c %c\\n\",a,c,b); else if(a>c) printf(\"%c %c %c\\n\",c,a,b); } } } 2001 计算两点间的距离 Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。 Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。 Output 对于每组输入数据,输出一行,结果保留两位小数。 学习指导参考 WORD格式整理版 Sample Input 0 0 0 1 0 1 1 0 Sample Output 1.00 1.41 Author lcy Source C语言程序设计练习(一) Recommend JGShining 解答: #include 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(\"%.2lf\\n\",s); } } 2002 计算球体积 Problem Description 根据输入的半径值,计算球的体积。 学习指导参考 WORD格式整理版 Input 输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。 Output 输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。 Sample Input 1 1.5 Sample Output 4.1 14.137 Hint #define PI 3.1415927 Author lcy Source C语言程序设计练习(一) Recommend JGShining 解答: #include double a,v; while(scanf(\"%lf\",&a)!=EOF) { v=4*PI*a*a*a/3; printf(\"%.3lf\\n\",v); } } 学习指导参考 WORD格式整理版 2003 求绝对值 Problem Description 求实数的绝对值。 Input 输入数据有多组,每组占一行,每行包含一个实数。 Output 对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。 Sample Input 123 -234.00 Sample Output 123.00 234.00 Author lcy Source C语言程序设计练习(一) Recommend JGShining 解答: #include double a; while(scanf(\"%lf\",&a)!=EOF) { 学习指导参考 WORD格式整理版 if(a<0) a=-a; printf(\"%.2lf\\n\",a); } } 2004 成绩转换 Problem Description 输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下: 90~100为A; 80~为B; 70~79为C; 60~69为D; 0~59为E; Input 输入数据有多组,每组占一行,由一个整数组成。 Output 对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“Score error!”。 Sample Input 56 67 100 123 Sample Output E D A Score is error! Author lcy 学习指导参考 is WORD格式整理版 Source C语言程序设计练习(一) Recommend JGShining 解答: #include int n; while(scanf(\"%d\",&n)!=EOF) { if(n>100||n<0)printf(\"Score is error!\\n\"); else if(n>=90)printf(\"A\\n\"); else if(n>=80)printf(\"B\\n\"); else if(n>=70)printf(\"C\\n\"); else if(n>=60)printf(\"D\\n\"); else printf(\"E\\n\"); } return 0; } 2005 第几天? Problem Description 给定一个日期,输出这个日期是该年的第几天。 Input 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。 Output 对于每组输入数据,输出一行,表示该日期是该年的第几天。 Sample Input 1985/1/20 2006/3/12 学习指导参考 WORD格式整理版 Sample Output 20 71 Author lcy Source C语言程序设计练习(一) Recommend JGShining 解答: #include 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; 学习指导参考 WORD格式整理版 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(\"%d\\n\",d); } } 2006 求奇数的乘积 Problem Description 给你n个整数,求他们中所有奇数的乘积。 Input 输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。 Output 输出每组数中的所有奇数的乘积,对于测试实例,输出一行。 Sample Input 3 1 2 3 4 2 3 4 5 学习指导参考 WORD格式整理版 Sample Output 3 15 Author lcy Source C语言程序设计练习(一) Recommend JGShining 解答: #include int n,s,i,a; while(scanf(\"%d\",&n)!=EOF) { s=1; for(i=0;i printf(\"%d\\n\",s); } } 2007 平方和与立方和 Problem Description 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。 学习指导参考 WORD格式整理版 Input 输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。 Output 对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。 你可以认为32位整数足以保存结果。 Sample Input 1 3 2 5 Sample Output 4 28 20 152 Author lcy Source C语言程序设计练习(一) Recommend JGShining 解答: #include 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); 学习指导参考 WORD格式整理版 else sum2+=(i*i*i); } printf(\"%d %d\\n\",sum1,sum2); } return 0; } 2008 数值统计 Problem Description 统计给定的n个数中,负数、零和正数的个数。 Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。 Output 对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。 Sample Input 6 0 1 2 3 -1 0 5 1 2 3 4 0.5 0 Sample Output 1 2 3 0 0 5 Author lcy Source C语言程序设计练习(二) Recommend JGShining 学习指导参考 WORD格式整理版 解答: #include int main() { int n,i,b1,b2,b3; double a[101]; while(scanf(\"%d\",&n)!=EOF && n!=0) { for(i=0;i else if(a[i]==0) b2++; else b3++; } printf(\"%d %d %d\\n\",b1,b2,b3); } } 2009 求数列的和 Problem Description 数列的定义如下: 数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。 Input 输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。 Output 对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。 Sample Input 81 4 2 2 学习指导参考 WORD格式整理版 Sample Output 94.73 3.41 Author lcy Source C语言程序设计练习(二) Recommend JGShining 解答: #include double n,m,s,w,i; while(scanf(\"%lf%lf\",&n,&m)!=EOF) { s=n; for(i=1;i printf(\"%.2lf\\n\",s); } } 2010 水仙花数 Problem Description 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。 现在要求输出所有在m和n范围内的水仙花数。 学习指导参考 WORD格式整理版 Input 输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。 Output 对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开; 如果给定的范围内不存在水仙花数,则输出no; 每个测试实例的输出占一行。 Sample Input 100 120 300 380 Sample Output no 370 371 Author lcy Source C语言程序设计练习(二) Recommend JGShining 解答: #include int m,n,i,w,a,b,c,j,s,d; while(scanf(\"%d %d\",&n,&m)!=EOF) { d=0; j=1; if(m>n) 学习指导参考 WORD格式整理版 { w=m; m=n; n=w; } else ; for(i=m;i<=n;i++) { a=i/100; b=i/10%10; c=i%10; s=a*a*a+b*b*b+c*c*c; if(i==s) { if(d!=0) printf(\" \"); printf(\"%d\",i); d=d+1; j=j+1; } } if(j==1) printf(\"no\\n\"); else printf(\"\\n\"); } } 2011 多项式求和 Problem Description 多项式的描述如下: 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ... 现在请你求出该多项式的前n项的和。 Input 输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。 学习指导参考 WORD格式整理版 Output 对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。 Sample Input 2 1 2 Sample Output 1.00 0.50 Author lcy Source C语言程序设计练习(二) Recommend JGShining 解答: #include double m,n,i,s,j,k,a; while(scanf(\"%lf\",&m)!=EOF) { for(i=0;i scanf(\"%lf\",&n); for(j=1;j<=n;j++) s=s+1/j*pow(-1,j+1); 学习指导参考 WORD格式整理版 printf(\"%.2lf\\n\",s); } } } 2012 素数判定 Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x 输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。 Output 对于每个给定范围内的取值,如果表达式的值都为素数,则输出\"OK\否则请输出“Sorry”,每组输出占一行。 Sample Input 0 1 0 0 Sample Output OK Author lcy Source C语言程序设计练习(二) Recommend JGShining 学习指导参考 WORD格式整理版 解答: #include int x,y,i,j,s,k,w,d; while(scanf(\"%d%d\",&x,&y)==2&&(x!=0||y!=0)) { w=0; for(i=x;i<=y;i++) { k=i*i+i+41; for(j=2;j if(w==0) printf(\"OK\\n\"); else printf(\"Sorry\\n\"); } } 2014 青年歌手大奖赛_评委会打分 Problem Description 青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。 Input 输入数据有多组,每组占一行,每行的第一个数是n(2 WORD格式整理版 Output 对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。 Sample Input 3 99 98 97 4 100 99 98 97 Sample Output 98.00 98.50 Author lcy Source C语言程序设计练习(三) Recommend lcy 解答: #include int n,s,a[100],i,k,b; double w; while(scanf(\"%d\",&n)!=EOF) { k=0; w=0; s=0; for(i=0;i 学习指导参考 WORD格式整理版 for(i=0;i for(i=1;i w=(w-s-b)/(k-2); printf(\"%.2lf\\n\",w); } } 2015 偶数求和 Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。 Input 输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。 Output 对于每组输入数据,输出一个平均值序列,每组输出占一行。 Sample Input 3 2 4 2 Sample Output 3 6 3 7 Author lcy 学习指导参考 WORD格式整理版 Source C语言程序设计练习(三) Recommend lcy 解答: #include int n,m,a,b,i,j,k,w,l,e,s,d,r; while(scanf(\"%d%d\",&n,&m)!=EOF) { s=0; e=0; l=0; if(n<=m) { for(i=0;i printf(\"%d\\n\",k); } else { w=n%m; r=0; for(i=1;i<=n-w;i++) { s=s+2; l=l+s; e=e+s; if(i%m==0) { k=e/m; e=0; if(r) printf(\" \"); 学习指导参考 WORD格式整理版 printf(\"%d\",k); r=r+1; } } s=0; if(w!=0) { for(j=0;j } d=e-l; k=d/w; printf(\" \"); printf(\"%d\",k); } printf(\"\\n\"); } } } 2016 数据的交换输出 Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。 Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。 Output 对于每组输入数据,输出交换后的数列,每组输出占一行。 Sample Input 4 2 1 3 4 学习指导参考 WORD格式整理版 5 5 4 3 2 1 0 Sample Output 1 2 3 4 1 4 3 2 5 Author lcy Source C语言程序设计练习(三) Recommend lcy 解答: #include int n,a[100],i,j,k,s,w; while(scanf(\"%d\",&n)!=EOF&&n!=0) { j=0; for(i=0;i for(i=0;i k=a[i]; j=i; } } w=a[0]; 学习指导参考 WORD格式整理版 a[0]=k; a[j]=w; for(i=0;i printf(\"\\n\"); } } 2017 字符串统计 Problem Description 对于给定的一个字符串,统计其中数字字符出现的次数。 Input 输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。 Output 对于每个测试实例,输出该串中数值的个数,每个输出占一行。 Sample Input 2 asdfasdf123123asdfasdf asdf111111111asdfasdfasdf Sample Output 6 9 Author lcy 解答: 学习指导参考 WORD格式整理版 #include int n,i,j,a; char s[1000]; while(scanf(\"%d\",&n)!=EOF) { getchar(); for(i=0;i for(j=0;s[j]!='\\0';j++) { if((s[j]<='9')&&(s[j]>='0')) a=a+1; } printf(\"%d\\n\",a); } } } 2019 数列有序! Problem Description 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。 Input 输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。 Output 对于每个测试实例,输出插入新的元素后的数列。 Sample Input 3 3 1 2 4 0 0 学习指导参考 WORD格式整理版 Sample Output 1 2 3 4 Author lcy Source C语言程序设计练习(三) Recommend lcy 解答: #include int n,m,a[100],b[100],i,j,k,s,w,d; scanf(\"%d%d\",&n,&m); while(!(n==0&&m==0)) { w=0; for(i=0;i printf(\"%d\",a[j]); } } else { for(j=0;j 学习指导参考 WORD格式整理版 } for(j=0;j printf(\"%d\",m); for(j=w;j printf(\"\\n\"); scanf(\"%d%d\",&n,&m); } } 2020 绝对值排序 Problem Description 输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。 Input 输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。 Output 对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。 Sample Input 3 3 -4 2 4 0 1 2 -3 0 Sample Output -4 3 2 学习指导参考 WORD格式整理版 -3 2 1 0 Author lcy Source C语言程序设计练习(三) Recommend lcy 解答: #include int n,m,a[100],b[100],c,d,e,f,i,j,k; while(scanf(\"%d\",&n)!=EOF&&n!=0) { for(i=0;i for(i=0;i else m=a[i]; if(c<=m) { c=m; b[j]=a[i]; k=i; } } a[k]=0; if(f) printf(\" \"); printf(\"%d\",b[j]); 学习指导参考 WORD格式整理版 f=f+1; } printf(\"\\n\"); } } 2021 发工资咯:) Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵 但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每个老师的工资额都知道,最少需要准备多少张人民币,才能在给每位老师发工资的时候都不用老师找零呢? 这里假设老师的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。 Input 输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资。 n=0表示输入的结束,不做处理。 Output 对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。 Sample Input 3 1 2 3 0 Sample Output 4 Author lcy Source 学习指导参考 WORD格式整理版 C语言程序设计练习(四) Recommend lcy 解答: #include int n,m,a,b,c,d,e,f,i,j,k; while(scanf(\"%d\",&n)!=EOF&&n!=0) { k=0; for(i=0;i printf(\"%d\\n\",k); } } 2033 人见人爱A+B Problem Description HDOJ上面已经有10来道A+B的题目了,相信这些题目曾经是大家的最爱,希望今天的这个A+B能给大家带来好运,也希望这个题目能唤起大家对ACM曾经的热爱。 这个题目的A和B不是简单的整数,而是两个时间,A和B 都是由3个整数组成,分别表示时分秒,比如,假设A为34 45 56,就表示A所表示的时间是34小时 45分钟 56秒。 Input 输入数据有多行组成,首先是一个整数N,表示测试实例的个数,然后是N行数据,每行有6个整数AH,AM,AS,BH,BM,BS,分别表示时间A和B所对应的时分秒。题目保证所有的数据合法。 学习指导参考 WORD格式整理版 Output 对于每个测试实例,输出A+B,每个输出结果也是由时分秒3部分组成,同时也要满足时间的规则(即:分和秒的取值范围在0~59),每个输出占一行,并且所有的部分都可以用32位整数表示。 Sample Input 2 1 2 3 4 5 6 34 45 56 12 23 34 Sample Output 5 7 9 47 9 30 Author lcy Source ACM程序设计期末考试(2006/06/07) Recommend lcy 解答: #include {int i,j,a,b,c,d,e,f,n,s; while(scanf(\"%d\",&n)!=EOF) { c=0;d=0;e=0; for(i=0;i scanf(\"%d%d%d%d%d%d\",&a,&b,&c,&d,&e,&f); a=a+d; b=b+e;c=c+f; j=0; while(c>60) { 学习指导参考 WORD格式整理版 c=c-60; j=j+1; } b=b+j; s=0; while(b>60) { b=b-60; s=s+1; } a=a+s; printf(\"%d %d %d\\n\",a,b,c); } } } 2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13407 Accepted Submission(s): 6933 Problem Description “今年暑假不AC?” “是的。” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。 作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目) Input 输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。 Output 对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。 学习指导参考 WORD格式整理版 Sample Input 12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0 Sample Output 5 Author lcy Source ACM程序设计期末考试(2006/06/07) Recommend lcy 代码: #include int i,j; int start; int count=0,max=-1; for(i=num-1;i>=num/2;i--) { if(count>max) max=count; count=1; start=arr[i][0]; for(j=i-1;j>=0;j--) { if(arr[j][1]<=start){ count++; start=arr[j][0]; } } } printf(\"%d\\n\} void prc() { int i,j; for(i=0;i WORD格式整理版 { if(arr[i][0]>arr[j][0]) { arr[i][0]+=arr[j][0]; arr[j][0]=arr[i][0]-arr[j][0]; arr[i][0]=arr[i][0]-arr[j][0]; arr[i][1]+=arr[j][1]; arr[j][1]=arr[i][1]-arr[j][1]; arr[i][1]=arr[i][1]-arr[j][1]; } if((arr[i][0]==arr[j][0])&&(arr[i][1]>arr[j][1])) { arr[i][1]+=arr[j][1]; arr[j][1]=arr[i][1]-arr[j][1]; arr[i][1]=arr[i][1]-arr[j][1]; } } res(); } int main() { scanf(\"%d\ while(num) { prc(); scanf(\"%d\ } return 0; } 2039 三角形 Problem Description 给定三条边,请你判断一下能不能组成一个三角形。 Input 输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C。其中A,B,C <1000; Output 对于每个测试实例,如果三条边长A,B,C能组成三角形的话,输出YES,否则NO。 学习指导参考 WORD格式整理版 Sample Input 2 1 2 3 2 2 2 Sample Output NO YES Author linle Source 2005实验班短学期考试 Recommend lcy 解答: #include double n,a,b,c,i; while(scanf(\"%lf\",&n)!=EOF) { for(i=0;i printf(\"NO\\n\"); } } } 2040 亲和数 学习指导参考 WORD格式整理版 Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为: 1+2+4+5+10+11+20+22+44+55+110=284。 而284的所有真约数为1、2、4、71、 142,加起来恰好为220。人们对这样的数感到很惊奇,并称之为亲和数。一般地讲,如果两个数中任何一个数都是另一个数的真约数之和,则这两个数就是亲和数。 你的任务就编写一个程序,判断给定的两个数是否是亲和数 Input 输入数据第一行包含一个数M,接下有M行,每行一个实例,包含两个整数A,B;A,B <= 600000 ; Output 对于每个测试实例,如果A和B是亲和数的话输出YES,否则输出NO。 Sample Input 2 220 284 100 200 Sample Output YES NO Author linle Source 2005实验班短学期考试 Recommend lcy 学习指导参考 其中 0 <= WORD格式整理版 解答: #include double n,a,b,c,i; while(scanf(\"%lf\",&n)!=EOF) { for(i=0;i printf(\"NO\\n\"); } } } 2045 不容易系列之(3)—— LELE的 RPG难题 Problem Description 人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即\"可乐\"),经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研究起了著名的RPG难题: 有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法. 以上就是著名的RPG难题. 如果你是Cole,我想你一定会想尽办法帮助LELE解决这个问题的;如果不是,看在众多漂亮的痛不欲生的Cole女的面子上,你也不会袖手旁观吧? Input 输入数据包含多个测试实例,每个测试实例占一行,由一个整数N组成,(0 对于每个测试实例,请输出全部的满足要求的涂法,每个实例的输出占一行。 Sample Input 1 2 学习指导参考 WORD格式整理版 Sample Output 3 6 Author lcy Source 递推求解专题练习(For Beginner) Recommend lcy 递推公式:f(n)=f(n-1)+2*f(n-2) 代码:#include int main() { __int i,arr[51]; __int num; arr[0]=0; arr[1]=3; arr[2]=6; arr[3]=6; for(i=4;i<51;i++) arr[i]=arr[i-1]+2*arr[i-2]; while(scanf(\"%d\ { printf(\"%Id\\n\ } return 0; } 2049 不容易系列之(4)——考新郎 Problem Description 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做\"考新郎\具体的操作是这样的: 学习指导参考 WORD格式整理版 首先,给每位新娘打扮得几乎一模一样,并盖上大大的红盖头随机坐成一排; 然后,让各位新郎寻找自己的新娘.每人只准找一个,并且不允许多人找一个. 最后,揭开盖头,如果找错了对象就要当众跪搓衣板... 看来做新郎也不是容易的事情... 假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能. Input 输入数据的第一行是一个整数C,表示测试实例的个数,然后是C行数据,每行包含两个整数N和M(1 对于每个测试实例,请输出一共有多少种发生这种情况的可能,每个实例的输出占一行。 Sample Input 2 2 2 3 2 Sample Output 1 3 Author lcy Source 递推求解专题练习(For Beginner) Recommend lcy 错排类递推公式为:f(n)=(m-1)*[f(n-1)+f(n-2)],其中m表示错排的个数(即错排的新人对数),n表示全部的新人个数。 代码: #include int main(void) 学习指导参考 WORD格式整理版 { int i, m, n; __int a[21][2] = {{1,0},{1,0},{2,1},{6,2}}; for (i = 4; i < 21; i++) { a[i][0] = i * a[i-1][0]; a[i][1] = (i-1) * (a[i-1][1] + a[i-2][1]); } scanf(\"%d\ while (i-- && scanf(\"%d%d\ printf(\"%Id\\n\ return 0; } 2056 Rectangles Problem Description Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY . Input Input The first line of input is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the two points on the first rectangle are(x1,y1),(x2,y2);the other two points on the second rectangle are (x3,y3),(x4,y4). Output Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places. Sample Input 1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50 Sample Output 1.00 56.25 Author seeyou Source 校庆杯Warm Up 学习指导参考 WORD格式整理版 Recommend linle 代码: #include void res(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { double tmpx1,tmpy1,tmpx2,tmpy2; double tmpx,tmpy; tmpx=x1+x2; x1=(x1>x2)?x2:x1; x2=tmpx-x1; tmpy=y1+y2; y1=(y1>y2)?y2:y1; y2=tmpy-y1; tmpx=x3+x4; x3=(x3>x4)?x4:x3; x4=tmpx-x3; tmpy=y3+y4; y3=(y3>y4)?y4:y3; y4=tmpy-y3; tmpx1=(x1>x3)?x1:x3; tmpx2=(x2>x4)?x4:x2; tmpy1=(y1>y3)?y1:y3; tmpy2=(y2>y4)?y4:y2; if(tmpx1 int main() { double x1,y1,x2,y2; double x3,y3,x4,y4; while(scanf(\"%lf%lf%lf%lf%lf%lf%lf%lf\ { res(x1,y1,x2,y2,x3,y3,x4,y4); } return 0; } 2073 无限的路 Problem Description 甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画 直线,于是他就在平面直角坐标系中画出如下的图形: 学习指导参考 WORD格式整理版 甜甜的好朋友蜜蜜发现上面的图还是有点规则的,于是他问甜甜:在你画的图中,我给你两 个点,请你算一算连接两点的折线长度(即沿折线走的路线长度)吧。 Input 第一个数是正整数N(≤100)。代表数据的组数。 每组数据由四个非负整数组成x1,y1,x2,y2;所有的数都不会大于100。 Output 对于每组数据,输出两点(x1,y1),(x2,y2)之间的折线距离。注意输出结果精确到小数点后3位。 Sample Input 5 0 0 0 1 0 0 1 0 2 3 3 1 99 99 9 9 5 5 5 5 Sample Output 1.000 2.414 10.6 54985.047 0.000 Author Lily Source 浙江工业大学网络选拔赛 Recommend 学习指导参考 WORD格式整理版 linle 代码: #include int n, x[3], y[3]; double s; scanf(\"%d\ while (n-- && scanf(\"%d%d%d%d\ { if ((x[2] = x[0]+y[0]) > (y[2] = x[1]+y[1])) { swap(x[0], x[1]); swap(y[0], y[1]); swap(x[2], y[2]); } if (x[2] == y[2]) printf(\"%.3f\\n\ else { s = sqrt((double)2.0)*(x[2] + x[1] - x[0] + y[2]*(y[2]-1)/2.0 - x[2]*(x[2]+1)/2.0); for (; x[2] < y[2]; x[2]++) s += sqrt((double)2*x[2]*x[2]+2*x[2]+1); printf(\"%.3f\\n\ } } return 0; } 2084 数塔 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 学习指导参考 WORD格式整理版 已经告诉你了,这是个DP的题目,你能AC吗? Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。 Output 对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。 Sample Input 1 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 Sample Output 30 Source 2006/1/15 ACM程序设计期末考试 Recommend lcy 代码:#include #include int arr[MAX][MAX][2]; void res() 学习指导参考 WORD格式整理版 { int n; int i,j; memset(arr,0,MAX*MAX*sizeof(int)); scanf(\"%d\ for(i=0;i for(j=0;j<=i;j++) { if(arr[i+1][j][1]>arr[i+1][j+1][1]) arr[i][j][1]+=arr[i+1][j][1]; else arr[i][j][1]+=arr[i+1][j+1][1]; } } printf(\"%d\\n\} int main() { int num; scanf(\"%d\ while(num--) { res(); } return 0; } 2201 熊猫阿波的故事 Problem Description 凡看过功夫熊猫这部电影的人都会对影片中那只憨憨的熊猫阿波留下相当深的印象,胖胖的熊猫阿波自从打败了凶狠强悍的雪豹泰龙以后,在和平谷的地位是越来越高,成为谷中第一的功夫大师。并因此他父亲经营的面馆的生意也越来越好,店里每天都会有许多慕名而来吃面和想拜阿波为师的人。 一日,阿波收到了一张请柬,请柬里说在遥远的美国将召开全球比武大会,特邀请阿波过去做嘉宾。阿波当然很高兴,因为自己长这么大都还没出过和平谷,更何况是出国去那遥远的美国。于是他托人买了当晚的机票,阿波来到机场发现其他乘客们正准备按机票上的号码(1,2,3,.....,n)依次排队上飞机,由于阿波是第一次坐飞机,所以他想先一步登机,因此他插队第一个登上了飞机,并且他也不看机票,随机的选择了一个座位坐下了。乘客们都很气氛,他们想:既然阿波都不遵守规定,那么我为什么要遵守呢?因此后面所有的人也都随意地找了位置坐下来,并且坚决不让座给其他的乘客。 现在的问题是这样的:在这样的情况下,第i个乘客(除去熊猫阿波外)坐到原机票位置的概率是多少? 学习指导参考 WORD格式整理版 Input 输入包含多组测试数据,每组数据占一行,包含两个整数,分别是n和m(n>=m),n表示共有n个乘客(包括阿波),m表示第m个乘客。 Output 对于每组数据,请输出第m个乘客(除去熊猫阿波外)坐到原机票位置的概率是多少?(结果保留2位小数) 每组输出占一行。 Sample Input 2 1 11 3 Sample Output 0.50 0.09 Author Eddy Recommend lcy 代码: #include int main() { double n, m; while( cin >> n >> m) { double k = 1/ n; printf(\"%.2f\ cout << endl; } return 0; } 2212 DFS Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit of a positive integer. 学习指导参考 WORD格式整理版 For example ,consider the positive integer 145 = 1!+4!+5!, so it's a DFS number. Now you should find out all the DFS numbers in the range of int( [1, 21474837] ). There is no input for this problem. Output all the DFS numbers in increasing order. The first 2 lines of the output are shown below. Input no input Output Output all the DFS number in increasing order. Sample Output 1 2 ...... Author zjt Recommend Lcy 代码: #include printf(\"1\\n2\\n145\\n40585\\n\"); } 2304 Electrical Outlets Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating back to the days before people had electricity in their houses. Because of this, Roy's apartment has only one single wall outlet, so Roy can only power one of his electrical appliances at a time. Roy likes to watch TV as he works on his computer, and to listen to his HiFi system (on high volume) while he vacuums, so using just the single outlet is not an option. Actually, he wants to have all his appliances connected to a powered outlet, all the time. The answer, of course, is power strips, and Roy has some old ones that he used in his old apartment. However, that apartment had many more wall outlets, so he is not sure whether his power strips will provide him with enough outlets now. 学习指导参考 WORD格式整理版 Your task is to help Roy compute how many appliances he can provide with electricity, given a set of power strips. Note that without any power strips, Roy can power one single appliance through the wall outlet. Also, remember that a power strip has to be powered itself to be of any use. Input Input will start with a single integer 1 <= N <= 20, indicating the number of test cases to follow. Then follow N lines, each describing a test case. Each test case starts with an integer 1 <= K <= 10, indicating the number of power strips in the test case. Then follow, on the same line, K integers separated by single spaces, O1 O2 . . . OK, where 2 <= Oi <= 10, indicating the number of outlets in each power strip. Output Output one line per test case, with the maximum number of appliances that can be powered. Sample Input 3 3 2 3 4 10 4 4 4 4 4 4 4 4 4 4 4 10 10 10 10 Sample Output 7 31 37 Source NCPC2005 Recommend zty 代码: #include int sum,num,t,n,i; scanf(\"%d\ while(t--) { scanf(\"%d\ sum=0; for(i=0;i 学习指导参考 WORD格式整理版 } printf(\"%d\\n\ } return 0; } 2309 ICPC Score Totalizer Software Problem Description The International Clown and Pierrot Competition (ICPC), is one of the most distinguished and also the most popular events on earth in the show business. One of the unique features of this contest is the great number of judges that sometimes counts up to one hundred. The number of judges may differ from one contestant to another, because judges with any relationship whatsoever with a specific contestant are temporarily excluded for scoring his/her performance. Basically, scores given to a contestant's performance by the judges are averaged to decide his/her score. To avoid letting judges with eccentric viewpoints too much influence the score, the highest and the lowest scores are set aside in this calculation. If the same highest score is marked by two or more judges, only one of them is ignored. The same is with the lowest score. The average, which may contain fractions, are truncated down to obtain final score as an integer. You are asked to write a program that computes the scores of performances, given the scores of all the judges, to speed up the event to be suited for a TV program. Input The input consists of a number of datasets, each corresponding to a contestant's performance. There are no more than 20 datasets in the input. A dataset begins with a line with an integer n, the number of judges participated in scoring the performance (3 ≤ n ≤ 100). Each of the n lines following it has an integral score s (0 ≤ s ≤ 1000) marked by a judge. No other characters except for digits to express these numbers are in the input. Judges' names are kept secret. The end of the input is indicated by a line with a single zero in it. Output For each dataset, a line containing a single decimal integer indicating the score for the corresponding performance should be output. No other characters should be on the output line. 学习指导参考 WORD格式整理版 Sample Input 3 1000 342 0 5 2 2 9 11 932 5 300 1000 0 200 400 8 353 242 402 274 283 132 402 523 0 Sample Output 342 7 300 326 Source Japan 2007 Domestic Recommend teddy 代码: #include int n,i,s,x,y,a; while(scanf(\"%d\ { x=0,y=1000; for(i=1,s=0;i<=n;i++) { scanf(\"%d\ s+=a; if(a>x) x=a; if(a 2317 Nasty Hacks Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of malicious software which teenagers may use to fool their friends. The company has just finished their first product and it is 学习指导参考 WORD格式整理版 time to sell it. You want to make as much money as possible and consider advertising in order to increase sales. You get an analyst to predict the expected revenue, both with and without advertising. You now want to make a decision as to whether you should advertise or not, given the expected revenues. Input The input consists of n cases, and the first line consists of one positive integer giving n. The next n lines each contain 3 integers, r, e and c. The first, r, is the expected revenue if you do not advertise, the second, e, is the expected revenue if you do advertise, and the third, c, is the cost of advertising. You can assume 666 that the input will follow these restrictions: -10 ≤ r, e ≤ 10 and 0 ≤ c ≤ 10. Output Output one line for each test case: “advertise”, “do not advertise” or “does not matter”, presenting whether it is most profitable to advertise or not, or whether it does not make any difference. Sample Input 3 0 100 70 100 130 30 -100 -70 40 Sample Output advertise does not matter do not advertise Source Nordic 2006 Recommend zty 代码: #include int i; long a[3],k; scanf(\"%d\ while(i--) { scanf(\"%ld%ld%ld\ k=a[1]-a[2]; if(a[0] 学习指导参考 WORD格式整理版 } } else if(a[0]==k) printf(\"does not matter\\n\"); else printf(\"do not advertise\\n\"); 2401 Baskets of Gold Coins Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes 1 coin from Basket 1, 2 coins from Basket 2, and so on, up to and including N-1 coins from Basket N-1. He does not take any coins from Basket N. He weighs the selected coins and concludes which of the N baskets contains the lighter coins. Your mission is to emulate the wizard's computation. Input The input file will consist of one or more lines; each line will contain data for one instance of the problem. More specifically, each line will contain four positive integers, separated by one blank space. The first three integers are, respectively, the numbers N, w, and d, as described above. The fourth integer is the result of weighing the selected coins. N will be at least 2 and not more than 8000. The value of w will be at most 30. The value of d will be less than w. Output For each instance of the problem, your program will produce one line of output, consisting of one positive integer: the number of the basket that contains lighter coins than the other baskets. Sample Input 10 25 8 1109 10 25 8 1045 8000 30 12 959879400 Sample Output 2 10 50 Source ACM/ICPC 2008 Warmup(2)——测试帐号(杭州) 学习指导参考 WORD格式整理版 Recommend lcy 代码: #include int a,b,c,d,sum; while(scanf(\"%d%d%d%d\ { sum=a*(a-1)/2*b; if(sum==d) printf(\"%d\\n\ else printf(\"%d\\n\ } return 0; } 2500 做一个正气的杭电人 Problem Description 做人要有一身正气,杭电学子都应该如此。比如我们今天的考试就应该做到“诚信”为上。 每次考试的第一个题目总是很简单,今天也不例外,本题是要求输出指定大小的\"HDU\"字符串,特别地,为了体现“正气”二字,我们要求输出的字符串也是正方形的(行数和列数相等)。 Input 输入的第一行包含一个正整数N(N<=20),表示一共有N组数据,接着是N行数据,每行包含一个正整数M(M<=50),表示一行内有M个“HDU”相连。 Output 输出指定大小的方形字符串,输出格式参见样本数据。 Sample Input 2 1 2 Sample Output HDU HDU HDU HDUHDU HDUHDU HDUHDU HDUHDU HDUHDU HDUHDU 学习指导参考 WORD格式整理版 Source 《ACM程序设计》短学期考试_软件工程及其他专业 Recommend lcy 代码: #includeprintf(\"%d\",m); for(j=0;j