20120422河南科技大学ACM培训题
大学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秒【T1】序号互换Dr.Kong 设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来。
单元格的行坐标是由数字编号的数字序号,而列坐标使用字符序号。
观察字母序号,发现第1列到第26列的字母序号分别为A,B,……,Z,接着,第27列序号为AA,第28列为AB,以此类推。
若给Dr.Kong的机器人卡多一个数字序号(比如32),它能很快算出等价的字母序号(即AF),若给机器人一个字母序号(比如AA),它也能很快算出等价的数字序号(27),你能不能与卡多比试比试,看谁能算得更快更准。
【标准输入】第一行:N 表示有多少组测试数据。
接下来N行,每行或者是一个正整数,或者是一个仅由大写字母组成的字符串。
【标准输出】对于每一行测试数据,输出一行。
如果输入为一个正整数序号,则输出等价的字母序号;如果输入为字符串,则输出等价的数字序号。
【约束条件】输入保证,所有数字序号和字母序号对应的数字序号均<=2*10^9【样例】【T2】节能Dr.kong 设计的机器人卡多越来越聪明。
最近市政府公司交给卡多一项任务,每天早晨5:00开始,它负责关掉ZK大道右侧上的所有路灯。
卡多每到早晨5:00准会在ZK大道上某盏灯的旁边,然后他开始关灯。
每盏灯都有一定的功率,机器人卡多有自觉的节能意识,它希望在关灯期间,ZK大道右侧上所有的路灯的耗电总量数是最少的。
机器人卡多以1m/s的速度行走。
假设关灯动作不需要花费额外的时间,因为当它通过某盏路灯时就顺手将灯关掉。
请编写程序,计算在给定路灯设置,灯泡功率以及机器人卡多的起始位置的情况下,卡多关灯期间,Zk大道上所有灯耗费的最小能量。
【标准输入】第一行N 表示ZK大道右侧路灯的数量(2<=N<=1000)第二行V 表示机器人卡多开始关灯的路灯号。
(1<=V<=N)接下来的N行中,每行包含两个空格隔开的整数D和W,用来描述每盏灯的参数D表示该路灯与ZK大道起点的距离(用米为单位来表示)W表示灯泡的功率,即每秒该灯泡所消耗的能量数。
acm试题及答案2
acm试题及答案2ACM试题及答案21. 问题描述:编写一个程序,计算给定整数序列中的最大子段和。
2. 输入格式:第一行包含一个整数N,表示序列的长度。
第二行包含N个整数,表示序列中的整数。
3. 输出格式:输出一个整数,表示序列中的最大子段和。
4. 样例输入:```51 -2 -34 -1```5. 样例输出:```6```6. 问题分析:该问题可以通过动态规划的方法来解决。
定义一个数组dp,其中dp[i]表示以第i个元素结尾的最大子段和。
状态转移方程为dp[i] = max(dp[i-1] + nums[i], nums[i])。
7. 算法实现:```pythondef maxSubArray(nums):n = len(nums)dp = [0] * ndp[0] = nums[0]max_sum = nums[0]for i in range(1, n):dp[i] = max(dp[i-1] + nums[i], nums[i])max_sum = max(max_sum, dp[i])return max_sum```8. 复杂度分析:时间复杂度为O(n),其中n为序列的长度。
空间复杂度为O(n)。
9. 测试用例:- 输入:`[3, -2, 4]`输出:`5`- 输入:`[-2, 1, -3, 4, -1, 2, 1, -5, 4]`输出:`6`10. 注意事项:- 确保输入的序列长度N大于等于1。
- 序列中的整数可以是负数或正数。
- 输出结果应该是一个整数。
11. 扩展思考:- 如何优化算法以减少空间复杂度?- 如果序列中的整数是浮点数,算法是否仍然有效?12. 参考答案:- 可以通过只使用一个变量来存储最大子段和,以及一个变量来存储当前子段和,从而将空间复杂度优化到O(1)。
- 如果序列中的整数是浮点数,算法仍然有效,但需要注意浮点数运算的精度问题。
整理出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竞赛基础训练题.docx
ACM竞赛基础训练题H【题目]】N皇后问题(八皇后问题的扩展)【题ri 2】排球队员站位问题【题H 3]把自然数N分解为若干个自然数之和。
【题H 4]把自然数N分解为若干个自然数之积。
【题H 5】马的遍历问题。
【题H 6】加法分式分解【题H 7】地图着色问题【题H 8]在n和的正方形屮放置长为2,宽为1的长条块,【题H 9】找迷宫的最短路径。
(广度优先搜索算法)【题Fl 10]火车调度问题【题H 11]农夫过河【题FI 12]七段数码管问题。
【题H 13]把1-8这8个数放入下图8个格中,要求相邻的格(横,竖,对角线)上填的数不连续. 【题FI 14]在4X4的棋盘上放置8个棋,要求每一行,毎一列上只能放置2个.【题H 15]迷宫问题.求迷宫的路径.(深度优先搜索法)【题H 16]一笔画问题【题H 17]城市遍历问题.【题H 18]棋子移动问题【题H 19]求集合元索问题(l,2x+l,3X+l类)【题RI N皇后问题(含八皇后问题的扩展,规则同八皇后):在N粒的棋盘上, 放置N个皇后,要求每一横行每一列,每一对角线上均只能放置一个皇后,问可能的方案及方案数。
const max=8;var i,j:integer;a: array [1.. max] of 0.. max; {放皇后数组}b:array[2.. 2*max] of boolean; {/对角线标志数组}c:array[-(max-1).. max-1 ] of boolean; {\对角线标志数组}col:array [1.. max] of boolean; {列标志数组}total: integer; {统计总数}procedure output; {输出}var i:inte^er;beginwrite (' No.' :4,' [', total+1:2,']');for i :=1 to max do write (a [i] :3) ;write(, ');if (to tal+1) mod 2 =0 t hen writ eln; inc (to tai);end;function ok(i, dep: integer) :boolean; {判断第dep 行第i 列可放否}begin ok:=false;if ( b[i+dep]二true) and ( c[dep-i]二true) {and (a[dep]=0)} and(col[i]=true) then ok:二trueend;procedure try (dep:integer);var i,j:integer;beginfor i:=l to max do 侮一行均有max种放法}i f ok (i, dep) then begi na [dep]:=i;b[i+dep]:二false; {/对角线已放标志}c[dep-i]:二false; {\对角线已放标志}col [i]:二false; {列已放标志}if dep二max then outputelse try (dep+1) ; {递归下一层}a[dep] :=0; {取走皇后,回溯}b[i+dep]:二true; {恢复标志数组}c[dep-i]:二true;col[i]:二true;end;end;beginfor i:=1 to max do begin a[i]:=0;col[i]:=true;end;for i:=2 to 2*max do b[i]:二true;for i:=-(max-1) to max~l do c[i]:=true;total:二0;try(l); writelntotal , total);end.【测试数据】n二8八皇后问题No.[ 1] 1 5 8 6 3 7 2 4 No. [ 2] 1 6 83 7 4 2 5 No.[ 3] 1 7 4 6 8 2 5 3 No. [ 4] 1 7 58 2 4 6 3 No.[ 5] 2 4 6 8 3 1 7 5 No. [ 6] 2 5 71 3 8 6 4 No.[ 7] 2 5 7 4 1 8 6 3 No. [ 8] 2 6 17 4 8 3 5 No.[ 9] 2 6 8 3 1 4 7 5 No. [10] 2 7 36 8 5 1 4No. [11] 2 7 5 8 1 4 6 3 No. [12] 2 8 61 3 5 7 4 No. [13] 3 1 7 5 8 2 4 6 No. [14] 3 5 28 1 7 4 6 No. [15] 3 5 2 8 6 4 7 1 No. [16] 3 5 71 4 2 8 6 No. [17] 3 5 8 4 1 7 2 6 No. [18] 3 6 25 8 1 7 4 No. [19] 3 6 2 7 1 4 8 5 No. [20] 3 6 27 5 1 8 4 No. [21] 3 6 4 1 8 5 7 2 No. [22] 3 6 42 8 5 7 1 No. [23] 3 6 8 1 4 7 5 2 No. [24] 3 6 81 5 7 2 4 No. [25] 3 6 8 2 4 1 7 5 No. [26] 3 7 28 5 1 4 6 No. [27] 3 7 2 8 6 4 1 5 No. [28] 3 8 47 1 6 2 5 No. [29] 4 1 5 8 2 7 3 6 No. [30] 4 1 58 6 3 7 2 No. [31] 4 2 5 8 6 1 3 7 No. [32] 4 2 73 6 8 1 5 No. [33] 4 2 7 3 6 8 5 1 No. [34] 4 2 75 1 8 6 3 No. [35] 4 2 8 5 7 1 3 6 No. [36] 4 2 86 1 3 5 7 No. [37] 4 6 1 5 2 8 3 7 No. [38] 4 6 82 7 1 3 5 No. [39] 4 6 8 3 1 7 5 2 No. [40] 4 7 18 5 2 6 3 No. [41] 47 3 8 2 5 1 6 No. [42] 4 7 52 6 1 3 8 No. [43] 4 7 5 3 1 6 8 2 No. [44] 4 8 13 6 2 7 5 No. [45] 4 8 1 5 7 2 6 3 No. [46] 4 8 53 1 7 2 6 No. [47] 5 1 4 6 8 2 7 3 No. [48] 5 1 84 2 7 3 6 No. [49] 5 1 8 6 3 7 2 4 No. [50] 5 2 46 8 3 1 7 No. [51] 5 2 47 3 8 6 1 No. [52] 5 2 61 7 48 3 No. [53] 5 2 8 1 47 3 6 No. [54] 5 3 16 8 2 4 7 No. [55] 5 3 1 7 28 6 4 No. [56] 5 3 84 7 1 6 2 No. [57] 5 7 1 3 8 6 4 2 No. [58] 5 7 14 2 8 6 3 No. [59] 5 7 2 4 8 1 3 6 No. [60] 5 7 26 3 1 4 8 No. [61] 5 7 2 6 3 1 8 4 No. [62] 5 7 41 3 8 6 2 No. [63] 5 8 4 1 3 6 2 7 No. [64] 5 8 41 7 2 6 3 No. [65] 6 1 5 2 8 3 7 4 No. [66] 6 2 71 3 5 8 4 No. [67] 6 2 7 1 4 8 5 3 No. [68] 6 3 17 5 8 2 4 No. [69] 6 3 1 8 4 2 7 5 No. [70] 6 3 18 5 2 4 7 No. [71] 6 3 5 7 1 4 2 8 No. [72] 6 3 58 1 4 2 7 No. [73] 6 3 7 2 48 1 5 No. [74] 6 3 72 8 5 1 4 No. [75] 6 3 7 4 1 8 2 5 No. [76] 6 4 15 8 2 7 3 No. [77] 6 4 2 8 5 7 1 3 No. [78] 6 4 71 3 5 2 8 No. [79] 6 4 7 1 8 2 5 3 No. [80] 6 8 24 1 7 5 3 No. [81] 7 1 3 8 6 4 2 5 No. [82] 7 2 41 8 5 3 6 No. [83] 7 2 6 3 1 4 8 5 No. [84] 7 3 16 8 5 2 4 No. [85] 7 3 8 2 5 1 6 4 No. [86] 7 4 25 8 1 3 6 No. [87] 7 4 2 8 6 1 3 5 No. [88] 7 5 31 6 8 2 4 No. [89] 8 2 4 1 7 5 3 6 No. [90] 8 2 53 1 7 4 6 No. [91] 8 3 1 6 2 5 7 4 No. [92] 8 4 13 6 2 7 5 total:92 对于N皇后:【题H 】排球队员站位问题I ----------------- 1图为排球场的平面图,其屮一、二、三、四、五、六为位 置编号, I |二、三、四号位置为前排,一、六、五号位为后排。
河南acm试题及答案
河南acm试题及答案河南ACM试题及答案1. 问题描述编写一个程序,计算给定整数序列中,满足条件的子序列数量。
条件为:子序列中任意两个相邻元素的差的绝对值不超过1。
2. 输入格式第一行包含一个整数N,表示序列的长度,1 <= N <= 100000。
第二行包含N个整数,表示序列中的元素,-1000000 <= 数字 <= 1000000。
3. 输出格式输出满足条件的子序列数量。
4. 样例输入```51 2 3 2 1```5. 样例输出```5```6. 问题分析本题需要对给定的整数序列进行遍历,使用动态规划的方法来计算满足条件的子序列数量。
设dp[i]表示以第i个元素结尾的满足条件的子序列数量,那么dp[i]的值可以通过dp[i-1](当前元素与前一个元素相等或相邻)和dp[i-2](当前元素与前两个元素相邻)计算得出。
7. 算法实现```pythondef countSubsequences(nums):dp = [0] * (len(nums) + 1)dp[0] = 1for i in range(1, len(nums) + 1):for j in range(i):if abs(nums[i-1] - nums[j-1]) <= 1:dp[i] += dp[j]return sum(dp)```8. 测试用例对于样例输入,程序应输出5,表示有5个满足条件的子序列。
9. 注意事项- 确保输入数据的格式正确。
- 考虑边界条件,如序列长度为1时的情况。
- 动态规划数组的大小应根据序列长度动态调整。
部分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计算球体积 (20)2003求绝对值 (21)2004成绩转换 (22)2005第几天? (24)2006求奇数的乘积 (26)2007平方和与立方和 (27)2008数值统计 (28)2009求数列的和 (30)2010水仙花数 (31)2011多项式求和 (33)2012素数判定 (34)2014青年歌手大奖赛_评委会打分 (36)2015偶数求和 (38)2016数据的交换输出 (40)2017字符串统计 (41)2019数列有序! (43)2020绝对值排序 (45)2021发工资咯:) (46)2033人见人爱A+B (48)2039三角形 (50)2040亲和数 (51)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 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解答:#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]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 Output10156lcyRecommendJGShining解答: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试题1.开灯问题有n盏灯,编号为1-n。
第1个人把所有的灯打开,第二个人按下所有编号为2的倍数的开关(这些灯被关掉),第3个人按下所有边后卫3的倍数的开关(其中关掉的灯将被打开,打开的灯将被关闭),以此类推。
一共有k个人,问最后有哪些灯开着?输入:n和k,输出开着的灯的编号。
K≤n≤1000.样例输入:7 3样例输出:1 5 6 7测试数据样例输入:10 4样例输出:1 4 5 6 7 8样例输入:10 5样例输出:1 4 6 7 8 10样例输入:15 6样例输出:1 4 7 8 10 11 12 13 15源码:#include"stdio.h"#include"string.h"#define MAXN 1000+10int a[MAXN];int main(){int i,j,n,k,first=1;memset(a,0,sizeof(a));scanf("%d%d",&n,&k);for(i=1;i<=k;i++)for(j=1;j<=n;j++)if(j%i==0) a[j]=!a[j]; for(i=1;i<=n;i++)if(a[i]){if(first)first = 0;elseprintf(" ");printf("%d",i);}printf("\n");return 0;}2.蛇形填数在n*n方阵里填入1,2,3…..n*n,要求填成蛇形。
例如n=4时方阵为:10 11 12 19 16 13 28 15 14 37 6 5 4上面的方阵中,多余的空格只是为了便于观察规律,不必严格输出。
n≤8.样例输入:4样例输出:10 11 12 19 16 13 28 15 14 37 6 5 4测试数据样例输入:3样例输出:6 9 25 4 3样例输入:6样例输出:16 17 18 19 20 1153****221214 29 36 33 22 3132****423412 27 26 25 24 511 10 9 8 7 6源码:#include"stdio.h"#include"string.h"#define MAXN 10int a[MAXN][MAXN]; int main(){int n,x,y,tot=0;scanf("%d",&n);memset(a,0,sizeof(a));tot= a[x=0][y=n-1] = 1;while(tot<n*n)< p="">{while(x+1while(y-1>=0 && !a[x][y-1]) a[x][--y] = ++tot; while(x-1>=0 && !a[x-1][y]) a[--x][y] = ++tot; while(y+1<="">for(x=0;x<n;x++)< p="">{for(y=0;y<="">printf("\n");}return 0;}3.字母重排输入一个字典(用******结尾),然后再输入若干单词。
20120422河南科技大学ACM培训题
1、最少钱币数:【问题描述】这是一个古老而又经典的问题。
用给定的几种钱币凑成某个钱数,一般而言有多种方式。
例如:给定了6种钱币面值为2、5、10、20、50、100,用来凑15元,可以用5个2元、1个5元,或者3个5元,或者1个5元、1个10元,等等。
显然,最少需要2个钱币才能凑成15元。
你的任务就是,给定若干个互不相同的钱币面值,编程计算,最少需要多少个钱币才能凑成某个给出的钱数。
【要求】【数据输入】输入可以有多个测试用例。
每个测试用例的第一行是待凑的钱数值M(1 <= M <= 2000,整数),接着的一行中,第一个整数K(1 <= K <= 10)表示币种个数,随后是K 个互不相同的钱币面值Ki(1 <= Ki <= 1000)。
输入M=0时结束。
【数据输出】每个测试用例输出一行,即凑成钱数值M最少需要的钱币个数。
如果凑钱失败,输出“Impossible”。
你可以假设,每种待凑钱币的数量是无限多的。
【样例输入】156 2 5 10 20 50 10011 2【样例输出】2Impossible2、蛇行矩阵【问题描述】蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。
【要求】【数据输入】本题有多组数据,每组数据由一个正整数N组成。
(N不大于100)【数据输出】对于每一组数据,输出一个N行的蛇形矩阵。
两组输出之间不要额外的空行。
矩阵三角中同一行的数字用一个空格分开。
行尾不要多余的空格。
【样例输入】5【样例输出】1 3 6 10 152 5 9 144 8 137 12113、敲七【问题描述】输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)【要求】【数据输入】一个整数N。
(N不大于30000)【数据输出】从小到大排列的不大于N的与7有关的数字,每行一个。
【样例输入】20【样例输出】714174、半数集问题问题描述:给定一个自然数n,由n开始可以依次产生半数集set(n)中的数如下:(1) n∈set(n);(2) 在n的左边加上一个自然数,但该自然数不能超过最近添加的数的一半;(3) 按此规则进行处理,直到不能再添加自然数为止。
ACM算法题以及答案
在做这些题目之前必须了解vector(数组),list(链表)、deque(双端队列)、queue(队列),priority_queue(优先队列)Stack(栈)、set(集合)、map(键值对),mutilset、mutilmap。
stack堆栈,没有迭代器,支持push()方法。后进先出,top()返回最顶端的元素,pop()剔除最顶元素
定义queue对象的示例代码如下:
queue<int> q1;
queue<double> q2;
queue的基本操作有:
入队,如例:q.push(x);将x接到队列的末端。
出队,如例:q.pop();弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
#include <vector>
vector属于std命名域的,因此需要通过命名限定,如下完成你的代码:
using std::vector; vector<int> v;
或者连在一起,使用全名:
std::vector<int> v;
建议使用全局的命名域方式:
using namespace std;
list不支持随机访问。所以没有 at(pos)和operator[]。
list对象list1, list2 分别有元素list1(1,2,3),list2(4,5,6) 。list< int>::iterator it;
list成员
说明
constructor
构造函数
destructor
析构函数
acm大学生程序试题及答案
acm大学生程序试题及答案1. 题目:字符串反转描述:给定一个字符串,编写一个函数来将字符串中的字符按相反的顺序重新排列。
输入:一个字符串输出:反转后的字符串答案:```pythondef reverse_string(s):return s[::-1]```2. 题目:寻找最大数描述:给定一个整数数组,找出数组中的最大数。
输入:一个整数数组输出:数组中的最大数答案:```pythondef find_max(nums):return max(nums)```3. 题目:两数之和描述:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数的索引(从1开始计数)。
输入:一个整数数组和一个目标值输出:两个数的索引,如果没有则返回空数组答案:```pythondef two_sum(nums, target):num_to_index = {}for i, num in enumerate(nums):complement = target - numif complement in num_to_index:return [num_to_index[complement] + 1, i + 1] num_to_index[num] = ireturn []```4. 题目:无重复字符的最长子串描述:给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
输入:一个字符串输出:最长子串的长度答案:```pythondef length_of_longest_substring(s):char_map = {}start = max_length = 0for end in range(len(s)):if s[end] in char_map:start = max(start, char_map[s[end]] + 1)char_map[s[end]] = endmax_length = max(max_length, end - start + 1)return max_length```5. 题目:整数转罗马数字描述:将一个整数转换为罗马数字。
ACM题库完整版
if(str[i]>='a'&&str[i]<='z') { str[i]=str[i]-32; } i++;
} puts(str); return 0; }
日历问题
1.题目描述 在我们现在使用的日历中, 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年 是例外,它们不是闰年。例如:1700, 1800, 1900 和 2100 不是闰年,而 1600, 2000 和 2400是闰年。 给定从公元2000年1月1日开始逝去的天数,你的任务是给出这一天是哪年哪月哪日星期几。 2.输入 输入包含若干行,每行包含一个正整数,表示从2000年1月1日开始逝去的天数。输入最后一行是?1, 不必处理。可以假设结果的年份不会超过9999。 3.输出 对每个测试样例,输出一行,该行包含对应的日期和星期几。格式为“YYYY-MM-DD DayOfWeek”, 其中 “DayOfWeek” 必须是下面中的一个: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" 或 "Saturday“。 4.样例输入 1730 1740 1750 1751 -1 5.样例输出 2004-09-26 Sunday 2004-10-06 Wednesday 2004-10-16 Saturday 2004-10-17 Sunday 6.提示 2000.1.1. 是星期六
整理出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编程比赛入门题目集
最少钱币数:【问题描述】这是一个古老而又经典的问题。
用给定的几种钱币凑成某个钱数,一般而言有多种方式。
例如:给定了6种钱币面值为2、5、10、20、50、100,用来凑 15元,可以用5个2元、1个5元,或者3个5元,或者1个5元、1个10元,等等。
显然,最少需要2个钱币才能凑成15元。
你的任务就是,给定若干个互不相同的钱币面值,编程计算,最少需要多少个钱币才能凑成某个给出的钱数。
【要求】【数据输入】输入可以有多个测试用例。
每个测试用例的第一行是待凑的钱数值M (1 <= M <= 2000,整数),接着的一行中,第一个整数K(1 <= K <= 10)表示币种个数,随后是K个互不相同的钱币面值Ki(1 <= Ki <= 1000)。
输入M=0时结束。
【数据输出】每个测试用例输出一行,即凑成钱数值M最少需要的钱币个数。
如果凑钱失败,输出“Impossible”。
你可以假设,每种待凑钱币的数量是无限多的。
【样例输入】156 2 5 10 20 50 10011 2【样例输出】2 ImpossibleFeli 的生日礼物【问题描述】Felicia 的生日是11月1日(和Kitty是同一天生的哦)。
于是Feli请来Kitty 一起过生日。
Kitty带来了最新款的“Kitty猫”玩具准备送给 Feli,不过她说,这份礼物可不是白送的。
Feli要帮她一个忙,才能够得到心仪已久的玩具。
Kitty 说,“Kitty猫”玩具已经卖出了n!个,n<=10^100 *_*,Kitty想知道确切的数字,而不是无聊的“一个数加个感叹号”。
Feli听了大吃一惊。
要知道,算出n!是一个无比艰巨的任务。
Feli告诉Kitty,就算Feli算出n!,Kitty也看不下去,因为当n=20 时,计算机的长整型已经存不下了(Kitty只能接受1-9之间的数字)。
于是Kitty说,你只要告诉我n!最后一位非0的数就可以了。
河南科技大学_ACM培训_试题
G国发行了n种不同面值的邮票,并且规定每张信封上最多只允许贴m张邮票。
连续邮资问题要求对于给定的n和m的值,给出邮票面值的最佳设计,使得可在1张信封上贴出从邮资1开始,增量为1的最大连续邮资区间。
例如,当n=5和m=4时,面值为(1,3,11,15,32)的5种邮票可以贴出邮资的最大连续邮资区间是1到70。
编程任务:对于给定的正整数m 和n,计算出邮票面值的最佳设计。
【要求】【数据输入】输入数据每一行给出2个正整数m和n的值(1<=n,m<=9),最后以00表示文件结束。
【数据输出】对于输以假定(ai,aj)=1.输出包含一个正整数,即为Andy家至少养猪的数目。
【样例输入】3315172【样例输出】162、【问题描述】给定n个点的坐标(x,y,z),且n<=50,从点1出发,怎么样才能走一条路径,访问每个点一次且仅一次,使走过的距离和最小?【要求】【数据输入】多组数据.第1行n,然后n行3个整数坐标【数据输出】每组一行,代表最小权和【样例输入】30001101-10【样例输出】3.4一本书的页码从自然数1开始顺序编码直到自然数n。
书的页码按照通常的习惯编排,每个页码都不含多余的前导数字0。
例如,第6页用数字6表示,而不是06或006等。
数字计数问题要求对给定书的总页码n,计算出书的全部页码中分别用到多少次数字0,1,2, (9)4、问题描述:给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数。
多重集S中重数最大的元素称为众数。
例如,S={1,2,2,2,3,5}。
多重集S的众数是2,其重数为3。
对于给定的由n个自然数组成的多重集S,编程计算S的众数及其重数。
河南科技大学ACM选拔赛试题
河南科技大学ACM-ICPC选拔赛试题承办:电子信息工程学院地点:开园校5区-511机房时间:2011年5月18日-比赛规则考试时间:3小时2:30~5:30)题目:共4题评分:以正确答题数多且完成用时少者为优胜1.中国象棋【问题描述】在中国象棋中,马是一个非常有用的棋子,它能从任意一点出发遍历整个棋盘。
请你编写一个程序,判断一个马能否从指定的起始位置经过一定的步数跳到另一个位置。
棋盘有9*10个交叉点,编号从(1、1)到(9.10),马只能能走日字型,跳的时候不能跳出棋盘。
【标准输入】有多个测试用例,输入第一行包含一个正整数K(0<K<1000),表示有K组测试数据。
每个测试用例包含一行为(SX、SY、EX、EY、N)(1<=N<=20),分别代表起始位置、结束位置和步数。
【标准输出】对于每个测试用例,输出一行,如果在N部内能走到,则输出“YES”,否则输出“NO”。
【样例输入】11 1 5 5 10【样例输出】YES2.栈【问题描述】栈是在计算机中经常用到的一种数据结构,它限制仅在表的一端进行插入和删除运算的线性表。
栈的修改是按后进先出的原则进行。
每次退栈的总是当前栈中“最新”的元素,即最后进栈的元素,而最先插入的是被放在栈的底部,要到最后才能删除。
请编写一个程序,能得到栈经过一系列的操作后的数据元素的个数。
【标准输入】有多个测试用例,输入第一行包含一个正整数K(0<K<1000),表示有K组测试数据。
每个测试用例第一行包含一个正整数M(1<=M<=10000),表示有M个对栈的操作,以下包含M个正整数N(1<=N<=3),1表示进栈操作,2表示出栈操作,3表示取栈顶元素操作。
(当前栈为空时出栈操作不做任何操作)。
【标准输出】对于每个测试用例,输出一行,即当前栈的元素个数。
【样例输入】251 2 3 1 142 2 1 1【样例输出】223.摆数字【问题描述】用火柴摆数字是大家可能玩过的一个小游戏,用火柴能摆出以下的十个数字,每个数字需要一定量的火柴,如7需要3根火柴,而8需要7根火柴,请编写一个小程序,计算使用N 根火柴所能摆出的最小数字和最大数字。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、最少钱币数:
【问题描述】
这是一个古老而又经典的问题。
用给定的几种钱币凑成某个钱数,一般而言有多种方式。
例如:给定了6种钱币面值为2、5、10、20、50、100,用来凑15元,可以用5个2元、1个5元,或者3个5元,或者1个5元、1个10元,等等。
显然,最少需要2个钱币才能凑成15元。
你的任务就是,给定若干个互不相同的钱币面值,编程计算,最少需要多少个钱币才能凑成某个给出的钱数。
【要求】
【数据输入】输入可以有多个测试用例。
每个测试用例的第一行是待凑的钱数值M(1 <= M <= 2000,整数),接着的一行中,第一个整数K(1 <= K <= 10)表示币种个数,随后是K 个互不相同的钱币面值Ki(1 <= Ki <= 1000)。
输入M=0时结束。
【数据输出】每个测试用例输出一行,即凑成钱数值M最少需要的钱币个数。
如果凑钱失败,输出“Impossible”。
你可以假设,每种待凑钱币的数量是无限多的。
【样例输入】
15
6 2 5 10 20 50 100
1
1 2
【样例输出】
2
Impossible
2、蛇行矩阵
【问题描述】
蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。
【要求】
【数据输入】本题有多组数据,每组数据由一个正整数N组成。
(N不大于100)
【数据输出】对于每一组数据,输出一个N行的蛇形矩阵。
两组输出之间不要额外的空行。
矩阵三角中同一行的数字用一个空格分开。
行尾不要多余的空格。
【样例输入】
5
【样例输出】
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
3、敲七
【问题描述】
输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)
【要求】
【数据输入】一个整数N。
(N不大于30000)
【数据输出】从小到大排列的不大于N的与7有关的数字,每行一个。
【样例输入】
20
【样例输出】
7
14
17
4、半数集问题
问题描述:
给定一个自然数n,由n开始可以依次产生半数集set(n)中的数如下:(1) n∈set(n);
(2) 在n的左边加上一个自然数,但该自然数不能超过最近添加的数的一半;(3) 按此规则进行处理,直到不能再添加自然数为止。
例如:set(6)={6,16,26,126,36,136},半数集set(6)中有6 个元素。
注意半数集
是多重集,对于给定的自然数n,编程计算半数集set(n)中的元素个数。
题目分析:
题目要求输入有多行,每行给出一个整数n (0 < n < 1000) ,输入以文件结束标志EOF 结束。
半数集set(n)中的元素个数即为所有半数集set(j) (j<=n/2)的元素个数之和加1(即将其自身添加到所求元素中)。
5、编辑距离问题
问题描述:
设A 和B 是2 个字符串。
要用最少的字符操作将字符串A 转换为字符串B。
这里所说的字符操作包括 (1)删除一个字符; (2)插入一个字符; (3)将一个字符改为另一个字符。
将字符串A变换为字符串B 所用的最少字符操作数称为字符串A到B 的编辑距离,记为d(A,B)。
试设计一个有效算法,对任给的2 个字符串A和B,计算出它们的编辑距离d(A,B)。
6、8皇后问题
在一个8×8的棋盘里放置8个皇后,要求这8个皇后两两之间互相都不“冲突”。
7、素数环问题
把从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数。