一些简单的acm题
- 格式:doc
- 大小:57.00 KB
- 文档页数:13
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数学竞赛试题通常涉及各种数学领域,包括但不限于代数、几何、概率统计和组合数学等。
以下是一些经典的ACM数学竞赛试题:
1. 平面上n个点的k距离和最小值问题:给定平面上n个点,对于每个点,计算它到其他所有点的距离,然后求出这些距离中的k个最小值。
问题是:如何有效地计算这k个最小值?
2.最长公共子序列问题:给定两个序列,找出它们的最长公共子序列。
例如,对于序列
A = [1, 2, 3, 4] 和
B = [2, 3, 4, 5],最长公共子序列是[2, 3, 4]。
3. 凸包问题:给定平面上的一组点,找到一个最小的凸多边形,使得这个多边形能够包含这组点中的所有点。
4. 最短路问题:给定一个有向图,其中每条边都有一个非负的权重,找出图中任意两点之间的最短路径。
5. 子集和问题:给定一个正整数数组和一个目标值,判断数组中是否存在和为目标值的两个非空子集。
例如,给定数组[1, 2, 3, 4] 和目标值7,判断是否存在两个子集,它们的和分别为7。
以上只是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题目、测试用例及参考答案汇编——一次ACM协会内部测试第一题:梦境是虚幻吗?时间限制:3000ms 内存限制:65535KB 难度:★★描述《盗梦空间》是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中的时间过得快得多,这里假设现实中的3分钟,在梦里就是1小时。
然而,Cobb他们利用强效镇静剂,可以从第一层梦境进入第二层梦境,甚至进入三层,四层梦境,每层梦境都会产生同样的时间加速效果。
那么现在给你Cobb在各层梦境中经历的时间,你能算出现实世界过了多长时间吗?比如,Cobb先在第一层梦境待了1个小时,又在第二层梦境里待了1天,之后,返回第一层梦境之后立刻返回了现实。
那么在现实世界里,其实过了396秒(6.6分钟)输入第一行输入一个整数T(0<=T<=100),表示测试数据的组数。
每组测试数据的第一行是一个数字M(3<=M<=100)随后的M行每行的开头是一个字符串,该字符串如果是"IN" 则Cobb向更深层的梦境出发了,如果是字符串"OUT"则表示Cobb从深层的梦回到了上一层。
如果是首字符串是"STAY"则表示Cobb在该层梦境中停留了一段时间,本行随后将是一个整数S表示在该层停留了S分钟(1<=S<=10000000)。
数据保证在现实世界中,时间过了整数秒。
输出对于每组测试数据,输出现实世界过的时间(以秒为单位)。
样例输入16INSTAY 60INSTAY 1440OUTOUT样例输出396测试输入106INSTAY 60INSTAY 1440OUTOUT6INININOUTOUTOUT7INININSTAY 0 OUTOUTOUT2INSTAY 203INSTAY 0 OUT3INSTAY 10 OUT4INSTAY 10 STAY 10 OUT5INSTAY 20 STAY 20 OUT STAY 120 10INSTAY 20 STAY 20 INSTAY 1440STAY 1440OUTSTAY 120OUTSTAY 11STAY 50测试输出39660306073209723000参考代码:#include<stdio.h>int main(){int n;char a[5];scanf("%d",&n);while(n--){int m,i,b=1,c,time=0;scanf("%d",&m);for(i=0;i<m;i++){scanf("%s",&a);if(a[0]=='I') b*=20;else if(a[0]=='S') {scanf("%d",&c);time+=c*60/b;} else if(a[0]=='O') b/=20;}printf("%d\n",time);}return 0;}第二题:独木舟过河时间限制:3000ms 内存限制:65535KB 难度:★★描述进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。
ACM选拔赛试题
注意:程序文件命名为:Test+题号
1、已知四位数3025有一个特殊性质:它的前两位数30和后两位数25的和是55,而55的平方刚好等于该数(55*55=3025),编写一个程序求出所有的具有这种性质的四位数。
2、有的三位数很独特,它们每位上的数字互不相同且都不大于7,特别是十位数正好是百位数和个位数之差,编写程序求所有这样的三位数。
3、某电台组织一次智力竞赛,计划奖励30人。
准备了50件奖品,得一等奖者可得3件,二等奖可得2件,三等奖可得1件。
希望把所有奖品都发到获奖者手中,请编程找出所有的设奖方案(即各等奖各有多少人)。
4、修改31743的某一位数字,使之成为823的倍数。
5、在自然数中,各位数字之和的11倍正好等于自身的自然数只有一个,编写程序请找出这个自然数。
6、小明的妈妈是负责分发单位工资的。
为了使分发时有足够的零钞,同时又尽量不使每个人领到的钱太零碎。
每个月她都要计算出各种面值的钞票(100元、50元、20元、10元、5元、2元、1元)各需多少张。
假设每个人的工资都是整数元,你能否为她设计一个程序,从键盘输入10个人的工资,再计算各种面值的钞票各需多少张?
7、4名专家对四款赛车进行评论:
A说:2号赛车是最好的。
B说:4号赛车是最好的。
C说:3号赛车不是最佳赛车。
D说:B说错了。
事实上,只有一款赛车最佳,且只有一名专家说对了,其他3人说错了,请编程输出最佳赛车的车号,以及哪一位专家说对了。
acm大学生程序设计试题题目一:最大公约数(GCD)题目描述:给定两个正整数,求它们的最大公约数(GCD)。
输入两个正整数a和b(1 <= a, b <= 10^9),求它们的最大公约数。
输入格式:两个正整数,以空格分隔。
输出格式:输出一个整数,表示输入两个正整数的最大公约数。
示例:输入:14 21输出:7思路和分析:最大公约数(GCD)可以使用欧几里得算法来求解,即辗转相除法。
具体的步骤如下:1. 用较大的数除以较小的数,将得到的余数作为新的较大数。
2. 再用新的较大数除以较小数,将得到的余数作为新的较大数。
3. 如此重复,直到两个数可以整除,此时较小的数就是最大公约数。
代码实现:```cpp#include <iostream>using namespace std;int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}int main() {int a, b;cin >> a >> b;int result = gcd(a, b);cout << result << endl;return 0;}```题目二:字符串反转题目描述:给定一个字符串,要求将其反转并输出。
输入一个字符串s(1 <= |s| <= 1000),输出该字符串的反转结果。
输入格式:一个字符串s,只包含大小写字母和数字。
输出格式:一个字符串,表示输入字符串的反转结果。
示例:输入:HelloWorld123输出:321dlroWolleH思路和分析:字符串反转可以使用双指针的方法来实现。
初始时,左指针指向字符串的开头,右指针指向字符串的末尾,然后交换左右指针所指向的字符,并向中间移动,直到左指针不小于右指针。
代码实现:```cpp#include <iostream>using namespace std;string reverseString(string s) {int left = 0, right = s.length() - 1; while (left < right) {swap(s[left], s[right]);left++;right--;}return s;}int main() {string s;cin >> s;string result = reverseString(s); cout << result << endl;return 0;}```题目三:字符串匹配题目描述:给定一个字符串s和一个模式串p,判断s中是否存在与p相匹配的子串。
Acm试题及答案Acm试题及答案1001 Sum Problem (2)1089 A+B for Input-Output Practice (I) (3) 1090 A+B for Input-Output Practice (II) (4) 1091A+B for Input-Output Practice (III) (6) 1092A+B for Input-Output Practice (IV) (7) 1093 A+B for Input-Output Practice (V) (9) 1094 A+B for Input-Output Practice (VI) (11) 1095A+B for Input-Output Practice (VII) (12) 1096 A+B for Input-Output Practice (VIII) (14) 2000 ASCII码排序 (15)2001计算两点间的距离 (16)2002计算球体积 (17)2003求绝对值 (18)2004成绩转换 (19)2005第几天? (20)2006求奇数的乘积 (22)2007平方和与立方和 (23)2008数值统计 (24)2009求数列的和 (25)2010水仙花数 (27)2011多项式求和 (28)2012素数判定 (29)2014青年歌手大奖赛_评委会打分 (31)2015偶数求和 (32)2016数据的交换输出 (34)2017字符串统计 (35)2019数列有序! (37)2020绝对值排序 (38)2021发工资咯:) (40)2033人见人爱A+B (41)2039三角形 (43)2040亲和数 (44)1001 Sum ProblemProblem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.InputThe input will consist of a series of integers n, one integer per line.OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.Sample Input1100Sample Output15050AuthorDOOM III解答:#includemain(){int n,i,sum;sum=0;while((scanf("%d",&n)!=-1)){sum=0;for(i=0;i<=n;i++)sum+=i;printf("%d\n\n",sum);}}1089 A+B for Input-Output Practice(I)Problem DescriptionYour task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners.You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input1 510 20Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n",a+b);}1090 A+B for Input-Output Practice(II)Problem DescriptionYour task is to Calculate a + b.InputInput contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input21 510 20Sample Output630AuthorlcyRecommendJGShining解答:#include#define M 1000void main(){int a ,b,n,j[M],i;//printf("please input n:\n");scanf("%d",&n);for(i=0;i<n;i++)< bdsfid="216" p=""></n;i++)<> {scanf("%d%d",&a,&b);//printf("%d %d",a,b);j[i]=a+b;}i=0;while(i<n)< bdsfid="224" p=""></n)<>{printf("%d",j[i]); i++;printf("\n");}}1091A+B for Input-Output Practice (III) Problem DescriptionYour task is to Calculate a + b.InputInput contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input1 510 200 0Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;scanf("%d %d",&a,&b);while(!(a==0&&b==0)){printf("%d\n",a+b);scanf("%d %d",&a,&b);}}1092A+B for Input-Output Practice (IV)Problem DescriptionYour task is to Calculate the sum of some integers.InputInput contains multiple test cases. Each test case contains a integer N, and then N integers followin the same line. A test case starting with 0 terminates the input and this test case is not to be processed.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecommendJGShining解答:#includeint main(){int n,sum,i,t;while(scanf("%d",&n)!=EOF&&n!=0)sum=0;for(i=0;i<n;i++)< bdsfid="290" p=""></n;i++)<>{scanf("%d",&t);sum=sum+t;}printf("%d\n",sum); }1093 A+B for Input-Output Practice (V)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.Sample Input24 1 2 3 45 1 2 3 4 5Sample Output1015Authorlcy解答:#includemain()int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(i=0;i<n;i++)< bdsfid="322" p=""></n;i++)<>{scanf("%d",&b);for(j=0;j<b;j++)< bdsfid="326" p=""></b;j++)<>{scanf("%d",&a);sum+=a;}printf("%d\n",sum); sum=0;}}}1094 A+B for Input-Output Practice(VI)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.OutputFor each test case you should output the sum of N integers in one line, and with one line of output for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecommendJGShining解答:#includemain(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1) {for(j=0;j<n;j++)< bdsfid="362" p=""></n;j++)<>{scanf("%d",&a);sum+=a;}printf("%d\n",sum); sum=0;}}1095A+B for Input-Output Practice (VII)Problem DescriptionYour task is to Calculate a + b.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.Sample Input1 510 20Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n\n",a+b);}1096 A+B for Input-Output Practice(VIII)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of input integers you should output their sumin one line, and you must note that there is a blank line between outputs.Sample Input34 1 2 3 45 1 2 3 4 53 1 2 3Sample Output10156AuthorlcyRecommendJGShining解答:int main(){int a,b,i,j,l[1000],k;scanf("%d",&i);getchar();for(j=1;j<=i;j++)l[j]=0;for(j=1;j<=i;j++){scanf("%d",&a);getchar();for(k=1;k<=a;k++){scanf("%d",&b);getchar();l[j]+=b;}}for(j=1;j<=i-1;j++)printf("%d\n\n",l[j]);printf("%d\n",l[i]);}2000 ASCII码排序Problem Description输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
acm数学竞赛试题及答案# 题目一:数列问题问题描述:给定一个数列 \( a_1, a_2, a_3, \ldots, a_n \),数列中每个元素都是正整数,且满足 \( a_i = a_{i-1} + a_{i-2} \) 对于所有\( i \geq 3 \)。
如果 \( a_1 = 1 \) 且 \( a_2 = 1 \),请找出数列的第 \( n \) 项。
解答:根据题意,这是一个斐波那契数列。
第 \( n \) 项的值可以通过递归关系计算得出。
对于 \( n \) 的值,可以使用以下递归公式:\[ a_n = a_{n-1} + a_{n-2} \]其中,\( a_1 = 1 \) 和 \( a_2 = 1 \)。
因此,数列的前几项为 1, 1, 2, 3, 5, 8, 13, 21, ...。
对于任意的 \( n \),可以通过递归或动态规划方法计算出 \( a_n \)。
# 题目二:组合问题问题描述:从 \( n \) 个不同的元素中选择 \( k \) 个元素的所有可能组合的个数是多少?解答:这个问题可以通过组合数学中的二项式系数来解决。
从 \( n \) 个不同元素中选择 \( k \) 个元素的组合数 \( C(n, k) \) 可以用以下公式计算:\[ C(n, k) = \frac{n!}{k!(n-k)!} \]其中,\( n! \) 表示 \( n \) 的阶乘。
# 题目三:几何问题问题描述:在一个直角坐标系中,给定三个点 \( A(x_1, y_1) \),\( B(x_2, y_2) \) 和 \( C(x_3, y_3) \)。
如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 是垂直的,求证 \( A \) 是直角三角形 \( ABC \) 的直角顶点。
解答:如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 垂直,那么它们的数量积(点积)应该为零。
ACM程序设计竞赛例题备战ACM资料习题1. 0-1背包问题在0 / 1背包问题中,需对容量为c 的背包进行装载。
从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。
对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。
程序如下:#includevoid readdata();void search(int);void checkmax();void printresult();int c=35, n=10; //c:背包容量;n:物品数int w[10], v[10]; //w[i]、v[i]:第i件物品的重量和价值int a[10], max; //a数组存放当前解各物品选取情况;max:记录最大价值//a[i]=0表示不选第i件物品,a[i]=1表示选第i件物品int main() {readdata(); //读入数据search(0); //递归搜索printresult();}void search(int m){if(m>=n)checkmax(); //检查当前解是否是可行解,若是则把它的价值与max 比较else{a[m]=0; //不选第m件物品search(m+1); //递归搜索下一件物品a[m]=1; //不选第m件物品search(m+1); //递归搜索下一件物品}}void checkmax(){int i, weight=0, value=0;for(i=0;i<n;i++)< p="">{if(a[i]==1) //如果选取了该物品{weight = weight + w[i]; //累加重量value = value + v[i]; //累加价值}}if(weight<=c) //若为可行解if(value>max) //且价值大于maxmax=value; //替换max}void readdata(){int i;for(i=0;i<n;i++)< p="">scanf("%d%d",&w[i],&v[i]); //读入第i件物品重量和价值}void printresult(){printf("%d",max);}2.装载问题有两艘船,载重量分别是c1、c2,n个集装箱,重量是wi (i=1…n),且所有集装箱的总重量不超过c1+c2。
acm基础试题及答案1. 题目:给定一个整数数组,请找出数组中第二大的数。
答案:我们可以使用排序的方法,将数组从小到大排序,然后数组中的倒数第二个数就是第二大的数。
或者使用一次遍历的方法,首先初始化两个变量,一个用来存储最大值,一个用来存储第二大的值。
遍历数组,每次比较当前元素与最大值,如果当前元素大于最大值,则更新第二大的值为最大值,并将当前元素赋给最大值;如果当前元素小于最大值但大于第二大的值,则更新第二大的值。
2. 题目:实现一个函数,计算一个字符串中字符出现的次数。
答案:可以使用哈希表来实现,遍历字符串中的每个字符,将其作为键值对存储在哈希表中,键是字符,值是该字符出现的次数。
遍历结束后,哈希表中存储的就是每个字符出现的次数。
3. 题目:给定一个链表,删除链表的倒数第n个节点,并且返回新的链表头节点。
答案:可以使用双指针的方法,首先初始化两个指针,都指向链表的头节点。
然后移动第一个指针,移动n步,此时第一个指针指向倒数第n个节点的前一个节点。
接着同时移动两个指针,直到第一个指针到达链表的末尾,此时第二个指针指向的节点就是需要删除的节点的前一个节点。
然后更新第二个指针的next指针,使其指向第二个指针的next节点的next节点,最后返回链表的头节点。
4. 题目:编写一个函数,判断一个整数是否是回文数。
回文数是指正序和倒序读都一样的数。
答案:首先将整数转换为字符串,然后使用双指针的方法,一个指针从字符串的开始位置,一个指针从字符串的结束位置,向中间移动。
如果两个指针指向的字符不相等,则该整数不是回文数。
如果遍历结束后没有发现不相等的字符,则该整数是回文数。
5. 题目:给定一个字符串,找出其中不含有重复字符的最长子串的长度。
答案:可以使用滑动窗口的方法,维护一个哈希表记录窗口内字符的出现情况,以及一个变量记录不含有重复字符的最长子串的长度。
遍历字符串,每次移动窗口的右端点,如果当前字符不在窗口内,则更新最长子串的长度,并将字符添加到哈希表中。
【人民币问题】Time Limit:1000MS Memory Limit:10000KTotal Submit:574 Accepted:278Description给出任意的人民币(>10元)的整币兑换成5元、2元和1元币值(要求三种币值均有)的方法有多少种。
Input输入任意的人民币(>10元)的整币100,50,20,10Output计算出兑换成5元、2元和1元币值(要求三种币值均有)的方法有多少种Sample Input50Sample Output106Source【哥德巴赫曾猜测】Time Limit:10000MS Memory Limit:65536KTotal Submit:592 Accepted:194Description德国数学家哥德巴赫曾猜测:任何大于6的偶数都可以分解成两个素数(素数对)的和。
但有些偶数可以分解成多种素数对的和,如: 10=3+7,10=5+5,即10可以分解成两种不同的素数对。
Input输入任意的>6的正偶数(<32767)Output试求给出的偶数可以分解成多少种不同的素数对(注: A+B与B+A认为是相同素数对)Sample Input1234Sample Output25SourceCode:#include<iostream>#include<math.h>using namespace std;int main(){int n;int z=0;int f(int);cin>>n;for(int i=2;i<=n/2;i++){if(f(i)) {if(f(n-i)){// cout<<i<<"+"<<n-i<<endl;z+=1;}}}cout<<z;cout<<endl;return 0;}int f(int y){int j;for( j=2;j<=sqrt(y);j++)if(y%j==0) break;if(j<=sqrt(y))return 0;elsereturn y;}【数列问题】Time Limit:10000MS Memory Limit:65536KTotal Submit:647 Accepted:312Description已知一个数列的前3个数为3,4,5,以后每个数为前3个数的和,编程序求此数列的第N项Input输入N(N<=35)Output求出第N项的值Sample Input28Sample Output25527448Source代码:#include<stdio.h>void main(){int a[35]={3,4,5};int n,i;scanf(“%d”,&n);for(i=3;i<=n;i++)a[i]=a[i-1]+a[i-2]+a[i-3];printf(“%d\n”,a[n-1]);}【敲七】Time Limit:10000MS Memory Limit:65536KTotal Submit:502 Accepted:170Description输出7和7的倍数,还有包含7的数字例如(17,27,37...70,71,72,73...)Input一个整数N。
(N不大于30000)Output统计出不大于N的与7有关的数字的个数。
如20以内与7有关的数为7、14、17共3个。
Sample Input20Sample Output3Source/*验证哥德巴赫猜想:任一充分大的偶数,可以用两个素数之和表示,例如:4 = 2 + 2 6 = 3 + 3 …… 98 = 19 + 79。
程序要求:键盘输入一个偶数x>4,找到两个素数a、b,满足 x=a+b。
最后输出此等式提示我们先不考虑怎样判断一个数是否为素数,而从整体上对这个问题进行考虑,可以这样做:读入一个偶数n ,将它分成p 和q ,使n = p + q 。
怎样分呢?可以令p 从2 开始,每次加1 ,而令q = n - p ,如果p 、q 均为素数,则正为所求,否则令p = p + 1 再试。
其基本算法如下:1)读入大于3的偶数n。
2)P=13)do{4)p=p+1;q=n-p;5)p是素数吗?6)q是素数吗?7)}while(p、q有一个不是素数)8)输出n=p+q。
为了判明p、q是否是素数,我们设置两个标志量flagp和flagq,初始值为0,若p是素数,令flagp=1,若q是素数,令flagq=1,于是第7步变成:7)}while(flagp*flagq==0);再来分析第5、第6步,怎样判断一个数是不是素数呢?素数就是除了1和它自身外,不能被任何数整除的整数,由定义可知:2、3、5、7、11、13、17、19等是素数;1、4、6、8、9、10、12、14等不是素数。
要判断i是否是素数,最简单的办法是用2、3、4、……i-1这些数依次去除i,看能否除尽,若能被其中之一除尽,则i不是素数,反之,i是素数。
其实,没必要用那么多的数去除,实际上,用反证法很容易证明,如果不大于i的平方根的数都不能整除,则i必是素数。
于是,上述算法中的第5步、第6步可以细化为:第5)步p是素数吗?flagp=1;for(j=2;j<=[sqrt(p)];j++)if p除以j的余数==0{flagp=0;break;}第6)步q是素数吗?flagq=1;for(j=2;j<=[sqrt(q)];j++)if q除以j的余数==0{flagq=0;break;} */#include<stdio.h>#include<math.h>void main(){int s=0,n,i;int f(int);scanf("%d",&n);for(i=1;i<=n/2;i++)if(f(i)+f(n-i)==2)s+=1;printf("%d\n",s);}int f(int b){ int a,c;for(a=2;a<=sqrt(b);a++)if(b%a==0)break;if(a>sqrt(b))c=1;else c=0;return c;}【倒杨辉三角形】Time Limit:1000MS Memory Limit:65536KTotal Submit:178 Accepted:13DescriptionFans喜欢图形,而且喜欢把图形倒过来欣赏。
有一次,他看见杨辉三角形了,觉得很新鲜,于是就把它们大大小小地摆布出来。
输入一些整数n(1≤n≤10),读入其每个整数,以该整数为行数,其画出来的倒杨辉三角形(每个数据占三个字符)就是fans 所喜欢欣赏的。
Fans是手工做的,你却可以用编程更快捷地做出来,多爽啊! InputOutputSample Input53Sample Output1 4 6 4 11 3 3 11 2 11 111 2 11 11Source【大小写转换】Time Limit:10000MS Memory Limit:65536KTotal Submit:341 Accepted:154Description读入一些字符串,将其中的小写字母转成大写字母(其他字符不变)。
Input输入为多行,每行为一个字符串,字符串只由字母和数字组成,长度不超过80。
输入以“End of file”结束。
Output对于每行输入,输出转换后的字符串。
Sample InputHelloICPC200412345abcdeSample OutputHELLOICPC200412345ABCDESourceCodes:#include<iostream>#include<string>using namespace std;int main(){char a;while((a=getchar())!=EOF){if(a>='a'&&a<='z')printf("%c",a-('a'-'A'));else printf("%c",a);}return 0;}最大子矩阵Time Limit:1000MS Memory Limit:65536KTotal Submit:7 Accepted:7Description已知矩阵的大小定义为矩阵中所有元素的和。
给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。
比如,如下4 * 4的矩阵0 -2 -7 09 2 -6 2-4 1 -4 1-1 8 0 -2的最大子矩阵是9 2-4 1-1 8这个子矩阵的大小是15。
Input输入部分第一行是一个正整数N,说明矩阵的大小。
下一行后面跟着n*n个整数。
留意这些整数的输入格式可能比较混乱。
矩阵是以行优先存储的。
N可能等于100,矩阵中每个数字的范围为[-127, 127].Output输出最大子矩阵的大小。
Sample Input40 -2 -7 0 9 2 -6 2-4 1 -4 1 -18 0 -2Sample Output15Hint翻译自Greater New York 2001 的试题Sourcexinyue【练习试题】:全排列Time Limit:1000MS Memory Limit:65536KTotal Submit:27 Accepted:10Description编程列举出1、2、…、n的全排列,要求产生的任一个数字序列中不允许出现重复的数字Input输入n(1<=n<=9)Output有1到n组成的所有不重复数字的序列,每行一个序列Sample Input3Sample Output1 2 31 3 22 1 32 3 13 1 23 2 1Sourcexinyue【母牛生小牛】Time Limit:10000MS Memory Limit:65536KTotal Submit:525 Accepted:196Description设有一头小母牛,从出生第四年起每年生一头小母牛,按此规律,第N年时有几头母牛?Input输入一个整数N。
(1≤N≤50)Output第N年时母牛的数量Sample Input5Sample Output3对n个数的排序:(从小到大) #include<iostream>using namespace std;int main (){int n,temp;cin>>n;int i,j,k;int *p=new(int[n]);for(i=0;i<n;i++)scanf("%d",&p[i]);for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(p[j]>p[j+1]){temp=p[j];p[j]=p[j+1];p[j+1]=temp;}for(k=0;k<n;k++)printf("%d ",p[k]);cout<<endl;delete []p;return 0;}输入一个整数,求出各位上的数字的乘积方法一:#include<iostream>using namespace std;int main(){ int n,s=1;cin>>n;while(n){s*=n%10;n/=10;}cout<<s;}1.基本思想:冒泡排序(BubbleSort)(稳定排序)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。