整理的ACM题目及答案(共211页).pdf
完整word版杭电ACM部分题目答案

杭电ACM 1001 Sum Problem1089 1090 10911092 1093 1094A+B for Input-Output Practice (I) .A+B for Input-Output Practice(II) ... A+B for Input-OutputPractice (III)A+B for Input-Output Practice (IV) ...A+B for Input-Output Practice (V)A+B for Input-Output Practice (VI)A+B for Input-Output Practice (VII)A+B for Input-Output Practice (VIII)246891113109510962000 ASCII 码排序................ 2001 计算两点间的距离........... 2002 计算球体积................. 2003 求绝对值................... 2004 成绩转换................... 2005 第几天?................... 2006 求奇数的乘积............... 2007 平方和与立方和............. 2008 数值统计................... 2009 求数列的和................. 2010 水仙花数................... 2011 多项式求和................. 2012 素数判定................... 2014 青年歌手大奖赛_评委会打分2015 偶数求和................... 2016 数据的交换输出............. 2017 字符串统计................. 2019 数列有序! ................... 2020 绝对值排序................. 2021 发工资咯:)............... 2033 人见人爱A+B ................. 2039 三角形..................... 2040 亲和数 (14)1617 19202122242627283031 3334 36 384042434546 48 5051姓名:电商 1001学号:10105041341001 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 () {intwhile printf}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 ; //prin tf(" please input n:\n"); scanf ("%d",& n);for (i =0; i vn; i ++) { scanf ("%d%d",& a,& b); //prin tf("%d %d",a,b); j[i ]= a+b;} iwhile {=0;(i <n)printf i printf++;("%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 510 200 0Sample Output6 30AuthorlcyRecommendJGShining解答:#i ncludevstdio.h> main () {int scanf while { printf scanf } }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 Clip board ] [ 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 Input3Sam 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题库完整版

#include<stdio.h> int type(int); char week[7][10]={"saturday","sunday","monday","tuesday","wednesday","thursday","friday"}; int year[2]={365,366}; int month[2][12]={31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31}; int main(void) { int days,dayofweek; int i=0,j=0; while(scanf("%d",&days)&&days!=-1) { dayofweek=days%7; for(i=2000;days>=year[type(i)];i++) days-=year[type(i)]; for(j=0;days>=month[type(i)][j];j++) days-=month[type(i)][j]; printf("%d-%02d-%02d%s\n",i,j+1,days+1,week[dayofweek]); } return 0; } int type(int m) { if(m%4!=0||(m%100==0&&m%400!=0)) return 0; else return 1; }
登山
1.题目描述 五一到了,NUIST-ACM队组织大家去登 山观光,队员们发现山上一个有N个景点, 并且决定按照顺序来浏览这些景点,即 每次所浏览景点的编号都要大于前一个 浏览景点的编号。同时队员们还有另一 个登山习惯,就是不连续浏览海拔相同 的两个景点,并且一旦开始下山,就不 再向上走了。队员们希望在满足上面条 件的同时,尽可能多的浏览景点,你能 帮他们找出最多可能浏览的景点数么? 2.输入 Line 1: N (2 <= N <= 1000) 景点数 Line 2: N个整数,每个景点的海拔 3.输出 最多能浏览的景点数 4.样例输入 8 186 186 150 200 160 130 197 220 5.样例输出 4
ACM软件大赛之编程大赛题目(附部分答案)

ACM 软件大赛之编程大赛比赛注意事项:l 比赛时间为3小时(小时(180180分钟);比赛分两个阶段:第一阶段限时30分钟,完成公示的3题,第二阶段限时150分钟(事先完成第一阶段题目的小组可提前进入第二阶段); l 比赛第一阶段的3道题目将在前期宣传中告知参赛选手,比赛第二阶段的题目将由赛事主席当场公布竞赛题目;主席当场公布竞赛题目;l 前两阶段题目分为三个分值(前两阶段题目分为三个分值(55分、分、1010分、分、1515分),第一阶段3道公示题都为5分;第二阶段总共15道题,根据不同的难度分值不同,分别为5道5分题,分题,55道10分题,分题,55道15分题;第一阶段参赛队员不可参考任何相关资料;第二阶段参赛队员可以携带诸如书,如书,手册,程序清单等参考资料。
手册,程序清单等参考资料。
手册,程序清单等参考资料。
比赛过程中队员不得携带任何电子媒质的资料;参比赛过程中队员不得携带任何电子媒质的资料;参赛者可以选择自己擅长的语言(赛者可以选择自己擅长的语言(C,C++,JAVA C,C++,JAVA 等等)进行编写等等)进行编写l 考虑到大一和大二学生的知识掌握程度,大一参加选手一开始就会有10分的分数,最后总分是由所做题目及初始的10分相加得到。
分相加得到。
l 每组队员根据安排使用电脑,小组人数为两人的使用一台电脑,超过两人的使用两台电脑,每台的电脑配置完全相同;脑,每台的电脑配置完全相同;l 各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分;各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分; l 如在比赛中发现作弊等行为,将取消比赛资格。
如在比赛中发现作弊等行为,将取消比赛资格。
第一阶段公示题目:题目一:(5分) 打印以下图形,纵遵从字母顺序,行字符数遵从斐波那契数列ABCCDDD EEEEEFFFFFFFFGGGGGGGGGGGGG#include<iostream>int f(int x){int a = 1 , b = 0;int max_ = x;int sum = 0; for(int i = 0; i < max_ ; i++){sum = a + b;a = b;b = sum;}return sum;}void loop_print(int num,char chr){for(int i = 0; i < num ;i++)std::cout<<chr;std::cout<<"\n";}int main(){int line_max = 7;char chr = 'A';for(int line = 0; line < line_max; line++){loop_print(f(line+1),chr);chr++;}return 0;}题目二:(5分)有个电子钟,12点显示为12:00(即12小时制),那么请问一天24时间内,出现连续3个相同数字的钟点有几个?#include<iostream>using namespace std;bool check(int me){int h= me/100;int m= me-100*h;return h<=12&&m<=59&&h>0?true:false;//12小时制小时制}int main(){int me=0;int j(0);//总计数器总计数器while( me<1270){//max 12:59int t= me;int n[4];for(int i=0;i<4;i++){n[i]=t%10;t /= 10;}if(n[1]==n[2]&&(n[0]==n[1]||n[3]==n[1])&&check( me)){//cout<<n[3]<<n[2]<<":"<<n[1]<<n[0]<<"\n";//testj++;me++;}cout<<"total: "<<j*2<<endl;}题目三:(5分)10进制的四位数中有几个符合如下特征:将其分别表示为16进制、10进制、12进制,在每种状态下,分别将各个位上的数相加,能得到3个相等10进制数。
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试题及参考答案

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’而来,通过回溯每个顶点的永久标签就可以找出最长有向路。
c acm试题及答案

c acm试题及答案ACM(Association for Computing Machinery)是计算机科学领域的国际性学术组织,旨在推动计算机科学的发展与研究。
ACM竞赛是ACM组织举办的一项计算机算法编程竞赛,每年都有数以千计的计算机专业学生参加。
这篇文章将介绍一道ACM试题,并提供解答。
题目描述:给定一个整数数组nums,其中所有元素都是非负整数,现在要求你计算nums中连续子数组的最大和,并输出该最大和。
输入:第一行输入一个整数n,表示数组长度(1 <= n <= 10^5)。
第二行输入n个整数,表示数组nums的元素(元素范围为0 <= nums[i] <= 100)。
输出:输出一个整数,表示nums中连续子数组的最大和。
示例:输入:51 2 3 4 5输出:15解释:最大和子数组为[1, 2, 3, 4, 5],和为15。
解答:首先,我们可以使用一个变量maxSum来记录当前得到的最大和,初始化为0。
同时,我们还使用一个变量sum来记录当前连续子数组的和,初始化为0。
接下来,我们对数组nums进行遍历。
对于每一个元素nums[i],我们将其加到sum中,并判断sum是否大于0。
如果sum大于0,说明当前连续子数组的和对后面的结果是有正向贡献的,我们可以继续累加后面的元素。
如果sum小于等于0,说明当前连续子数组的和对后面的结果没有正向贡献,我们可以将sum重新置为0,重新开始计算连续子数组的和。
在遍历过程中,我们不断更新maxSum的值,保证其为当前得到的最大和。
最终,遍历结束后的maxSum即为所求的最大和。
下面是具体的实现代码:```cpp#include <iostream>#include <vector>#include <algorithm>using namespace std;int maxSubArray(vector<int>& nums) { int maxSum = 0;int sum = 0;for(int i = 0; i < nums.size(); i++) { sum += nums[i];if(sum > maxSum) {maxSum = sum;}if(sum <= 0) {sum = 0;}}return maxSum;}int main() {int n;cin >> n;vector<int> nums(n);for(int i = 0; i < n; i++) {cin >> nums[i];}int result = maxSubArray(nums);cout << result << endl;return 0;}```以上就是解答c 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;long long f(long long m, long long n){if(n==0) return 1;else return f(m-1,n-1)*m/n;}int main(){long long m,n;while(scanf("%I64d %I64d",&n,&m) && n+m){printf("%I64d\n",f(m+n,min(m,n)));}return 0;}1、北大ACM(1012)JosephTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 31213 Accepted: 11700 DescriptionThe Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.题目大意:编号为1,2…, n的n个人排成一圈,从第一个人开始,去掉后面的第m个人,在从第m+1个人开始去掉后面第m个人,以此类推。
ACM几道练习题及其答案

…………………………………………………………………………………………………….. 标题:HDU- 1001-Sum Problem代码:#include <stdio.h>#include <iostream>#include <algorithm>#include <ctype.h>using namespace std;int main(){//freopen("text.txt","r",stdin);int sum,i;while(scanf("%d",&i)!=EOF){if((i+1)%2==0)sum=(i+1)/2*i;elsesum=i/2*(i+1);printf("%d\n\n",sum);}//fclose(stdin);return 0;}…………………………………………………………………………………………………….. …………………………………………………………………………………………………….. 标题:HDU-2027-代码:#include <stdio.h>#include <iostream>#include <algorithm>#include <ctype.h>using namespace std;int main(){//freopen("text.txt","r",stdin);char c[101];int n,j,k,a,e,i,o,u;scanf("%d\n",&n);//注意这里for(j=1;j<=n;j++){a=e=i=o=u=0;gets(c);for(k=0;c[k]!='\0';k++){if(c[k]=='a')a++;if(c[k]=='e')e++;if(c[k]=='i')i++;if(c[k]=='o')o++;if(c[k]=='u')u++;}printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n",a,e,i,o,u);if(j<n)printf("\n");}//fclose(stdin);return 0;}…………………………………………………………………………………………………….. …………………………………………………………………………………………………….. 标题:HDU-2025- 查找最大元素代码:#include <stdio.h>#include <iostream>#include <algorithm>#include <ctype.h>using namespace std;int main(){//freopen("text.txt","r",stdin);char a[105],max='A';int i,n;while(scanf("%s",a)!=EOF){n=strlen(a);max='A';for(i=0;i<n;i++){if(a[i]>max) max=a[i];}for(i=0;i<n;i++){printf("%c",a[i]);if(a[i]==max)printf("(max)");}printf("\n");}//fclose(stdin);return 0;}…………………………………………………………………………………………………….. …………………………………………………………………………………………………….. 标题:HDU-1037-Keep on Truckin'代码:#include <stdio.h>#include <iostream>#include <algorithm>#include <ctype.h>using namespace std;int main(){//freopen("text.txt","r",stdin);int a,b,c,min;while(cin >> a >> b >> c){min = a>b?b:a;min = min>c?c:min;if(min<=168)printf("CRASH %d\n",min);elseprintf("NO CRASH\n");}//fclose(stdin);return 0;}…………………………………………………………………………………………………….. …………………………………………………………………………………………………….. 标题:HDU-2052-Picture代码:#include <stdio.h>#include <iostream>#include <algorithm>#include <ctype.h>using namespace std;int main(){//freopen("text.txt","r",stdin);int n,m,i;while(scanf("%d%d",&n,&m)!=EOF){printf("+");for(i=0;i<n;++i){printf("-");}printf("+\n");for(i=0;i<m;++i){printf("|");for(int j=0;j<n;++j){printf(" ");}printf("|\n");}printf("+");for(i=0;i<n;++i){printf("-");}printf("+\n\n");}//fclose(stdin);return 0;}…………………………………………………………………………………………………….. …………………………………………………………………………………………………….. 标题:HDU-1034-Candy Sharing Game代码:#include <stdio.h>#include <iostream>#include <algorithm>#include <ctype.h>using namespace std;const int MAXN=1000;int a[MAXN];int main(){//freopen("text.txt","r",stdin);int n;int i;while(scanf("%d",&n),n){for(i=0;i<n;i++) scanf("%d",&a[i]);int res=0;while(1){for(i=1;i<n;i++)if(a[i-1]!=a[i]) break;if(i>=n) break;res++;int temp=a[n-1]/2;for(i=n-1;i>0;i--){a[i]/=2;a[i]+=a[i-1]/2;}a[0]/=2;a[0]+=temp;for(i=0;i<n;i++)if(a[i]&1) a[i]++;}printf("%d %d\n",res,a[0]);}//fclose(stdin);return 0;}……………………………………………………………………………………………………..。
acm

数据范围:
T <= 20
1 <= r, R, H <= 100; 0 <= V <= 1000,000,000
r <= R
Output
对于每个测试用例,输出杯子中的水的高度。保留6位小数
Sample Input
1
100 100 100 3141562Sample Output
5 5
1 1 3 2
10 10
2 2 5 8
Sample Output
1
3
提示:图的遍历。
DATASECURITY
BEST
Sample Output
EELTTIUNSMLR
第六题(汽车到哪儿了)
Description长途坐车真难受,人们可能在车上早早睡着了,等到醒来的时候,车行驶到哪了?假如长途车经过一些城镇,现在知道长途车行驶路线上相邻的城镇之间的距离和车从出发时算已经行驶的距离,请计算出汽车行驶到哪个城镇或哪两个城镇之间Input第一行是一个整数T,代表接下来有多少组数据接下来是各组数据,每一组数据的第一行是两个整数N,M,分别表示城镇数目(包括起点和终点城镇)和要询问多少次到哪了。接下来一行有N-1个整数,第i个数表示路线上第i个城镇(城镇从1开始编号,并且起始点为第1号)和第i+1个城镇之间的距离。接下来一行有M个整数,表示M次询问到哪里时已经行驶的距离一组测试数据中,城镇数不会超过10000个,两个城镇之间的距离不会超过100,询问次数不会超过100000Output对于每一组数据中的每一次询问,输出一行。若汽车刚好行驶到某个城镇,则输出这个城镇的编号;若汽车行驶到两个城镇之间,输出两个城镇的编号,小编号先输出,大编号后输出,且用一个空格隔开Sample Input1
整理出ACM所有题目和答案解析

1000 A + B ProblemProblem DescriptionCalculate A + B.InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2AuthorHDOJ代码:#include<stdio.h>int main(){int a,b;while(scanf("%d %d",&a,&b)!=EOF)printf("%d\n",a+b);}1001 Sum ProblemProblem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.InputThe input will consist of a series of integers n, one integer per line.OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.Sample Input1100Sample Output15050AuthorDOOM III解答:#include<stdio.h>main(){int n,i,sum;sum=0;while((scanf("%d",&n)!=-1)){sum=0;for(i=0;i<=n;i++)sum+=i;printf("%d\n\n",sum);}}1002 A + B Problem IIProblem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.InputThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.OutputFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2112233445566778899 998877665544332211Sample OutputCase 1:1 +2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110AuthorIgnatius.L代码:#include <stdio.h>#include <string.h>int main(){char str1[1001], str2[1001];int t, i, len_str1, len_str2, len_max, num = 1, k; scanf("%d", &t);getchar();while(t--){int a[1001] = {0}, b[1001] = {0}, c[1001] = {0}; scanf("%s", str1);len_str1 = strlen(str1);for(i = 0; i <= len_str1 - 1; ++i)a[i] = str1[len_str1 - 1 - i] - '0';scanf("%s",str2);len_str2 = strlen(str2);for(i = 0; i <= len_str2 - 1; ++i)b[i] = str2[len_str2 - 1 - i] - '0';if(len_str1 > len_str2)len_max = len_str1;elselen_max = len_str2;k = 0;for(i = 0; i <= len_max - 1; ++i){c[i] = (a[i] + b[i] + k) % 10;k = (a[i] + b[i] + k) / 10;}if(k != 0)c[len_max] = 1;printf("Case %d:\n", num);num++;printf("%s + %s = ", str1, str2);if(c[len_max] == 1)printf("1");for(i = len_max - 1; i >= 0; --i){printf("%d", c[i]);}printf("\n");if(t >= 1)printf("\n");}return 0;}1005 Number SequenceProblem DescriptionA number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.Given A, B, and n, you are to calculate the value of f(n).InputThe input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.OutputFor each test case, print the value of f(n) on a single line.Sample Input1 1 31 2 100 0 0Sample Output25AuthorCHEN, ShunbaoSourceZJCPC2004RecommendJGShining代码:#include<stdio.h>int f[200];int main(){int a,b,n,i;while(scanf("%d%d%d",&a,&b,&n)&&a&&b&&n) {if(n>=3){f[1]=1;f[2]=1;for(i=3;i<=200;i++){f[i]=(a*f[i-1]+b*f[i-2])%7; if(f[i-1]==1&&f[i]==1)break;}i-=2;n=n%i;if(n==0)printf("%d\n",f[i]);elseprintf("%d\n",f[n]);}elseprintf("1\n");}return 0;}1008 ElevatorProblem DescriptionThe highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.InputThere are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.OutputPrint the total time on a single line for each test case.Sample Input1 23 2 3 1Sample Output1741AuthorZHENG, JianqiangSourceZJCPC2004RecommendJGShining代码:#include<stdio.h>int a[110];int main(){int sum,i,n;while(scanf("%d",&n)&&n!=0){for(i=1;i<=n;i++)scanf("%d",&a[i]);sum=0;a[0]=0;for(i=1;i<=n;i++){if(a[i]>a[i-1])sum+=6*(a[i]-a[i-1]);elsesum+=4*(a[i-1]-a[i]);sum+=5;}printf("%d\n",sum);}return 0;}1009 FatMouse' TradeProblem DescriptionFatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.InputThe input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followedby two -1's. All integers are not greater than 1000.OutputFor each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.Sample Input5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1Sample Output13.333 31.500AuthorCHEN, YueSourceZJCPC2004RecommendJGShining代码:#include<stdio.h>#include<string.h>#define MAX 1000int main(){int i,j,m,n,temp;int J[MAX],F[MAX];double P[MAX];double sum,temp1;scanf("%d%d",&m,&n);while(m!=-1&&n!=-1){sum=0;memset(J,0,MAX*sizeof(int));memset(F,0,MAX*sizeof(int));memset(P,0,MAX*sizeof(double));for(i=0;i<n;i++){ scanf("%d%d",&J[i],&F[i]); P[i]=J[i]*1.0/((double)F[i]); }for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(P[i]<P[j]){temp1=P[i]; P[i]=P[j]; P[j]=temp1;temp=J[i]; J[i]=J[j]; J[j]=temp;temp=F[i]; F[i]=F[j]; F[j]=temp;}}}for(i=0;i<n;i++){if(m<F[i]){ sum+=m/((double)F[i])*J[i]; break; }else { sum+=J[i]; m-=F[i]; }}printf("%.3lf\n",sum); scanf("%d%d",&m,&n);}return 0;}1021 Fibonacci AgainProblem DescriptionThere are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000).OutputPrint the word "yes" if 3 divide evenly into F(n).Print the word "no" if not.Sample Input12345Sample OutputnonoyesnononoAuthorLeojayRecommendJGShining#include<stdio.h>int main(){long n;while(scanf("%ld",&n) != EOF) if (n%8==2 || n%8==6)printf("yes\n");elseprintf("no\n");return 0;}1089 A+B for Input-Output Practice(I)Problem DescriptionYour task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners.You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input1 510 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n",a+b);}1090 A+B for Input-Output Practice(II)Problem DescriptionYour task is to Calculate a + b.InputInput contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input21 510 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>#define M 1000void main(){int a ,b,n,j[M],i;//printf("please input n:\n");scanf("%d",&n);for(i=0;i<n;i++){scanf("%d%d",&a,&b);//printf("%d %d",a,b);j[i]=a+b;}i=0;while(i<n){printf("%d",j[i]);i++;printf("\n");}}1091 A+B for Input-Output Practice(III)Problem DescriptionYour task is to Calculate a + b.InputInput contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input1 510 200 0Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int a,b;scanf("%d %d",&a,&b);while(!(a==0&&b==0)){printf("%d\n",a+b);scanf("%d %d",&a,&b);}}1092 A+B for Input-Output Practice(IV)Problem DescriptionYour task is to Calculate the sum of some integers.InputInput contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecommendJGShining解答:#include <stdio.h>int main(){int n,sum,i,t;while(scanf("%d",&n)!=EOF&&n!=0){sum=0;for(i=0;i<n;i++){scanf("%d",&t);sum=sum+t;}printf("%d\n",sum);}}1093 A+B for Input-Output Practice(V)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.Sample Input24 1 2 3 45 1 2 3 4 5Sample Output1015Authorlcy解答:#include<stdio.h>main(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(i=0;i<n;i++){scanf("%d",&b);for(j=0;j<b;j++){scanf("%d",&a);sum+=a;}printf("%d\n",sum);sum=0;}}}1094 A+B for Input-Output Practice(VI)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.OutputFor each test case you should output the sum of N integers in one line, and with one line of output for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(j=0;j<n;j++){scanf("%d",&a);sum+=a;}printf("%d\n",sum);sum=0;}}[ Copy to Clipboard ][ Save to File]1095 A+B for Input-Output Practice(VII)Problem DescriptionYour task is to Calculate a + b.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.Sample Input1 510 20Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n\n",a+b);}1096 A+B for Input-Output Practice(VIII)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.Sample Input34 1 2 3 45 1 2 3 4 53 1 2 3Sample Output10156AuthorlcyRecommendJGShining解答:int main(){int a,b,i,j,l[1000],k;scanf("%d",&i);getchar();for(j=1;j<=i;j++)l[j]=0;for(j=1;j<=i;j++){scanf("%d",&a);getchar();for(k=1;k<=a;k++){scanf("%d",&b);getchar();l[j]+=b;}}for(j=1;j<=i-1;j++)printf("%d\n\n",l[j]);printf("%d\n",l[i]);}1176 免费馅饼Problem Description都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。
河南acm试题及答案

河南acm试题及答案河南ACM试题及答案1. 问题描述编写一个程序,计算给定整数序列中,满足条件的子序列数量。
条件为:子序列中任意两个相邻元素的差的绝对值不超过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基础试题及答案

acm基础试题及答案1. 题目:给定一个整数数组,请找出数组中第二大的数。
答案:我们可以使用排序的方法,将数组从小到大排序,然后数组中的倒数第二个数就是第二大的数。
或者使用一次遍历的方法,首先初始化两个变量,一个用来存储最大值,一个用来存储第二大的值。
遍历数组,每次比较当前元素与最大值,如果当前元素大于最大值,则更新第二大的值为最大值,并将当前元素赋给最大值;如果当前元素小于最大值但大于第二大的值,则更新第二大的值。
2. 题目:实现一个函数,计算一个字符串中字符出现的次数。
答案:可以使用哈希表来实现,遍历字符串中的每个字符,将其作为键值对存储在哈希表中,键是字符,值是该字符出现的次数。
遍历结束后,哈希表中存储的就是每个字符出现的次数。
3. 题目:给定一个链表,删除链表的倒数第n个节点,并且返回新的链表头节点。
答案:可以使用双指针的方法,首先初始化两个指针,都指向链表的头节点。
然后移动第一个指针,移动n步,此时第一个指针指向倒数第n个节点的前一个节点。
接着同时移动两个指针,直到第一个指针到达链表的末尾,此时第二个指针指向的节点就是需要删除的节点的前一个节点。
然后更新第二个指针的next指针,使其指向第二个指针的next节点的next节点,最后返回链表的头节点。
4. 题目:编写一个函数,判断一个整数是否是回文数。
回文数是指正序和倒序读都一样的数。
答案:首先将整数转换为字符串,然后使用双指针的方法,一个指针从字符串的开始位置,一个指针从字符串的结束位置,向中间移动。
如果两个指针指向的字符不相等,则该整数不是回文数。
如果遍历结束后没有发现不相等的字符,则该整数是回文数。
5. 题目:给定一个字符串,找出其中不含有重复字符的最长子串的长度。
答案:可以使用滑动窗口的方法,维护一个哈希表记录窗口内字符的出现情况,以及一个变量记录不含有重复字符的最长子串的长度。
遍历字符串,每次移动窗口的右端点,如果当前字符不在窗口内,则更新最长子串的长度,并将字符添加到哈希表中。
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.字母重排输入一个字典(用******结尾),然后再输入若干单词。
ACM软件大赛之编程大赛题目(附部分答案)

ACM软件大赛之编程大赛比赛注意事项:比赛时间为3小时(180分钟);比赛分两个阶段:第一阶段限时30分钟,完成公示的3题,第二阶段限时150分钟(事先完成第一阶段题目的小组可提前进入第二阶段);比赛第一阶段的3道题目将在前期宣传中告知参赛选手,比赛第二阶段的题目将由赛事主席当场公布竞赛题目;前两阶段题目分为三个分值(5分、10分、15分),第一阶段3道公示题都为5分;第二阶段总共15道题,根据不同的难度分值不同,分别为5道5分题,5道10分题,5道15分题;第一阶段参赛队员不可参考任何相关资料;第二阶段参赛队员可以携带诸如书,手册,程序清单等参考资料。
比赛过程中队员不得携带任何电子媒质的资料;参赛者可以选择自己擅长的语言(C,C++,JAVA等等)进行编写考虑到大一和大二学生的知识掌握程度,大一参加选手一开始就会有10分的分数,最后总分是由所做题目及初始的10分相加得到。
每组队员根据安排使用电脑,小组人数为两人的使用一台电脑,超过两人的使用两台电脑,每台的电脑配置完全相同;各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分;如在比赛中发现作弊等行为,将取消比赛资格。
第一阶段公示题目:题目一:(5分)打印以下图形,纵遵从字母顺序,行字符数遵从斐波那契数列ABCCDDDEEEEEFFFFFFFFGGGGGGGGGGGGG#in clude<iostream> int f(int x){int a = 1 , b = 0; int max_ = x;int sum = 0;for(int i = 0; i < max_ ; i++){ sum = a + b; a = b; b = sum;}return sum;}void loop_print(int num,char chr){for(int i = 0; i < num ;i++) std::cout<<chr;std::cout<<"\n";}int main(){int line_max = 7;char chr = 'A';for(int line = 0; line < line_max; line++){ loop_print(f(line+1),chr); chr++;}return 0;}题目二:(5 分)有个电子钟,12 点显示为12:00(即12 小时制),那么请问一天24 时间内,出现连续相同数字的钟点有几个?#include<iostream>using namespace std;bool check(int time){int h=time/100;int m=time-100*h;return h<=12&&m<=59&&h>0?true:false;//12 小时制}int main(){int time=0;int j(0);// 总计数器while(time<1270){//max 12:59int t=time;int n[4];for(int i=0;i<4;i++){n[i]=t%10;t /= 10;} if(n[1]==n[2]&&(n[0]==n[1]||n[3]==n[1])&&check(time)){//cout<<n[3]<<n[2]<<":"<<n[1]<<n[0]<<"\n";//test j++;} time++;}cout<<"total: "<<j*2 <<endl;}题目三:(5 分)10 进制的四位数中有几个符合如下特征:将其分别表示为16 进制、10 进制、12 进制,在每种状态下,分别将各个位上的数相加,能得到 3 个相等10 进制数。
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试题及答案# 美国ACM试题及答案1. 问题描述给定一个整数数组,找出数组中第k个最大的元素。
2. 输入格式第一行包含两个整数n和k,分别表示数组的大小和要找出的第k 个最大的元素。
第二行包含n个整数,表示数组的元素。
3. 输出格式输出第k个最大的元素。
4. 样例输入5 33 14 1 55. 样例输出46. 问题分析本题要求找出数组中的第k个最大元素,可以使用排序算法,但更高效的方法是使用快速选择算法。
7. 算法描述快速选择算法是基于快速排序的变种,其基本思想是:- 随机选择一个元素作为基准。
- 将数组分为两部分,一部分大于基准,一部分小于基准。
- 如果基准元素的索引正好是k-1,则找到了第k个最大元素。
- 如果基准元素的索引大于k-1,则在左侧数组中继续查找。
- 如果基准元素的索引小于k-1,则在右侧数组中继续查找。
8. 代码实现```pythondef quick_select(arr, k):if len(arr) == 1:return arr[0]pivot = arr[0]left = [x for x in arr[1:] if x < pivot]right = [x for x in arr[1:] if x >= pivot]if k <= len(left):return quick_select(left, k)elif k == len(left) + 1:return pivotelse:return quick_select(right, k - len(left) - 1)n, k = map(int, input().split())arr = list(map(int, input().split()))print(quick_select(arr, k))```9. 注意事项- 确保输入的k值在1到n之间。
- 快速选择算法的平均时间复杂度为O(n),但最坏情况下为O(n^2)。
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程序设计试题及参考答案

ACM程序设计试题及参考答案猪的安家Andy和Mary养了很多猪。
他们想要给猪安家。
但是Andy没有足够的猪圈,很多猪只能够在一个猪圈安家。
举个例子,假如有16头猪,Andy建了3个猪圈,为了保证公平,剩下1头猪就没有地方安家了。
Mary生气了,骂Andy没有脑子,并让他重新建立猪圈。
这回Andy建造了5个猪圈,但是仍然有1头猪没有地方去,然后Andy又建造了7个猪圈,但是还有2头没有地方去。
Andy都快疯了。
你对这个事情感兴趣起来,你想通过Andy建造猪圈的过程,知道Andy家至少养了多少头猪。
输入输入包含多组测试数据。
每组数据第一行包含一个整数n (n <= 10) – Andy 建立猪圈的次数,解下来n行,每行两个整数ai, bi( bi <= ai <= 1000), 表示Andy建立了ai个猪圈,有bi头猪没有去处。
你可以假定(ai, aj) = 1.输出输出包含一个正整数,即为Andy家至少养猪的数目。
样例输入33 15 17 2样例输出16答案:// 猪的安家.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream.h"void main(){int n;int s[10][2];bool r[10];char ch;cout<<"请输入次数:"<<endl;cin>>n;for (int i=0;i<n;i++){cout<<"请输入第"<<i+1<<"次的猪圈个数和剩下的猪:,用--分开,"<<endl;cin>>s[i][0]>>ch>>ch>>s[i][1];}for (i=0;i<10;i++)r[i]=true;for (int sum=1;;sum++){for (i=0;i<n;i++)r[i]=(sum%s[i][0]==s[i][1]);for (i=0;i<n;i++){if (r[i]==0)break;}if (i==n)break;}cout<<"猪至少有"<<sum<<"只。