ACM部分练习题目答案
(完整版)杭电acm部分答案
Problem DescriptionCalculate A + B.InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2#include<stdio.h>void main(){int a,b;while(scanf("%d %d",&a,&b)!=EOF){printf("%d\n",a+b);}}Problem DescriptionHey, welcome 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 Input1100Sample Output15050#include<stdio.h>void main(){int n,sum,i;while(scanf("%d",&n)!=EOF){sum=0;for( i=0;i<=n;i++)sum+=i;printf("%d\n\n",sum);}}Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.InputThe 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.OutputFor 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", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2112233445566778899 998877665544332211Sample OutputCase 1:1 +2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110 #include<stdio.h>#include<string.h>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;elselen_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)printf("1");for(i = len_max - 1; i >= 0; --i){printf("%d", c[i]);}printf("\n");if(t >= 1)printf("\n");}return 0;}Problem DescriptionGiven a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.InputThe 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 starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).OutputFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.Sample Input25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5Sample OutputCase 1:14 1 4Case 2:7 1 6注:最大子序列是要找出由数组成的一维数组中和最大的连续子序列。
大学ACM考试题目及作业答案整理
ACM作业与答案整理1、平面分割方法:设有n条封闭曲线画在平面上,而任何两条封闭曲线恰好相交于两点,且任何三条封闭曲线不相交于同一点,问这些封闭曲线把平面分割成的区域个数。
#include <iostream.h>int f(int n){if(n==1) return 2;else return f(n-1)+2*(n-1);}void main(){int n;while(1){cin>>n;cout<<f(n)<<endl;}}2、LELE的RPG难题:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.编程全部的满足要求的涂法.#include<iostream.h>int f(int n){if(n==1) return 3;else if(n==2) return 6;else return f(n-1)+f(n-2)*2;}void main(){int n;while(1){cin>>n;cout<<f(n)<<endl;}}3、北大ACM(1942)Paths on a GridTime Limit: 1000MS Memory Limit: 30000K DescriptionImagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time he's explaining that (a+b)2=a2+2ab+b2). So you decide to waste your time with drawing modern art instead.Fortunately you have a piece of squared paper and you choose a rectangle of size n*m on the paper. Let's call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:Really a masterpiece, isn't it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?InputThe input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimension. Input is terminated by n=m=0.OutputFor each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how many paths are there on a grid where each step of the path consists of moving one unit to the right orone unit up? You may safely assume that this number fits into a 32-bit unsigned integer.Sample Input5 41 10 0Sample Output1262#include<iostream>using namespace std;longlong f(long long m, long long n){if(n==0) return 1;else return f(m-1,n-1)*m/n;}int main(){longlongm,n;while(scanf("%I64d %I64d",&n,&m) &&n+m){printf("%I64d\n",f(m+n,min(m,n)));}return 0;}1、(并查集)若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。
计算机acm试题及答案
计算机acm试题及答案一、选择题1. 在计算机科学中,ACM代表什么?A. 人工智能与机器学习B. 计算机辅助制造C. 计算机辅助设计D. 国际计算机学会答案:D2. 下列哪个不是计算机程序设计语言?A. PythonB. JavaC. C++D. HTML答案:D3. 在计算机系统中,CPU代表什么?A. 中央处理单元B. 计算机辅助设计C. 计算机辅助制造D. 计算机辅助教学答案:A二、填空题1. 计算机的内存分为__________和__________。
答案:RAM;ROM2. 在编程中,__________是一种用于存储和操作数据的数据结构。
答案:数组3. 计算机病毒是一种__________,它能够自我复制并传播到其他计算机系统。
答案:恶意软件三、简答题1. 请简述计算机操作系统的主要功能。
答案:计算机操作系统的主要功能包括管理计算机硬件资源,提供用户界面,运行应用程序,以及控制其他系统软件和应用软件的运行。
2. 什么是云计算,它与传统的本地计算有何不同?答案:云计算是一种通过互联网提供计算资源(如服务器、存储、数据库、网络、软件等)的服务模式。
与传统的本地计算相比,云计算允许用户按需获取资源,无需购买和维护物理硬件,具有更高的灵活性和可扩展性。
四、编程题1. 编写一个程序,计算并输出从1到100(包括1和100)之间所有偶数的和。
答案:```pythonsum = 0for i in range(1, 101):if i % 2 == 0:sum += iprint(sum)```2. 给定一个字符串,编写一个函数,将字符串中的所有字符按ASCII 码值排序并返回。
答案:```pythondef sort_string(s):return ''.join(sorted(s))```五、论述题1. 论述计算机硬件和软件之间的关系及其对计算机系统性能的影响。
答案:计算机硬件是计算机系统的物质基础,包括CPU、内存、硬盘等,而软件则是运行在硬件上的程序和数据。
ACM试题及参考答案
1. 给定一个矩阵M(X, Y),列集为X ,行集为Y 。
如果存在对其列的一个排序,使得每一行的元素都严格递增,称M 是一个次序保持矩阵。
例如下图中存在一个排序x 4,x 1,x 2,x 3,x 5I ⊆X ,满足:子矩阵M(I,Y)是次序保持矩阵。
[测试数据] 矩阵M :[测试数据结果] I={ x 1,x 3,x 4,x 7,x 8}[解题思路] 将该问题归约为在一个有向图中找一条最长路径的问题。
给定矩阵M=(a ij ),行集Y ,列集X ,行子集J ⊆Y ,定义有向图D A =(V A ,E A ),其中V A 含有|X|个顶点,每个顶点代表X 中的一列,如果顶点u ,v 对应的列x u ,x v 满足,对于任意的j ∈J ,u v ij ij a a <,则有一条从u 到v 的弧(u ,v )∈E 。
显然,D A 是个无环图,可以在O(|X|2)时间内构造完毕。
对于任意的条件子集J ,A(I,J)是次序保持的当且仅当对应于J 中条件的顶点在D A 中构成一条有向路径。
从而我们只需在有向图D A 中找一条最长路径,该问题可在O(|V A |+| E A |)时间内完成。
按上面的方法构造有向图如下:有向图中找最长路径的线性时间算法。
一些表示方法如下:d out (u )为顶点u 的出度,d in (u )为顶点u 的入度,source 为入度为0的顶点,sink 为出度为0的顶点,N out (u )为u 指向的邻接点集合,P uv 为从u 到v 的最长路,显然应从source 到sink 。
在每一步为每个顶点关联一个永久的或临时的标签。
v被赋了一个临时标签(v’,i v)表明在当前步,算法找出的最长的从source到v的有向路长度为i v,且经由v’而来。
v被赋了一个永久标签[v’,i v]表明从source到v的最长有向路长度为i v,且经由v’而来,通过回溯每个顶点的永久标签就可以找出最长有向路。
整理出ACM所有题目及答案
1111111杭电:1000 A + B Problem (4)1001 Sum Problem (5)1002 A + B Problem II (6)1005 Number Sequence (8)1008 Elevator (9)1009 FatMouse' Trade (11)1021 Fibonacci Again (13)1089 A+B for Input-Output Practice (I) (14)1090 A+B for Input-Output Practice (II) (15)1091 A+B for Input-Output Practice (III) (16)1092 A+B for Input-Output Practice (IV) (17)1093 A+B for Input-Output Practice (V) (18)1094 A+B for Input-Output Practice (VI) (20)1095 A+B for Input-Output Practice (VII) (21)1096 A+B for Input-Output Practice (VIII) (22)1176 免费馅饼 (23)1204 糖果大战 (25)1213 How Many Tables (26)2000 ASCII码排序 (32)2001 计算两点间的距离 (34)2002 计算球体积 (35)2003 求绝对值 (36)2004 成绩转换 (37)2005 第几天? (38)2006 求奇数的乘积 (40)2007 平方和与立方和 (41)2008 数值统计 (42)2009 求数列的和 (43)2010 水仙花数 (44)2011 多项式求和 (46)2012 素数判定 (47)2014 青年歌手大奖赛_评委会打分 (49)2015 偶数求和 (50)2016 数据的交换输出 (52)2017 字符串统计 (54)2019 数列有序! (55)2020 绝对值排序 (56)2021 发工资咯:) (58)2033 人见人爱A+B (59)2037 今年暑假不AC (61)2039 三角形 (63)2040 亲和数 (64)2045 不容易系列之(3)—— LELE的RPG难题 (65)2049 不容易系列之(4)——考新郎 (66)2056 Rectangles (68)2073 无限的路 (69)2084 数塔 (71)2201 熊猫阿波的故事 (72)2212 DFS (73)2304 Electrical Outlets (74)2309 ICPC Score Totalizer Software (75)2317 Nasty Hacks (77)2401 Baskets of Gold Coins (78)2500 做一个正气的杭电人 (79)2501 Tiling_easy version (80)2502 月之数 (81)2503 a/b + c/d (82)2504 又见GCD (83)2519 新生晚会 (84)2520 我是菜鸟,我怕谁 (85)2521 反素数 (86)2522 A simple problem (88)2523 SORT AGAIN (89)2524 矩形A + B (90)2535 Vote (91)2537 8球胜负 (93)2539 点球大战 (95)2547 无剑无我 (98)2548 两军交锋 (99)2549 壮志难酬 (100)2550 百步穿杨 (101)2551 竹青遍野 (103)2552 三足鼎立 (104)2553 N皇后问题 (105)2554 N对数的排列问题 (106)2555 人人都能参加第30届校田径运动会了 (107)2560 Buildings (110)2561 第二小整数 (112)2562 奇偶位互换 (113)2563 统计问题 (114)2564 词组缩写 (115)2565 放大的X (117)2566 统计硬币 (118)2567 寻梦 (119)2568 前进 (121)2569 彼岸 (123)2700 Parity (124)2577 How to Type (126)北京大学:1035 Spell checker (129)1061 青蛙的约会 (133)1142 Smith Numbers (136)1200 Crazy Search (139)1811 Prime Test (141)2262 Goldbach's Conjecture (146)2407 Relatives (150)2447 RSA (152)2503 Babelfish (156)2513 Colored Sticks (159)ACM算法:kurXX最小生成树 (163)Prim (164)堆实现最短路 (166)最短路DIJ普通版 (167)floyd (168)BELL_MAN (168)拓扑排序 (169)DFS强连通分支 (170)最大匹配 (172)还有两个最大匹配模板 (173)最大权匹配,KM算法 (175)两种欧拉路 (177)无向图: (177)有向图: (178)【最大流】Edmonds Karp (178)dinic (179)【最小费用最大流】Edmonds Karp对偶算法 (181)ACM题目:【题目】排球队员站位问题 (182)【题目】把自然数N分解为若干个自然数之和。
整理出ACM所有题目及答案
1000 A + B ProblemProblem DescriptionCalculate A + B .InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2AuthorHDOJ代码:#include<stdio.h>int main(){int a , b;while( scanf ( "%d %d" ,& a,& b)!= EOF)printf( "%d\n" , a+b);}1001 Sum ProblemProblem DescriptionHey, welcome 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 Input1100Sample Output15050AuthorDOOM III解答:#include<stdio.h>main (){int n , i , sum;sum =0;while (( scanf ( "%d" ,& n)!=- 1)) {sum for =0;( i =0; i <= n; i ++)sum +=i ;printf( "%d\n\n" , sum);}}1002 A + B Problem IIProblem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. InputThe 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.OutputFor 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", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2Sample OutputCase 1:1+2=3Case 2:Author代码:#include <stdio.h>#include <string.h>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 ;elselen_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 )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 DescriptionA 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).InputThe 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.OutputFor each test case, print the value of f(n) on a single line.Sample Input1 1 312100 0 0Sample Output25AuthorCHEN, ShunbaoSourceRecommendJGShining代码:#include<stdio.h>int f [ 200 ];int main (){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 ]);elseprintf ( "%d\n" , f [ n]);}elseprintf ( "1\n" );}return 0 ;}1008 ElevatorProblem DescriptionThe 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 elevatorup 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.InputThere 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.OutputPrint the total time on a single line for each test case.Sample Input1 23231Sample Output1741AuthorZHENG, JianqiangSourceRecommendJGShining代码:#include<stdio.h>int a [ 110 ];int main(){int while { sum , i , n;( scanf ( "%d" ,& n)&& n!= 0)forscanf ( i =1; i <= n; i ++)( "%d" ,& a[ i ]);sum a for=0;[ 0]= 0;( i =1; i <= n; i ++){ifsum ( a[ i ]> a[ i - 1])+=6*( a[ i ]- a[ i - 1]);elsesum +=4*( a[ i - 1]- a[ i ]);sum +=5;printf ( "%d\n" , sum);}return 0 ;}1009 FatMouse' TradeProblem DescriptionFatMouse 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.InputThe input consists of multiple test cases. Each test case begins with a line containing two non-negative integersM and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test caseis followed by two -1's. All integers are not greater than 1000.OutputFor 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 Input53724352203251824151510-1-1Sample OutputAuthorCHEN, YueSourceRecommendJGShining代码:#include<stdio.h>#include<string.h>#define MAX 1000int main(){int i,j,m,n,temp;int J[MAX],F[MAX];double P[MAX];double sum,temp1;scanf("%d%d",&m,&n);while(m!=-1&&n!=-1){sum=0;memset(J,0,MAX*sizeof(int));memset(F,0,MAX*sizeof(int));memset(P,0,MAX*sizeof(double));for(i=0;i<n;i++){ scanf("%d%d",&J[i],&F[i]); P[i]=J[i]*1.0/((double)F[i]); }for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(P[i]<P[j]){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<n;i++) { if(m<F[i]){ else{sum+=m/((double)F[i])*J[i];sum+=J[i];break;m-=F[i];} }}printf("%.3lf\n",sum); scanf("%d%d",&m,&n); }return 0; }1021 Fibonacci AgainProblem DescriptionThere are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000).OutputPrint the word "yes" if 3 divide evenly into F(n). Print the word "no" if not.Sample Input0 1 2 3 4 5Sample Outputno no yes no no noAuthorLeojayRecommendJGShining#include<stdio.h> int main () { long while if printfn ;( scanf ( "%ld" ,& n) !=( n%8==2 || n %8==6) ( "yes\n" ); EOF ) elseprintf ( "no\n");return0 ;}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 Input151020Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main (){int a , b;while( scanf ( "%d%d" ,& a,& b)!=EOF)printf( "%d\n" , 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 Input2151020Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>#define M 1000void main (){int a , b, n, j [ M], 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);j[ i ]= a+b;}i=0;while( i <n){printf( "%d" , j [ i ]);i++;printf( "\n" );}}1091 A+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 ofoutput for each line in input.Sample Input15102000Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main (){int a , b;scanf( "%d %d" ,& a,& b);while(!( a== 0&&b==0)){printf( "%d\n" , a+b);scanf( "%d %d" ,& a,& b);}}1092 A+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.。
整理出ACM所有题目和答案解析
1000 A + B ProblemProblem DescriptionCalculate A + B.InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2AuthorHDOJ代码:#include<stdio.h>int main(){int a,b;while(scanf("%d %d",&a,&b)!=EOF)printf("%d\n",a+b);}1001 Sum ProblemProblem DescriptionHey, welcome 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 Input1100Sample Output15050AuthorDOOM 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("%d\n\n",sum);}}1002 A + B Problem IIProblem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.InputThe 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.OutputFor 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", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2112233445566778899 998877665544332211Sample OutputCase 1:1 +2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110AuthorIgnatius.L代码:#include <stdio.h>#include <string.h>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;elselen_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)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 SequenceProblem DescriptionA 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).InputThe 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.OutputFor each test case, print the value of f(n) on a single line.Sample Input1 1 31 2 100 0 0Sample Output25AuthorCHEN, ShunbaoSourceZJCPC2004RecommendJGShining代码:#include<stdio.h>int f[200];int main(){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]);elseprintf("%d\n",f[n]);}elseprintf("1\n");}return 0;}1008 ElevatorProblem DescriptionThe 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.InputThere 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.OutputPrint the total time on a single line for each test case.Sample Input1 23 2 3 1Sample Output1741AuthorZHENG, JianqiangSourceZJCPC2004RecommendJGShining代码:#include<stdio.h>int a[110];int main(){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]);elsesum+=4*(a[i-1]-a[i]);sum+=5;}printf("%d\n",sum);}return 0;}1009 FatMouse' TradeProblem DescriptionFatMouse 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.InputThe 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 followedby two -1's. All integers are not greater than 1000.OutputFor 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 Input5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1Sample Output13.333 31.500AuthorCHEN, YueSourceZJCPC2004RecommendJGShining代码:#include<stdio.h>#include<string.h>#define MAX 1000int main(){int i,j,m,n,temp;int J[MAX],F[MAX];double P[MAX];double sum,temp1;scanf("%d%d",&m,&n);while(m!=-1&&n!=-1){sum=0;memset(J,0,MAX*sizeof(int));memset(F,0,MAX*sizeof(int));memset(P,0,MAX*sizeof(double));for(i=0;i<n;i++){ scanf("%d%d",&J[i],&F[i]); P[i]=J[i]*1.0/((double)F[i]); }for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(P[i]<P[j]){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<n;i++){if(m<F[i]){ sum+=m/((double)F[i])*J[i]; break; }else { sum+=J[i]; m-=F[i]; }}printf("%.3lf\n",sum); scanf("%d%d",&m,&n);}return 0;}1021 Fibonacci AgainProblem DescriptionThere are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000).OutputPrint the word "yes" if 3 divide evenly into F(n).Print the word "no" if not.Sample Input12345Sample OutputnonoyesnononoAuthorLeojayRecommendJGShining#include<stdio.h>int main(){long n;while(scanf("%ld",&n) != EOF) if (n%8==2 || n%8==6)printf("yes\n");elseprintf("no\n");return 0;}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 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n",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 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>#define M 1000void main(){int a ,b,n,j[M],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);j[i]=a+b;}i=0;while(i<n){printf("%d",j[i]);i++;printf("\n");}}1091 A+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 0Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int a,b;scanf("%d %d",&a,&b);while(!(a==0&&b==0)){printf("%d\n",a+b);scanf("%d %d",&a,&b);}}1092 A+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 5Sample Output1015AuthorlcyRecommendJGShining解答:#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("%d\n",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 5Sample Output1015Authorlcy解答:#include<stdio.h>main(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(i=0;i<n;i++){scanf("%d",&b);for(j=0;j<b;j++){scanf("%d",&a);sum+=a;}printf("%d\n",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 5Sample Output1015AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(j=0;j<n;j++){scanf("%d",&a);sum+=a;}printf("%d\n",sum);sum=0;}}[ Copy to Clipboard ][ Save to File]1095 A+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 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>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 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 3Sample Output10156AuthorlcyRecommendJGShining解答: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正走在回家的小径上,忽然天上掉下大把大把的馅饼。
acm基础试题及答案
acm基础试题及答案ACM基础试题及答案1. 以下哪个选项是C语言中正确的字符串声明方式?A. char str[] = "Hello World";B. char str[10] = "Hello World";C. string str = "Hello World";D. char str = "Hello World";答案:A2. 在C++中,以下哪个关键字用于定义类的私有成员?A. publicB. privateC. protectedD. static答案:B3. 以下哪个数据结构允许快速随机访问元素?A. 链表B. 队列C. 数组D. 栈答案:C4. 在Python中,以下哪个函数用于将列表中的元素连接成一个字符串?A. join()B. concat()C. append()D. merge()答案:A5. 在数据库管理系统中,SQL代表什么?A. Structured Query LanguageB. Standard Query LanguageC. Simple Query LanguageD. System Query Language答案:A6. 在HTML中,用于定义最重要的标题的标签是什么?A. <h1>B. <title>C. <header>D. <h6>答案:A7. 在JavaScript中,以下哪个方法用于将字符串转换为小写?A. toUpperCase()B. toLowerCase()C. toUpperCase()D. toCamelCase()答案:B8. 在Unix/Linux系统中,哪个命令用于查看当前目录下的文件和文件夹?A. lsB. pwdC. cdD. mkdir答案:A9. 在C语言中,以下哪个函数用于计算数组中元素的总和?A. sum()B. count()C. sizeof()D. memset()答案:A10. 在Java中,以下哪个关键字用于创建单例模式?A. staticB. finalC. synchronizedD. volatile答案:A。
Acm试题及答案
Acm试题及答案Acm试题及答案1001 Sum Problem (2)1089 A+B for Input-Output Practice (I) (3) 1090 A+B for Input-Output Practice (II) (4) 1091A+B for Input-Output Practice (III) (6) 1092A+B for Input-Output Practice (IV) (7) 1093 A+B for Input-Output Practice (V) (9) 1094 A+B for Input-Output Practice (VI) (11) 1095A+B for Input-Output Practice (VII) (12) 1096 A+B for Input-Output Practice (VIII) (14) 2000 ASCII码排序 (15)2001计算两点间的距离 (16)2002计算球体积 (17)2003求绝对值 (18)2004成绩转换 (19)2005第几天? (20)2006求奇数的乘积 (22)2007平方和与立方和 (23)2008数值统计 (24)2009求数列的和 (25)2010水仙花数 (27)2011多项式求和 (28)2012素数判定 (29)2014青年歌手大奖赛_评委会打分 (31)2015偶数求和 (32)2016数据的交换输出 (34)2017字符串统计 (35)2019数列有序! (37)2020绝对值排序 (38)2021发工资咯:) (40)2033人见人爱A+B (41)2039三角形 (43)2040亲和数 (44)1001 Sum ProblemProblem DescriptionHey, welcome 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 Input1100Sample Output15050AuthorDOOM III解答:#includemain(){int n,i,sum;sum=0;while((scanf("%d",&n)!=-1)){sum=0;for(i=0;i<=n;i++)sum+=i;printf("%d\n\n",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 20Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n",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 20Sample Output630AuthorlcyRecommendJGShining解答:#include#define M 1000void main(){int a ,b,n,j[M],i;//printf("please input n:\n");scanf("%d",&n);for(i=0;i<n;i++)< bdsfid="216" p=""></n;i++)<> {scanf("%d%d",&a,&b);//printf("%d %d",a,b);j[i]=a+b;}i=0;while(i<n)< bdsfid="224" p=""></n)<>{printf("%d",j[i]); 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 0Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;scanf("%d %d",&a,&b);while(!(a==0&&b==0)){printf("%d\n",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 followin 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 5Sample Output1015AuthorlcyRecommendJGShining解答:#includeint main(){int n,sum,i,t;while(scanf("%d",&n)!=EOF&&n!=0)sum=0;for(i=0;i<n;i++)< bdsfid="290" p=""></n;i++)<>{scanf("%d",&t);sum=sum+t;}printf("%d\n",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 5Sample Output1015Authorlcy解答:#includemain()int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(i=0;i<n;i++)< bdsfid="322" p=""></n;i++)<>{scanf("%d",&b);for(j=0;j<b;j++)< bdsfid="326" p=""></b;j++)<>{scanf("%d",&a);sum+=a;}printf("%d\n",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 5Sample Output1015AuthorlcyRecommendJGShining解答:#includemain(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1) {for(j=0;j<n;j++)< bdsfid="362" p=""></n;j++)<>{scanf("%d",&a);sum+=a;}printf("%d\n",sum); sum=0;}}1095A+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 20Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){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 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 sumin 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 3Sample Output10156AuthorlcyRecommendJGShining解答: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]);}2000 ASCII码排序Problem Description输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
acm数学竞赛试题及答案
acm数学竞赛试题及答案# 题目一:数列问题问题描述:给定一个数列 \( a_1, a_2, a_3, \ldots, a_n \),数列中每个元素都是正整数,且满足 \( a_i = a_{i-1} + a_{i-2} \) 对于所有\( i \geq 3 \)。
如果 \( a_1 = 1 \) 且 \( a_2 = 1 \),请找出数列的第 \( n \) 项。
解答:根据题意,这是一个斐波那契数列。
第 \( n \) 项的值可以通过递归关系计算得出。
对于 \( n \) 的值,可以使用以下递归公式:\[ a_n = a_{n-1} + a_{n-2} \]其中,\( a_1 = 1 \) 和 \( a_2 = 1 \)。
因此,数列的前几项为 1, 1, 2, 3, 5, 8, 13, 21, ...。
对于任意的 \( n \),可以通过递归或动态规划方法计算出 \( a_n \)。
# 题目二:组合问题问题描述:从 \( n \) 个不同的元素中选择 \( k \) 个元素的所有可能组合的个数是多少?解答:这个问题可以通过组合数学中的二项式系数来解决。
从 \( n \) 个不同元素中选择 \( k \) 个元素的组合数 \( C(n, k) \) 可以用以下公式计算:\[ C(n, k) = \frac{n!}{k!(n-k)!} \]其中,\( n! \) 表示 \( n \) 的阶乘。
# 题目三:几何问题问题描述:在一个直角坐标系中,给定三个点 \( A(x_1, y_1) \),\( B(x_2, y_2) \) 和 \( C(x_3, y_3) \)。
如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 是垂直的,求证 \( A \) 是直角三角形 \( ABC \) 的直角顶点。
解答:如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 垂直,那么它们的数量积(点积)应该为零。
整理出ACM所有题目及答案
1000 A + B Problem Problem DescriptionCalculate A + B.InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2AuthorHDOJ代码:#include<stdio.h>int main(){int a,b;while(scanf("%d %d",&a,&b)!=EOF)printf("%d\n",a+b);}1001 Sum Problem Problem DescriptionHey, welcome 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 Input1100Sample Output15050AuthorDOOM 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("%d\n\n",sum);}}1002 A + B Problem IIProblem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.InputThe 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.OutputFor 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", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2112233445566778899 998877665544332211Sample OutputCase 1:1 +2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110AuthorIgnatius.L代码:#include <stdio.h>#include <string.h>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;elselen_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)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 DescriptionA 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).。
Acm试题及答案
Acm试题及答案1001 Sum Problem (2)1089 A+B for In put-Output Practice (I) (3)1090 A+B for In put-Output Practice (II) (4)1091 A+B for In put-Output Practice (III) (6)1092 A+B for Input-Output Practice (IV) (7)1093 A+B for In put-Output Practice (V) 9 1094 A+B for In put-Output Practice (VI) (11)1095 A+B for Input-Output Practice (VII) (12)1096 A+B for In put-Output Practice (VIII) (14)2000 ASCII 码排序 (15)2001计算两点间的距离 (16)2002计算球体积 (17)2003求绝对值 (18)2004成绩转换 (19)2005第几天? (20)2006求奇数的乘积 (22)2007平方和与立方和 (23)2008数值统计 (24)2009求数列的和 (25)2010水仙花数 (27)2011多项式求和 (28)2012素数判定 (29)2014青年歌手大奖赛—评委会打分 (31)2015偶数求和 (32)2016数据的交换输出 (34)2017字符串统计 (35)2019数列有序! (37)2020绝对值排序 (38)2021发工资咯:) (40)2033人见人爱A+B (41)2039三角形 (43)2040亲和数 (44)1001 Sum ProblemProblem Descripti onHey, welcome to HDOJ(Ha ngzhou Dianzi Uni versity On li ne Judge).In this problem, your task is to calculate SUM( n) = 1 + 2 + 3 + ... + n.In putThe in put will con sist of a series of in tegers n, one in teger 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 sig ned in teger.Sample In put1100Sample Output15050AuthorDOOM III解答:#i nclude<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 ("%d\n\n" , sum);}1089 A+B for In put-O utput Practice(I)Problem Descripti onYour task is to Calculate a + b.Too easy?! Of course! I specially desig ned the problem for acm beg inn ers.You must have found that some problems have the same titles with this one, yes, all these problems were desig ned for the same aim.In putThe in put will con sist of a series of pairs of in tegers a and b, separated by a space, one pair of in tegers per line.OutputFor each pair of in put in tegers 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 in put.Sample In put1 510 20Sample Output630AuthorlcyRecomme ndJGShi ning解答:#i nclude<stdio.h>main (){int while printf a , b;(scanf ("%d%d",& a,& b)!= EOF) ("%d\n" , a+b);1090 A+B for In put-Output Practice(II)Problem Descripti onYour task is to Calculate a + b.In putIn put contains an in teger N in the first line, and the n N lines follow. Each line con sists of a pair of in tegers a and b, separated by a space, one pair of in tegers per line.OutputFor each pair of in put in tegers 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 in put.Sample In put21 510 20Sample Output630AuthorlcyRecomme ndJGShi ning解答:#i nclude<stdio.h>#defi ne M 1000void mai n (){int a , b, n, j [ M], i ;//prin tf("please in put n:\n");scanf ("%d",& n);for (i =0; i <n; i ++){scanf ("%d%d",& a,& b);//prin tf("%d %d",a,b);j [ i ]= a+b;}i =0;while{ printf iprintf }}(i <n)("%d" , j [ i ]); ++;("\n");1091 A+B for In put-Output Practice (III)Problem Descripti onYour task is to Calculate a + b.In putIn put contains multiple test cases. Each test case contains a pair of in tegers a and b, one pair of in tegers per line. A test case containing 0 0 term in ates the in put and this test case is not to be processed.OutputFor each pair of in put in tegers 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 in put.Sample In put1 510 200 0Sample Output630AuthorlcyRecomme ndJGShi ning解答:#i nclude<stdio.h>main (){int a , b;scanf ("%d %d",& a,& b); while (!( a==0&&b==0)){printf ("%d\n" , a+b);scanf ("%d %d",& a,&b);}}1092 A+B for Input-Output Practice (IV)Problem Descripti onYour task is to Calculate the sum of some in tegers.In putIn put contains multiple test cases. Each test case contains a in teger N, and the n N in tegers follow in the same line. A test case start ing with 0 term in ates the in put and this test case is not to be processed.OutputFor each group of in put in tegers you should output their sum in one line, and with one line of output for each line in in put.Sample In put4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecomme ndJGShi ning解答:#i nclude <stdio.h>int mai n (){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 ;("%d\n" , sum);} printf}1093 A+B for In put-Output Practice (V)Problem Descripti onYour task is to calculate the sum of some in tegers.In putIn put contains an in teger N in the first line, and the n N lines follow. Each line starts with a in teger M, and then M integers follow in the same line.OutputFor each group of in put in tegers you should output their sum in one line, and with one line of output for each line in in put.Sample In put24 1 2 3 45 1 2 3 4 5Sample Output1015Authorlcy解答:#i nclude<stdio.h>main (){int n , a, b, i , j , sum;sum =0;while ( scanf ("%d\n" ,& n)!=- 1){for (i =0; i <n; i ++){scanf ("%d",& b);for (j =0; j <b; j ++){scanf ("%d",& a);sum +=a;printf ("%d\n",sum);sum =0;}}}1094 A+B for Input-Output Practice(VI)Problem Descripti onYour task is to calculate the sum of some in tegers.In putIn put contains multiple test cases, and one case one line. Each case starts with an in teger N, and then N integers follow in the same line.OutputFor each test case you should output the sum of N in tegers in one line, and with one line of output for each line in in put.Sample In put4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecomme ndJGShi ning解答:#i nclude<stdio.h>main (){int n , a, b, i , j , sum;sum =0;while ( scanf ("%d\n" ,& n)!=- 1) {for (j =0; j <n; j ++){scanf ("%d",& a);sum +=a;}printf ("%d\n" , sum);sum =0;}}1095 A+B for Input-Output Practice (VII)Problem Descripti onYour task is to Calculate a + b.In putThe in put will con sist of a series of pairs of in tegers a and b, separated by a space, one pair of in tegers per line.OutputFor each pair of in put in tegers a and b you should output the sum of a and b, and followed by a bla nk line.Sample In put1 510 20Sample Output630AuthorlcyRecomme ndJGShi ning解答:#i nclude<stdio.h>main (){int a , b;while (scanf ("%d%d",& a,& b)!= EOF)printf ("%d\n\n" , a+b);}1096 A+B for In put-Output Practice(VIII)Problem Descripti onYour task is to calculate the sum of some in tegers.In putIn put contains an in teger N in the first line, and the n N lines follow. Each line starts with a in teger M, and then M integers follow in the same line.OutputFor each group of in put in tegers you should output their sum in one line, and you must note that there is a bla nk line betwee n outputs.Sample In put34 1 2 3 4Sample Output10156AuthorlcyRecomme ndJGShi ning 解答:int mai n (){a , b, i , j , l [ 1000], k;intscanf ("%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 ]);}2000 ASCII码排序Problem Descripti on输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
2022南开大学acm竞赛题目及答案
2022南开大学acm竞赛题目及答案第一部分理论题(总分100分,考试时间:60分钟)一、选择题(单选,共30题,每个2分)1、下面关于变量及其作用范围的陈述哪个是不对的?()A.实例变量是类的成员变量。
B.实例变量用关键字static声明。
//Static 声明的是类变量C.在方法中定义的局部变量在该方法被执行时创建。
D.局部变量在使用前必须被初始化。
2、下面哪条语句把方法声明为抽象的公共方法?()A.public abstract method();B.public abstract void method();C.public abstract void method(){}D.public void method() extends abstract;3、哪个是将一个十六进制值赋值给一个long型变量?()A.long number = 345L;B.long number = 0345;C.long number = 0345L;D.long number = 0x345L;4、下面的哪个赋值语句是不对的?()A.float f = 11.1;B.double d = 5.3E12;C.double d = 3.14159;D.double d = 3.14D;5、main方法是Java Application程序执行的入口点,关于main 方法的方法头以下哪项是合法的()。
A、public static void main()B、public static void main(String[ ] args)C、public static int main(String[ ] args)D、public void main(String arg[ ])6、在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺序各不相同,传回的值也可以不相同。
这种面向对象程序的特性称为()。
acm考试题目及答案
acm考试题目及答案1. 题目:给定一个整数数组,找出数组中没有出现的最小的正整数。
答案:首先,我们可以遍历数组,将每个元素与它的索引对应起来,即如果数组中存在数字`i`,则将其与索引`i-1`对应。
然后,我们可以遍历数组,检查索引`i`是否与数组中第`i`个元素相等。
如果不相等,则索引`i`对应的值就是没有出现的最小正整数。
如果所有元素都与其索引对应,则没有出现的最小正整数为数组长度加1。
2. 题目:实现一个函数,检查一个链表是否为回文结构。
答案:我们可以将链表的前半部分反转,然后比较反转后的前半部分与后半部分是否相同。
如果相同,则链表是回文的;如果不相同,则不是。
具体步骤如下:首先找到链表的中点,然后反转前半部分链表,接着比较反转后的前半部分与后半部分是否相同,最后将前半部分链表再次反转回来。
3. 题目:给定一个只包含 '(' 和 ')' 的字符串,判断字符串是否有效。
答案:我们可以使用一个栈来解决这个问题。
遍历字符串中的每个字符,如果遇到'(',则将其压入栈中;如果遇到')',则检查栈是否为空,如果为空,则字符串无效;如果不为空,则弹出栈顶元素。
遍历结束后,如果栈为空,则字符串有效;如果栈不为空,则字符串无效。
4. 题目:找出一个无序数组中第k大的元素。
答案:我们可以使用快速选择算法来解决这个问题。
首先,选择一个元素作为基准,然后将数组分为两部分:一部分是大于基准的元素,另一部分是小于基准的元素。
根据基准的位置,我们可以确定第k大的元素是在基准的左边还是右边,然后递归地在相应的部分中寻找第k大的元素。
重复这个过程,直到找到第k大的元素。
5. 题目:给定一个字符串,找出其中不含有重复字符的最长子串的长度。
答案:我们可以使用滑动窗口的方法来解决这个问题。
维护一个窗口,记录窗口内字符的出现情况。
遍历字符串,如果遇到重复的字符,则移动窗口的左边界,直到窗口内没有重复的字符。
Acm试题及答案
Acm试题及答案1001 Sum Problem (2)1089 A+B for Input-Output Practice (I) (4)1090 A+B for Input-Output Practice (II) (6)1091 A+B for Input-Output Practice (III) (8)1092 A+B for Input-Output Practice (IV) (9)1093 A+B for Input-Output Practice (V) (11)1094 A+B for Input-Output Practice (VI) (12)1095 A+B for Input-Output Practice (VII) (13)1096 A+B for Input-Output Practice (VIII) (14)2000 ASCII码排序 (16)2001计算两点间的距离 (17)2002计算球体积 (19)2003求绝对值 (20)2004成绩转换 (21)2005第几天? (22)2006求奇数的乘积 (24)2007平方和与立方和 (26)2008数值统计 (27)2009求数列的和 (28)2010水仙花数 (29)2011多项式求和 (31)2012素数判定 (33)2014青年歌手大奖赛_评委会打分 (34)2015偶数求和 (36)2016数据的交换输出 (38)2017字符串统计 (40)2019数列有序! (41)2020绝对值排序 (43)2021发工资咯:) (45)2033人见人爱A+B (46)2039三角形 (48)2040亲和数 (49)1001 Sum ProblemProblem DescriptionHey, welcome 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 Input1100Sample Output15050AuthorDOOM 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("%d\n\n",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 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int a,b;while(scanf("%d%d",&a,&b)!=EOF) printf("%d\n",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 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>#define M 1000void main(){int a ,b,n,j[M],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);j[i]=a+b;}i=0;while(i<n){printf("%d",j[i]);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 0Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int a,b;scanf("%d %d",&a,&b);while(!(a==0&&b==0)){printf("%d\n",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 5Sample Output1015AuthorlcyRecommendJGShining解答:#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("%d\n",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 5Sample Output1015Authorlcy解答:#include<stdio.h>main(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(i=0;i<n;i++){scanf("%d",&b);for(j=0;j<b;j++){scanf("%d",&a); sum+=a;}printf("%d\n",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 5Sample Output1015AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1) {for(j=0;j<n;j++){scanf("%d",&a); sum+=a;}printf("%d\n",sum); sum=0;}}1095A+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 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>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 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 3Sample Output10156AuthorlcyRecommendJGShining解答: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]);}2000 ASCII码排序Problem Description输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
acm编程例题 参考答案
acm编程例题参考答案ACM编程例题参考答案ACM(Advanced Computer Mathematics)是一种面向计算机科学与技术的竞赛形式,旨在提高参与者的编程技能和解决问题的能力。
ACM编程例题是指在ACM竞赛中出现的一系列编程题目,这些题目涵盖了各种算法和数据结构的应用。
本文将给出一些ACM编程例题的参考答案,希望能够帮助读者更好地理解和掌握这些题目的解法。
一、题目一:最大公约数题目描述:给定两个正整数a和b,求它们的最大公约数。
解题思路:最大公约数可以通过欧几里得算法来求解。
该算法的基本思想是,两个正整数的最大公约数等于其中较小的数和两数之差的最大公约数。
具体的实现可以使用递归或循环的方式。
代码示例:```c++int gcd(int a, int b) {if (b == 0) {return a;}return gcd(b, a % b);}```二、题目二:素数判断题目描述:给定一个正整数n,判断它是否为素数。
解题思路:素数是只能被1和自身整除的正整数。
判断一个数是否为素数可以使用试除法,即从2开始,依次判断n是否能被2到sqrt(n)之间的数整除。
如果存在能整除n的数,则n不是素数;否则,n是素数。
代码示例:```c++bool isPrime(int n) {if (n <= 1) {return false;}for (int i = 2; i * i <= n; i++) {if (n % i == 0) {return false;}}return true;}```三、题目三:字符串反转题目描述:给定一个字符串s,将其反转后输出。
解题思路:字符串反转可以通过将字符串的首尾字符依次交换来实现。
可以使用双指针的方式,一个指针指向字符串的首字符,另一个指针指向字符串的尾字符,然后交换两个指针所指向的字符,并向中间移动,直到两个指针相遇。
代码示例:```c++void reverseString(string& s) {int left = 0;int right = s.length() - 1;while (left < right) {swap(s[left], s[right]);left++;right--;}}```四、题目四:二分查找题目描述:给定一个有序数组和一个目标值,使用二分查找算法在数组中找到目标值的索引,如果目标值不存在,则返回-1。
acm初级试题及答案
acm初级试题及答案1. 题目:字符串反转- 描述:编写一个函数,实现对输入字符串的反转。
- 输入:一个字符串。
- 输出:反转后的字符串。
答案:```pythondef reverse_string(s):return s[::-1]```2. 题目:求最大公约数- 描述:编写一个函数,计算两个正整数的最大公约数。
- 输入:两个正整数。
- 输出:两个数的最大公约数。
答案:```pythondef gcd(a, b):while b:a, b = b, a % breturn a```3. 题目:计算阶乘- 描述:编写一个函数,计算一个非负整数的阶乘。
- 输入:一个非负整数。
- 输出:该整数的阶乘。
答案:```pythondef factorial(n):if n == 0:return 1else:return n * factorial(n-1)```4. 题目:判断素数- 描述:编写一个函数,判断一个正整数是否为素数。
- 输入:一个正整数。
- 输出:如果该数是素数,返回True;否则返回False。
答案:```pythondef is_prime(n):if n <= 1:return Falsefor i in range(2, int(n0.5) + 1):if n % i == 0:return Falsereturn True```5. 题目:寻找数组中第二大的数- 描述:编写一个函数,找出数组中第二大的数。
- 输入:一个整数数组。
- 输出:数组中第二大的数。
答案:```pythondef find_second_max(arr):first = second = float('-inf')for num in arr:if num > first:second = firstfirst = numelif num > second and num != first: second = numreturn second```。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ACM部分习题答案:A +B ProblemTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 100972 Accepted Submission(s): 33404Problem DescriptionCalculate A + B.InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2# include<stdio.h>Int main(){int x,y,s;while(scanf("%d %d",&x,&y)!=EOF){s=x+y;printf("%d\n",s);}return 0;}Sum ProblemTime Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 85964 Accepted Submission(s): 19422Problem DescriptionHey, welcome 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 Input1100Sample Output15050# include<stdio.h>int main(){int n;long int s;while(scanf("%d",&n)!=EOF){ s=0;while(n>0){s=s+n;n--;}printf("%ld\n\n",s);}return 0;}A +B Problem IITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 58216 Accepted Submission(s): 10500Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.InputThe 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.OutputFor 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", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2112233445566778899 998877665544332211Sample OutputCase 1:1 +2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110#include<stdio.h>#include<string.h>int main(){ char x[1001],y[1001],z[1001];int n,i,j,k,m,o;scanf("%d",&n);o=n;while(n--){ scanf("%s%s",x,y);i=strlen(x); j=strlen(y);for(k=0,m=0;i>0&&j>0;i--,j--){m+=x[i-1]-'0'+y[j-1]-'0';z[k++]=m%10+'0'; m/=10;}for(;i>0;i--){m+=x[i-1]-'0';z[k++]=m%10+'0';m/=10;}for(;j>0;j--){ m+=y[j-1]-'0';z[k++]=m%10+'0'; m/=10;}if(m>0) z[k++]=m%10+'0';printf("Case %d:\n%s + %s = ",o-n,x,y);for(;k>0;k--) printf("%c",z[k-1]);printf("\n"); if(n) printf("\n"); }return 0;}Let the Balloon RiseTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24082 Accepted Submission(s): 7343Problem DescriptionContest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.This year, they decide to leave this lovely job to you.InputInput contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.A test case with N = 0 terminates the input and this test case is not to be processed.OutputFor each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.Sample Input5greenredblueredred3pinkorangepinkSample Outputredpink#include<stdio.h>#include<string.h>void main(){int n,i,j,k,t,a[1001];char h[1001][16];while(scanf("%d",&n)!=EOF){if(n==0) break;for(i=0;i<=n;++i) a[i]=0;t=i=k=0;while(i<n) scanf("%s",h[i++]);for(i=0;i<n;++i)for(j=i+1;j<n;++j)if(strcmp(h[i],h[j])==0){++a[i];if(a[i]>k){k=a[i];t=i;}}printf("%s\n",h[t]);}}ElevatorTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12849 Accepted Submission(s): 6646Problem DescriptionThe 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 floorwhen the requests are fulfilled.InputThere 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.OutputPrint the total time on a single line for each test case.Sample Input1 23 2 3 1Sample Output1741#include <stdio.h>int main(){int a,b,n,t;while(scanf("%d",&n)!=EOF&&n){t=0;b=0;t+=n*5;//停的总时间累加起来while(n--){scanf("%d",&a);if(a>b)t+=6*(a-b);//与前次比若上升else if(a<b)t+=4*(b-a);//与前次比若下降b=a;}printf("%d\n",t);}return 0;}Digital RootsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16322 Accepted Submission(s): 4610Problem DescriptionThe digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.InputThe input file will contain a list of positive integers, one per line. The end of the input will beindicated by an integer value of zero.OutputFor each integer in the input, output its digital root on a separate line of the output. Sample Input2439Sample Output63#include<stdio.h>#include<string.h>int main(){int l,s,i;char a[1000];while(scanf("%s",a)){l=strlen(a);s=0;if(l==1&&(strcmp(a,"0")==0) ) break;else{for(i=0;i<l;i++)s+=a[i]-'0';while(s>9){l=0;while (s){l+=s%10;s/=10;}s=l;}s=(s%10+s/10);printf("%d\n",s);}}return 0;}As Easy As A+BTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12198 Accepted Submission(s): 4870Problem DescriptionThese days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.Give you some integers, your task is to sort these number ascending (升序).You should know how easy the problem is now!Good luck!inputInput contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.It is guarantied that all integers are in the range of 32-int.OutputFor each case, print the sorting result, and one line one case.Sample Input23 2 1 39 1 4 7 2 5 8 3 6 9Sample Output1 2 31 2 3 4 5 6 7 8 9#include <stdio.h>int main(){int n,i;scanf("%d",&n);for (i=0;i<n;i++){int m,j,k,t,a[1001];scanf ("%d",&m);for (j=0;j<m;j++)scanf ("%d",&a[j]);for (j=0;j<m;j++)for (k=0;k<m-1-j;k++)if (a[k]>a[k+1]){t=a[k];a[k]=a[k+1];a[k+1]=t;}printf("%d", a[0]);for (j=1;j<m;j++){printf (" %d", a[j]);}printf("\n"); }return 0;}。