完整word版杭电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. 在计算机科学中,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竞赛试题及答案
acm竞赛试题及答案ACM竞赛试题及答案1. 问题描述:给定一个整数数组,找出数组中没有出现的最小的正整数。
2. 输入格式:第一行包含一个整数n,表示数组的长度。
第二行包含n个整数,表示数组的元素。
3. 输出格式:输出一个整数,表示数组中没有出现的最小的正整数。
4. 样例输入:53 4 1 2 55. 样例输出:66. 问题分析:首先,我们需要理解题目要求我们找出数组中缺失的最小正整数。
这意味着我们需要检查数组中的每个元素,并确定最小的正整数是否在数组中。
7. 算法描述:- 遍历数组,使用一个哈希集合记录出现的数字。
- 从1开始,检查每个正整数是否在哈希集合中,直到找到不在集合中的最小正整数。
8. 代码实现:```pythondef find_missing_positive(nums):seen = set()for num in nums:if num <= 0:continuewhile num in seen or num > len(nums):num += 1seen.add(num)return min(set(range(1, len(nums) + 1)) - seen)```9. 测试用例:- 输入:[3, 4, -1, 1]- 输出:210. 答案解析:在给定的测试用例中,数组[3, 4, -1, 1]中没有出现的最小正整数是2。
这是因为-1不是正整数,所以可以忽略。
数组中已经出现了1和3,所以下一个最小的正整数就是2。
11. 注意事项:- 确保数组中的元素是整数。
- 考虑数组中可能包含0或负数的情况。
- 算法的时间复杂度应尽可能低。
12. 扩展思考:- 如果数组非常大,如何优化算法?- 如果数组中的元素可以是浮点数,算法应该如何修改?13. 参考答案:- 针对大数组,可以考虑使用更高效的数据结构,如平衡二叉搜索树。
- 如果元素是浮点数,需要先将其转换为整数,然后再进行处理。
整理出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 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 31 2 100 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 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, JianqiangSourceRecommendJGShining代码:#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 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 Input5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1Sample Output13.333 31.500AuthorCHEN, 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]){ 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 Again Problem 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.。
(完整版)浙江工业大学ACM
浙江工业大学ACM部分程序解读●程序设计导引及在线实践●最重要的是边界:输入的边界和输出的边界:●if(n==0)cout<〈” 0-—>0”<<endl;//一定要考虑最特殊的情况●cout.precision(3); cout<<fixed<〈sum<〈endl; printf("%.3f\n",sum);//以后的采用●时刻请注意清空,向量及变量Zjut1002数字游戏#include <iostream〉using namespace std;int divide(int x, int y,int& k){if(x==y) return k;if(x%y==0){k++;return divide(x/y,y,k);}return 1;}int main(){int n,i,x,k,ki;while(scanf(”%d",&n)&&n!=0){ki=k=1;x=n;for(i=2; i<n; i++)if(n%i==0){k=divide(n,i,k);if(k!=1){x=i;ki=k;//importantk=1;//importantcontinue;}}//outif(ki==1)printf("%d %d\n”,0,0);elseprintf("%d %d\n”,x,ki);}return 0;}1010诡秘的余数#include <iostream>#include <string>using namespace std;int quyu(string s,int n){int m=0;for(int i=0;i〈s.size();i++){m=(m*10+s[i]-48)%n;}return m;}int main(){string s;int n;while(cin〉>s>〉n){cout〈〈quyu(s,n)<〈endl;}return 0;}1015简单的事情#include〈iostream〉using namespace std;int main(){int n,m,i,j,k;long nn;while(cin〉〉n〉〉m){if(n==m)cout〈<1〈<endl;else{if(m>n/2){m=n-m;}nn=1;for(i=n,j=0,k=1;j〈m;i——,j++,k++){nn=nn*i/k;}cout<<nn<〈endl;}}return 0;}1016折纸游戏#include 〈iostream>using namespace std;int main(){int n,m,i,j,k,s,t;int a[100][100];cin>〉n;for(i=0;i<n;i++){cin〉〉m;for(j=1;j<=m;j++){for(k=1;k<=m;k++){cin〉〉a[j][k];}}if(m%2==0){for(j=1;j<=m;j++){t=1;s=m;while(t〈m){if(a[j][t]!=a[j][s]){cout<<”no"〈<endl;goto RL;}t++;s-—;}}for(j=1;j〈=m;j++){t=1;s=m;while(t<m){if(a[t][j]!=a[s][j]){cout<<"no”〈〈endl;goto RL;}t++;s--;}}}else{for(j=1;j<=m;j++){t=1;s=m;while(t!=(m+1)/2){if(a[j][t]!=a[j][s]){cout<〈”no”〈<endl;goto RL;}t++;s--;}}for(j=1;j<=m;j++){t=1;s=m;while(t!=(m+1)/2){if(a[t][j]!=a[s][j]){cout〈〈”no"〈<endl;goto RL;}t++;s--;}}}cout〈〈”yes”<<endl;RL:continue;}return 0;}1021ACMICPC#pragma warning(disable:4786)#include <iostream>#include 〈string>#include 〈map〉using namespace std;int max_sum(int a[],int n,int &besti,int &bestj){int i,j,tsum,sum;tsum=0;sum=0;besti=0;bestj=0;i=1;for(j=1;j〈=n;j++){tsum=tsum+a[j];if(tsum>sum){sum=tsum;besti=i;bestj=j;}else if(tsum〈0){i=j+1;tsum=0;}}return sum;}int main(){map<char,int> m;int a[1002];string s;int i,n,j,k,ii,jj,flag,min;m['A']=—13;m['G’]=—7;m[’M']=-1;m['R’]=4;m[’B']=—12;m[’H']=—6;m[’N']=0;m[’S']=5;m['C’]=—11;m['I']=-5;m['O']=1;m[’T’]=6;m['D’]=-10;m['J']=—4;m['P']=2;m['U']=7;m[’E']=-9;m[’K']=-3;m['Q’]=3;m[’V']=8;m[’F’]=—8;m[’L’]=-2;m[’W’]=9;m[’X’]=10;m['Y']=11;m[’Z’]=12;cin>>n;for(i=0;i<n;i++){cin〉〉s;k=1;flag=0;for(j=0;j<s.size();j++){a[k++]=m[s[j]];if(a[k-1]〉0){//一定是k-1flag=1;}}if(flag==0){min=a[1];for(j=2;j〈k;j++){if(a[j]>min){min=a[j];cout<<a[j]〈〈” ”;}}cout〈<min〈<endl;}else{cout〈<max_sum(a,k—1,ii,jj)<〈endl;//cout<<ii<<” "〈〈jj〈<endl;}flag=0;}return 0;}1022IOI#pragma warning(disable:4786)#include <iostream>#include <string〉#include <algorithm>#include <set>using namespace std;int main(){int n,i;string s,ss;set<string〉 sss;set〈string>::iterator it;cin>>n;for(i=0;i〈n;i++){cin>>s;sss.clear();ss=s;reverse(ss.begin(),ss.end());if(ss==s)sss。
(完整word版)杭州电子科技大学计算机网络试卷+答案
(完整word版)杭州电子科技大学计算机网络试卷+答案杭州电子科技大学学生考试卷( B )卷考试课程计算机网络及应用考试日期年月日成绩课程号B0503450 教师号任课教师姓名胡昔祥考生姓名学号(8位)年级专业(答案写在第四页上,写明学生姓名、学号、班级)一单项选择(每空2分,共40分)1、以下给出的地址中,属于子网192.168.15.19/28 的主机地址是________ 。
A、192.168.15.17B、192.168.15.14C、192.168.15.16D、192.168.15.312、关于ICMP的论述,不正确的是_______。
A、ICMP协议典型用途是差错报告B、ICMP报文是封装在IP分组中,因此它和TCP协议一样都是传输层协议C、ping程序实际上向目标主机发送了一个ICMP类型8编码0的报文D、当某IP分组TTL为零时,路由器将丢弃改分组并发送一个ICMP报告给源主机3、关于DHCP的论述,正确的是____________。
A、DHCP只能可动态获取一个IP地址B、DHCP只能可动态获取一个IP地址和它的子网掩码C、DHCP只能可动态获取一个IP地址和默认网关D、DHCP可动态获取一个IP地址、以及它的子网掩码、默认网关和DNS服务器地址4、下列IP地址中为有效私有地址的是__________。
A、201.1.2.1B、110.1.2.1C、192.168.1.2D、172.161.2.35、TCP连接建立的三次握手过程是_________。
A、TCP RST, TCP SYNACK,TCP ACKB、TCP SYN, TCP FINACK, TCP ACKC、TCP SYN, TCP SYNACK, TCP ACKD、TCP PSH, TCP SYNACK, TCP ACK6、滑动窗口协议中,发送窗口保存的是_____________。
A、可发送的帧序号B、可接收的帧序号C、不可发送的帧序号D、不可接收的帧序号7、在TCP拥塞控制机制中,当拥塞窗口小于阈值时,拥塞窗口呈_________。
ACM入门十题(杭电oj)
ACM入门(杭电oj)Hdu 1000#include<stdio.h>#include<stdlib.h>int main(){int a,b;while(scanf("%d%d",&a,&b)!=EOF){printf("%d\n",a+b);}}Hdu 1001#include<stdio.h>#include<stdlib.h>int main(){int n;while(scanf("%d",&n)!=EOF){printf("%I64d\n\n",(__int64)(1+n)*n/2); }}Hdu 1002#include<stdio.h>#include<string.h>#include<stdlib.h>char str1[1005],str2[10005];int main(){int ca,count=0;scanf("%d",&ca);while(ca--){scanf("%s%s",str1,str2);int a[1005],i,j;memset(a,0,sizeof(a));for(i=strlen(str1)-1,j=0;i>=0;i--,j++)a[j]=str1[i]-'0';for(i=strlen(str2)-1,j=0;i>=0;i--,j++){a[j]=a[j]+str2[i]-'0';a[j+1]=a[j+1]+a[j]/10;a[j]=a[j]%10;}count++;printf("Case %d:\n",count);printf("%s + %s = ",str1,str2); int flag=0;for(i=1004;i>=0;i--)if(flag||a[i]){printf("%d",a[i]);flag=1;}printf("\n");if(ca!=0) printf("\n");}}Hdu 1003#include<stdio.h>#include<stdlib.h>int a[100005],sum[100005];int main(){int ca,count=0;scanf("%d",&ca);while(ca--){int n,i;scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d",&a[i]);sum[1]=a[1];int r=1,max=a[1];for(i=2;i<=n;i++){if(sum[i-1]>0){sum[i]=sum[i-1]+a[i];if(sum[i]>max){max=sum[i];r=i;}}else{sum[i]=a[i];if(sum[i]>max){max=sum[i];r=i;}}}count++;for(i=r-1;i>0;i--)if(sum[i]<0) break;printf("Case %d:\n",count);printf("%d %d %d\n",max,i+1,r); if(ca!=0) printf("\n");}}Hdu 1004#include<iostream>#include<algorithm>using namespace std;struct point{char c[50];}p[1005];int cmp(point p1,point p2){return strcmp(p1.c,p2.c)<0;}int main(){int n,i;while(scanf("%d",&n)!=EOF&&n) {for(i=0;i<n;i++)scanf("%s",p[i].c);sort(p,p+n,cmp);char res[100];strcpy(res,p[0].c);int num=1,ct=1;for(i=1;i<n;i++){if(strcmp(p[i].c,p[i-1].c)==0) num++; else num=1;if(num>ct){strcpy(res,p[i].c);ct=num;}}printf("%s\n",res);}}Hdu 1005#include<stdio.h>#include<stdlib.h>#include<string.h>int s[10][10],c[1000];int main(){c[1]=1;c[2]=1;int a,b,n;while(scanf("%d%d%d",&a,&b,&n)!=EOF){if(a==0&&b==0&&n==0) break;memset(s,0,sizeof(s));s[1][1]=1;int i;for(i=3;;i++){c[i]=(a*c[i-1]+ b*c[i-2])%7;if(s[c[i-1]][c[i]]!=0) break;s[c[i-1]][c[i]]=i-1;}/*for(int j=1;j<=i;j++)printf("%d ",c[j]);*/int m=s[c[i-1]][c[i]]-1;int len=i-1-s[c[i-1]][c[i]];//printf("%d %d\n",m,len);if(n<=m){printf("%d\n",c[n]);continue;}n=n-m;printf("%d\n",c[m+(n%len? n%len:len)]); }}Hdu 1008#include<stdio.h>#include<stdlib.h>int main(){int n;while(scanf("%d",&n)!=EOF&&n){int a,sum=0,st=0,m=n;while(n--){scanf("%d",&a);sum=sum+(a-st>0?(a-st)*6:(st-a)*4); st=a;}printf("%d\n",m*5+sum);}}Hdu 1012#include<stdio.h>int main(){printf("n e\n");printf("- -----------\n");printf("0 1\n");printf("1 2\n");printf("2 2.5\n");printf("3 2.666666667\n");printf("4 2.708333333\n");printf("5 2.716666667\n");printf("6 2.718055556\n");printf("7 2.718253968\n");printf("8 2.718278770\n");printf("9 2.718281526\n");}#include<stdlib.h>#include<string.h>#include<stdio.h>int main(){char s[1000];while(scanf("%s",s)!=EOF){if(strcmp(s,"0")==0) break;int i,t=0;for(i=0;i<strlen(s);i++)t=t+s[i]-'0';printf("%d\n",(t+8)%9+1);}}Hdu 1016#include<stdio.h>#include<string.h>inta[25]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61, 67,71,73};int num[25],flag[25],pri[100],n;void dsf(int x,int y){int i;if(y==n){if(pri[x+1]==1){for(i=1;i<n;i++)printf("%d ",num[i]);printf("%d\n",num[i]);}return;}for(i=2;i<=n;i++)if(flag[i]==0&&pri[x+i]==1){flag[i]=1;num[y+1]=i;dsf(i,y+1);flag[i]=0;}int main(){int ct=0,i;memset(pri,0,sizeof(pri));for(i=0;i<15;i++)pri[a[i]]=1;while(scanf("%d",&n)!=EOF){memset(flag,0,sizeof(flag)); printf("Case %d:\n",++ct);flag[1]=1;num[1]=1;dsf(1,1);printf("\n");}}。
杭电题目acm答案
1001 Sum Problem ............................................. 错误!未定义书签。
1089 A+B for Input-Output Practice (I) ...................... 错误!未定义书签。
1090 A+B for Input-Output Practice (II) ..................... 错误!未定义书签。
1091 A+B for Input-Output Practice (III) .................... 错误!未定义书签。
1092 A+B for Input-Output Practice (IV) ...................... 错误!未定义书签。
1093 A+B for Input-Output Practice (V) ...................... 错误!未定义书签。
1094 A+B for Input-Output Practice (VI) ..................... 错误!未定义书签。
1095 A+B for Input-Output Practice (VII) ..................... 错误!未定义书签。
1096 A+B for Input-Output Practice (VIII) ................... 错误!未定义书签。
2000 ASCII码排序............................................ 错误!未定义书签。
2001计算两点间的距离........................................ 错误!未定义书签。
2002计算球体积.............................................. 错误!未定义书签。
杭州电子科技大学acm答案
else s1+=i*i*i; } cout<<s2<<" "<<s1<<endl; } return 0;
} 题解:输入的两个区间 大小不一定
2008 数值统计
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20378 Accepted Submission(s): 10783
正确代码:
#include<iostream> #include<cmath> #include<iomanip> #define PI 3.1415927 using namespace std; int main() {
double r,v; while(cin>>r) { v=4*PI*r*r*r/3;
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
acm选拔试题及答案
acm选拔试题及答案# acm选拔试题及答案1. 问题描述:编写一个程序,计算给定整数序列中,连续子序列的最大和。
2. 输入格式:第一行包含一个整数 \( n \),表示序列的长度。
第二行包含 \( n \) 个整数,表示序列中的元素。
3. 输出格式:输出一个整数,表示连续子序列的最大和。
4. 示例:输入:```51 -234 -1```输出:```6```5. 问题分析:这个问题可以通过动态规划的方法来解决。
定义一个数组 `dp[i]` 来表示以第 `i` 个元素结尾的连续子序列的最大和。
6. 算法逻辑:- 初始化 `dp[0]` 为序列的第一个元素。
- 对于每个 `i`(从 1 到 `n-1`),`dp[i]` 可以通过 `dp[i-1] + nums[i]` 来更新,如果 `dp[i-1]` 是负数,则 `dp[i]` 应该等于`nums[i]`。
- 遍历序列,更新 `dp` 数组,同时记录最大和。
7. 代码实现:```pythondef max_subarray_sum(nums):n = len(nums)max_sum = nums[0]current_sum = nums[0]for i in range(1, n):current_sum = max(nums[i], current_sum + nums[i]) max_sum = max(max_sum, current_sum)return max_sum```8. 测试用例:- 输入:`[-2, 1, -3, 4, -1, 2, 1, -5, 4]`- 输出:`6`9. 答案解析:- 该测试用例中,连续子序列 `[4, -1, 2, 1]` 的和为 `6`,是所有可能子序列中的最大值。
10. 注意事项:- 考虑边界条件,如序列中所有元素都是负数的情况。
- 优化算法以处理大数据量的情况。
11. 附加说明:- 该问题也可以通过分治法或贪心算法来解决,但动态规划提供了一个更简洁且易于理解的解决方案。
(完整word版)杭电ACM试题答案
【杭电ACM1000】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 Output2# include <stdio.h>int main(){int a, b;while(scanf("%d%d", &a, &b)!=EOF)printf("%d\n", a+b);return 0;}【杭电ACM1001】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 Input1 100Sample Output1 5050# include <stdio.h>int main(){int n, i, sum = 0;while(scanf("%d", &n)!=EOF){for(i=1; i<=n; ++i)sum = sum + i;printf("%d\n\n", sum);sum = 0;}return 0;}【杭电ACM1002】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 Input2 1 2 112233445566778899 998877665544332211Sample OutputCase 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110#include<stdio.h>#include<string.h>int shu(char a){return (a-'0');}int main(){char a[1000],b[1000];int num[1001];int n,i,j=1,al,bl,k,t;scanf("%d",&n);while(n--){getchar();if(j!=1)printf("\n");scanf("%s",a);al=strlen(a);scanf("%s",b);bl=strlen(b);k=(al>bl)?al:bl;for(i=0;i<=k;i++)num[i]=0;t=k;for(k;al>0&&bl>0;k--){num[k]+=shu(a[--al])+shu(b[--bl]);if(num[k]/10){num[k-1]++;num[k]%=10;}}while(al>0){num[k--]+=shu(a[--al]);if(num[k+1]/10){num[k]++;num[k+1]%=10;}}while(bl>0){num[k--]+=shu(b[--bl]);if(num[k+1]/10){num[k]++;num[k+1]%=10;}}printf("Case %d:\n",j++);printf("%s + %s = ",a,b);for(i=0;i<=t;i++){if(i==0&&num[i]==0)i++;printf("%d",num[i]);}printf("\n");}return 0;}。
浙江大学acm答案完整版
求余运算给出S和M,求0*S%M,1*S%M,2*S%M......(M-1)*S%M能否组成一个集合包含0.1.。
M-1;(这个是原题意改造而来);算法:判断两个数是否互质;or 暴力解决其实暴力完全可以解决这个问题(⊙﹏⊙b),只是其中用数学方法更加高效,巧妙;证明如果S和M互质则满足题意:另G=gcd(S,M);则S=A*G,M=B*G;另X=K*S%M=K*S-T*M(T为整数,满足X属于0到M-1);X=K*A*G-T*B*G;因此取余后的整数一定是G的倍数,G只能取1才能满足条件;充分性的证明:(即当S与M互质,则0到M-1的S倍对M取余一定能遍历0到M-1)只需证明的是,该余数中两两之间互不相等;假设k*S和b*S对M取余相等(k和b∈[0,M),并且k和b不等);则k*S=q1*M+r=q2*M+r=b*S <==> (k-b)*S=M*(q1-q2);S与M互质,由上式子可得M|(k-b),与k和b∈[0,M),并且k和b不等矛盾;因此得证;另外,偶然看到一个很牛叉的辗转相除法;int gcd(int a,int b){while(b) b^=a^=b^=a%=b;return a;}此代码,很好很强大;把涉及位运算的交换的程序加入,便到得这段简洁高效的代码;注:A和B;经过A^=B^=A^=B,结果就得到A和B的交换//////////////////////////// 1000#include <stdio.h>int main(){int a,b,i,;scanf("%d",&a);for(i=1;i<=a;i++){ int sum=0;sum=sum+i;printf("%d\n",sum);}return 0;};1001;#include"stdio.h"int main(){unsigned _int64 n;unsigned _int64 temp;while(scanf("%I64u",&n)!=EOF) //是i 非L{temp=(1+n)*n/2;printf("%I64u\n\n",temp);}return 0;}//////////////////HDU ACM 1014 Uniform Generator 三月22nd, /showproblem.php?pid=1014这个题目是判断给定的步长和mod,判断所产生的随机数已经覆盖0~mod-1中所有的数,如果是,则说明所选的步长和mod是一个Good choice,否则为bad choice.需要懂得的基本内容为线性同余产生随机数,链接:/zh-cn/%E7%B7%9A%E6%80%A7%E5%90%8C%E9%A4%98%E6%96 %B9%E6%B3%95Problem DescriptionComputer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the formseed(x+1) = [seed(x) + STEP] % MODwhere '%' is the modulus operator.Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations.If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.InputEach line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).OutputFor each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either "Good Choice" or "Bad Choice" left-justified starting in column 25. The "Good Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message "Bad Choice". After each output test set, your program should print exactly one blank line.Sample Input3 515 2063923 99999Sample Output3 5 Good Choice15 20 Bad Choice63923 99999 Good Choice线性同余方法(LCG)是个产生伪随机数的方法。
acm考试题目及答案
acm考试题目及答案1. 题目:给定一个整数数组,找出数组中没有出现的最小的正整数。
答案:首先,我们可以遍历数组,将每个元素与它的索引对应起来,即如果数组中存在数字`i`,则将其与索引`i-1`对应。
然后,我们可以遍历数组,检查索引`i`是否与数组中第`i`个元素相等。
如果不相等,则索引`i`对应的值就是没有出现的最小正整数。
如果所有元素都与其索引对应,则没有出现的最小正整数为数组长度加1。
2. 题目:实现一个函数,检查一个链表是否为回文结构。
答案:我们可以将链表的前半部分反转,然后比较反转后的前半部分与后半部分是否相同。
如果相同,则链表是回文的;如果不相同,则不是。
具体步骤如下:首先找到链表的中点,然后反转前半部分链表,接着比较反转后的前半部分与后半部分是否相同,最后将前半部分链表再次反转回来。
3. 题目:给定一个只包含 '(' 和 ')' 的字符串,判断字符串是否有效。
答案:我们可以使用一个栈来解决这个问题。
遍历字符串中的每个字符,如果遇到'(',则将其压入栈中;如果遇到')',则检查栈是否为空,如果为空,则字符串无效;如果不为空,则弹出栈顶元素。
遍历结束后,如果栈为空,则字符串有效;如果栈不为空,则字符串无效。
4. 题目:找出一个无序数组中第k大的元素。
答案:我们可以使用快速选择算法来解决这个问题。
首先,选择一个元素作为基准,然后将数组分为两部分:一部分是大于基准的元素,另一部分是小于基准的元素。
根据基准的位置,我们可以确定第k大的元素是在基准的左边还是右边,然后递归地在相应的部分中寻找第k大的元素。
重复这个过程,直到找到第k大的元素。
5. 题目:给定一个字符串,找出其中不含有重复字符的最长子串的长度。
答案:我们可以使用滑动窗口的方法来解决这个问题。
维护一个窗口,记录窗口内字符的出现情况。
遍历字符串,如果遇到重复的字符,则移动窗口的左边界,直到窗口内没有重复的字符。
杭电acm答案无敌版
选修课考试作业2012 素数判定............................2014 青年歌手大奖赛_评委会打分3620172019 数列有序! ....2020 绝对值排序...2033 人见人爱A+B2039 三角形.......2040 亲和数....... 姓名:春杰级:电商1434 计4243 45 4850511001 Sum P roblemP roblem Descri pti onHey, welcome to HDOJ(Ha ngzhou Dianzi Uni versity On li ne Judge).In this p roblem, your task is to calculate SUM( n) = 1 + 2 + 3 + ... + n.InputThe input will con sist of a series of in tegers n, one in teger per line.Out putFor each case, out put SUM( n) in one line, followed by a bla nk line. You may assume the result will be in the range of 32-bit sig ned in teger.Sample Input1 100Sam ple Out put15050AuthorDOOM III解答:#i ncludevstdio.h>printf} }main (){int sum while { sumforn , i , sum;=0;((scanf ("%d",& n)!二 1)) =0; (i =0; i v=n; i ++) sum+=i ; ("%d\n\n" , sum);1089A+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 Output630AuthorlcyRecommendJGShininga , b; (scanf ("%d%d",& a,& b)!= EOF) ("%d\n" , a+b);解答:#i ncludevstdio.h>main (){intwhileprintf}1090A+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 Input2 1 510 20Sample Output630AuthorlcyRecommendJGShining解答:#i ncludevstdio.h> #defi ne M 1000 void mai n (){ int a , b, n, j [ M], i ; //printf(" please input n:\n"); scanf("%d",& n);for (i =0; i vn; i ++){ scanf ("%d%d",& a,& b); //printf("%d %d",a,b); j [i ]= a+b; } i while { =0; (i <n) printf iprintf ++; ("%d" , j [ i ]); ("\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 5 10 200 0Sample Output630AuthorlcyRecommendJGShining解答:#i ncludevstdio.h> main (){int scanf while{printfscanf}} 1092 A+B for Input-Output Practice(IV)P roblem Descri pti onYour task is to Calculate the sum of some in tegers.InputInput contains mult iple 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 input and this test case is not to be p rocessed.Out putFor each gro up of input in tegers you should out put their sum in one line, and with one line of out put for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sam ple Out put10,b;("%d %d",& a,& b); (!( a==0&&b==0)) ("%d\n" , a+b); ("%d %d",& a,&b);15AuthorIcyRecomme ndJGShi ning解答:#i nclude <stdio.h> int mai n{intwhile {()n , sum, i , t ;(seanf ("%d",& n)!= EOF&&n!= 0)sum for {scanfsum }printf =0;(i =0; i vn; i ++)("%d",& t);=sum+t ;("%d\n" , sum);1093A+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解答:#i ncludevstdio.h> main () {intsum while {n , a, b, i , j , sum;=0;(seanf ("%d\n" ,& n)!=- 1)for{(i =0; i vn; i ++)scanf for {scanfsum} printf sum("%d",& b);(j =0;j <b; j++)("%d",& a);+=a;("%d\n" , sum);=0;1094A+B for Input-Output Practice(VI)P roblem Descri pti onYour task is to calculate the sum of some in tegers.InputInput contains mult iple test cases, and one case one line. Each case starts with an in teger N, and the n N in tegers follow in the same line.Out putFor each test case you should out put the sum of N in tegers in one line, and with one line of out put for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sam ple Out put1015AuthorIcyRecomme ndJGShi ning解答:#i ncludevstdio.h> main () {intsum while {n , a, b, i , j , sum;=0;(seanf ("%d\n" ,& n)!=- 1)for{ scanf sum} printfsum(j =0;j <n; j++)("%d",& a);+=a;("%d\n" , sum);=0;[Copy to Clipboard ] [ Save to File]1095A+B for Inp ut-Out put P ractice(VII)P roblem Descri pti onYour task is to Calculate a + b.InputThe input will con sist of a series of p airs of in tegers a and b, sep arated by a sp ace, one p air of in tegers per line.Out putFor each p air of input in tegers a and b you should out put the sum of a and b, and followed by a bla nk line.Sample Input1 510 20Sam ple Out put630AuthorIcyRecomme ndJGShi ning156a , b; (scanf ("%d%d",& a,& b)!= EOF) ("%d\n\n" , a+b); A+B for Inp ut-Out put P ractice (VIII)P roblem Descri pti onYour task is to calculate the sum of some in tegers. InputInput contains an in teger N in the first line, and the n N lines follow. Each line starts with a in teger M, and the n M in tegers follow in the same line.Out putFor each group of input in tegers you should out put their sum in one line, and you must note that there is a bla nk line betwee n out pu ts.Sample Input34 1 2 3 45 1 2 3 4 53 1 2 3Sam pie Out put10解答:#i ncludevstdio.h>main (){intwhileprintf} 1096AuthorIcy Recomme nd JGShi ning解答: int mai n { intscanf ()getchar for l for { sca nf getchar for { a , b, i , j , l [ 1000], k; ("%d",& i );(); (j =1; j <=i ; j ++)[j ]= 0; (j =1; j <=i ; j ++) ("%d",& a); (); (k=1; k<=a; k++)scanf ("%d",& b); getchar (); l [j ]+= b; } for printf printf (j =1; j <=i-1; j ++) ("%d\n\n" , l [ j ]);("%d\n" , l [ i ]); 2000 ASCII 码排序P roblem Descri pti on 输入三个字符后,按各字符的 ASCII 码从小到大的顺序输出这三个字符。
杭电题目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) (13)1095 A+B for Input-Output Practice (VII) (14)1096 A+B for Input-Output Practice (VIII) (16)2000 ASCII码排序 (17)2001计算两点间的距离 (19)2002计算球体积 (21)2003求绝对值 (22)2004成绩转换 (23)2005第几天? (25)2006求奇数的乘积 (27)2007平方和与立方和 (29)2008数值统计 (30)2009求数列的和 (32)2010水仙花数 (33)2011多项式求和 (35)2012素数判定 (37)2014青年歌手大奖赛_评委会打分 (38)2015偶数求和 (40)2016数据的交换输出 (43)2017字符串统计 (45)2019数列有序! (46)2020绝对值排序 (48)2021发工资咯:) (50)2033人见人爱A+B (52)2039三角形 (54)2040亲和数 (55)姓名:郑春杰班级:电商1001学号:10105041341001 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 Input1100?Sample Output15050?AuthorDOOM III解答:#include<stdio.h>main(){int n,i,sum;sum=0;while((scanf("%d",&n)!=-1)){sum=0;for(i=0;i<=n;i++)sum+=i;printf("%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 20?Sample Output630?Authorlcy?RecommendJGShining解答:#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 20?Sample Output630?Authorlcy?RecommendJGShining解答:#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 0?Sample Output630?Authorlcy?RecommendJGShining解答:#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 5?Sample Output1015?Authorlcy?RecommendJGShining?解答:#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 5?Sample Output1015?Authorlcy解答:#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 5?Sample Output1015?Authorlcy?RecommendJGShining解答:#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]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 20?Sample Output630?Authorlcy?RecommendJGShining解答:#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 3?Sample Output10156?Authorlcy?RecommendJGShining解答: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码从小到大的顺序输出这三个字符。
杭电acm1002答案
#include<iostream>
#include<string>
using namespace std;
string result( string astr ,string bstr){
if(astr.length()>=bstr.length())
{
int index=0;
string astr;
string bstr;
string num[100];
for(int i=0;i<2*a;i=i+2)
{
cin>>num[i];
cin>>num[i+1];
}
for(int h=0;h<a*2;h=h+2){
string astr=num[h];
string bstr=num[h+1];
index=1;
}
else
{
astr[i]=(((astr[i]-'0')+(bstr[i]-'0')))+index+'0';
index=0;
}
}
if(index==1)
{
astr=string (index,'1')+astr;
}
}
return astr;
}
int main()
{
int a;
while(cin>>a){
int index=0;
bstr=string(astr.length()-bstr.length(),'0')+bstr;
完整word版杭电ACM试题答案
【杭电ACM1000】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 Output2# include <stdio.h>int main(){int a, b;while(scanf(%d%d, &a, &b)!=EOF)printf(%d\n, a+b);return 0;}【杭电ACM1001】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 Input1 100Sample Output1 5050# include <stdio.h>int main(){int n, i, sum = 0;while(scanf(%d, &n)!=EOF){for(i=1; i<=n; ++i)sum = sum + i;printf(%d\n\n, sum);sum = 0;}return 0;}【杭电ACM1002】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 integersare very large, that means you should not process them by using 32-bit integer. You may assumethe 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 ofthe 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 Input2 1 2 112233445566778899 998877665544332211Sample OutputCase 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110#include<stdio.h>#include<string.h>int shu(char a){return (a-'0');}int main(){char a[1000],b[1000];int num[1001];int n,i,j=1,al,bl,k,t;scanf(%d,&n);while(n--){getchar();if(j!=1)printf(\);scanf(%s,a);al=strlen(a);scanf(%s,b);bl=strlen(b);k=(al>bl)?al:bl;for(i=0;i<=k;i++)num[i]=0;t=k;for(k;al>0&&bl>0;k--){num[k]+=shu(a[--al])+shu(b[--bl]);if(num[k]/10){num[k-1]++;num[k]%=10;}}while(al>0){num[k--]+=shu(a[--al]);if(num[k+1]/10){num[k]++;num[k+1]%=10;}}while(bl>0){num[k--]+=shu(b[--bl]);if(num[k+1]/10){num[k]++;num[k+1]%=10;}}printf(Case %d:\n,j++);printf(%s + %s = ,a,b);for(i=0;i<=t;i++){if(i==0&&num[i]==0)i++;printf(%d,num[i]);}printf(\);}return 0;}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
【杭电ACM1000】
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
# include <stdio.h>
int main()
{
int a, b;
while(scanf(%d%d, &a, &b)!=EOF)
printf(%d\n, a+b);
return 0;
}
【杭电ACM1001】
Sum Problem
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
# include <stdio.h>
int main()
{
int n, i, sum = 0;
while(scanf(%d, &n)!=EOF)
{
for(i=1; i<=n; ++i)
sum = sum + i;
printf(%d\n\n, sum);
sum = 0;
}
return 0;
}
【杭电ACM1002】
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, 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 112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
#include<stdio.h>
#include<string.h>
int shu(char a)
{
return (a-'0');
}
int main(){
char a[1000],b[1000];
int num[1001];
int n,i,j=1,al,bl,k,t;
scanf(%d,&n);
while(n--)
{
getchar();
if(j!=1)
printf(\
);
scanf(%s,a);
al=strlen(a);
scanf(%s,b);
bl=strlen(b);
k=(al>bl)?al:bl;
for(i=0;i<=k;i++)
num[i]=0;
t=k;
for(k;al>0&&bl>0;k--)
{
num[k]+=shu(a[--al])+shu(b[--bl]);
if(num[k]/10)
{
num[k-1]++;
num[k]%=10;
}
}
while(al>0)
{
num[k--]+=shu(a[--al]);
if(num[k+1]/10)
{
num[k]++;
num[k+1]%=10;
}
}
while(bl>0)
{
num[k--]+=shu(b[--bl]);
if(num[k+1]/10)
{
num[k]++;
num[k+1]%=10;
}
}
printf(Case %d:\n,j++);
printf(%s + %s = ,a,b);
for(i=0;i<=t;i++)
{
if(i==0&&num[i]==0)
i++;
printf(%d,num[i]);
}
printf(\
);
}
return 0;
}。