历届程序设计acm试题

合集下载

山东科技大学第二届ACM程序设计大赛试题

山东科技大学第二届ACM程序设计大赛试题

山东科技大学第二届ACM程序设计大赛试题册试题共14页,题目共计12道山东科技大学第二届ACM 程序设计大赛试题册Problem A 简单计算Description给出n 个十进制的数,找出这n 个数的二进制表示中1的个数最少的数。

Input输入的第一行为一个正整数T (1≤T ≤20),代表测试数据组数。

对于每组测试数据,输入的第一行为一个正整数n (1≤n ≤10000),第二行为n个正整数A 1、A 2、…、A n (1≤A i ≤109),每个数之间以空格分隔。

Output每组数据输出一行,先输出数据组数,再输出二进制中含1最少的数,如果存在多个数符合条件,输出最小的那个。

具体输出格式见样例输出。

Sample Input Sample Output山东科技大学第二届ACM 程序设计大赛试题册Problem B 关键字搜索Description我们的新网站具有了全新的搜索功能,使用了2个通配符“*”和“?”,其中“*”表示0或者多个小写字母,“?”代表1个字母。

当我们输入一个关键字的时候,我们在不确定的地方就使用通配符。

我们在数据库里面有多条记录,每条记录都是由小写字母组成,现在给出一个关键字,你能告诉我数据库里面有多少条与关键字相匹配的记录吗?例如: 如果关键字是j*y*m*y?,那么jiyanmoyu ,jyanmoyu ,jymyu 都是相匹配的记录。

Input第一行输入一个T (T ≤20),表示有T 组测试数据。

对于每组测试数据,第一行是输入的关键字,接下是数据库里面的所有记录的条数n ,1≤n ≤10000,每条记录的长度不超过50个小写字母。

Output对于每组测试数据,输出与关键字相匹配的总记录条数,占一行。

Sample Input Sample Output山东科技大学第二届ACM 程序设计大赛试题册Problem C 正方形Description在二维坐标轴内给出四个点,这四个点能否构成一个正方形。

计算机acm试题及答案

计算机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试题

搜集的南开大学的ACM试题与你共享[A]南开大学Onlinejudge 在线判题系统A.Lucy的新难题时间限制:2秒内存限制:32000KB不知不觉,南开大学第三届“我为程序狂”又要拉开帷幕了。

这天,Lucy也来到南开大学ACM协会,与大家共同欢庆NKPC的三周岁的日子。

谈笑间,ACM协会的主席拿了圆形的生日蛋糕。

大伙开心地唱完了生日歌,一起吹灭了蜡烛。

要分蛋糕了,大家都很兴奋。

本着公平的原则,每位到场的人员都要在蛋糕上切一刀。

ACM协会的主席事先知道有n位朋友会参与这个欢庆宴会。

为了方便大家切蛋糕,主席在订蛋糕的时候就嘱咐在蛋糕的边缘布置上2n朵小花。

每个人切蛋糕都会从蛋糕的边缘的一朵小花笔直地切到蛋糕的另一端的小花,来表达自己对NKPC的祝福。

为了尊重其他同学,每个人在切蛋糕一定不会和蛋糕上已有的切痕相交,也不会从别人已切过的小花作为切蛋糕的起点或终点。

同时,每位同学在切蛋糕的时候,都要保证后面所有的同学都能够按照上述的规则切蛋糕。

这样,蛋糕上就留下n条切痕。

Lucy眨巴眨巴眼睛,问,要是不考虑切蛋糕的先后顺序和谁切的哪一刀,这蛋糕切完了共有多少种切法呢?大家听了呵呵一笑,说,那就把这个问题留给NKPC3,作为《Lucy的新难题》吧。

相信聪明的你,一定能够帮Lucy解答她的难题的,对吗?输入包括多组测试数据,你应当处理到输入结束为止。

每组输入数据中,都只有一行,仅包含一个正整数n,且0<n≤30。

对于每组输入数据,输出两行。

对于第i组输入数据,输出的第一行为”Case i:”,输出的第二行为切蛋糕的方法数。

[B]南开大学Onlinejudge 在线判题系统B.保龄球时间限制:2秒内存限制:32000KB南开大学ACM协会的一个元老毕业后,开了家保龄球馆。

他需要为他的保龄球馆的计算机写一个记分的程序。

一局(GAME)保龄球分为10格,每格里有两次投球机会,如在第一次投球时没能全中,就有需要投第二球。

大学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竞赛试题及答案

acm竞赛试题及答案

acm竞赛试题及答案ACM竞赛试题及答案1. 问题描述:给定一个整数数组,找出数组中没有出现的最小的正整数。

2. 输入格式:第一行包含一个整数n,表示数组的长度。

第二行包含n个整数,表示数组的元素。

3. 输出格式:输出一个整数,表示数组中没有出现的最小的正整数。

4. 样例输入:53 4 1 2 55. 样例输出:66. 问题分析:首先,我们需要理解题目要求我们找出数组中缺失的最小正整数。

这意味着我们需要检查数组中的每个元素,并确定最小的正整数是否在数组中。

7. 算法描述:- 遍历数组,使用一个哈希集合记录出现的数字。

- 从1开始,检查每个正整数是否在哈希集合中,直到找到不在集合中的最小正整数。

8. 代码实现:```pythondef find_missing_positive(nums):seen = set()for num in nums:if num <= 0:continuewhile num in seen or num > len(nums):num += 1seen.add(num)return min(set(range(1, len(nums) + 1)) - seen)```9. 测试用例:- 输入:[3, 4, -1, 1]- 输出:210. 答案解析:在给定的测试用例中,数组[3, 4, -1, 1]中没有出现的最小正整数是2。

这是因为-1不是正整数,所以可以忽略。

数组中已经出现了1和3,所以下一个最小的正整数就是2。

11. 注意事项:- 确保数组中的元素是整数。

- 考虑数组中可能包含0或负数的情况。

- 算法的时间复杂度应尽可能低。

12. 扩展思考:- 如果数组非常大,如何优化算法?- 如果数组中的元素可以是浮点数,算法应该如何修改?13. 参考答案:- 针对大数组,可以考虑使用更高效的数据结构,如平衡二叉搜索树。

- 如果元素是浮点数,需要先将其转换为整数,然后再进行处理。

acm大赛试题及答案

acm大赛试题及答案

acm大赛试题及答案ACM大赛试题及答案1. 题目一:字符串反转问题描述:编写一个程序,输入一个字符串,输出其反转后的字符串。

输入格式:输入包含一个字符串,字符串长度不超过100。

输出格式:输出反转后的字符串。

示例:输入:hello输出:olleh答案:```pythondef reverse_string(s):return s[::-1]input_string = input().strip()print(reverse_string(input_string))```2. 题目二:计算阶乘问题描述:编写一个程序,输入一个非负整数n,输出n的阶乘。

输入格式:输入包含一个非负整数n,n不超过20。

输出格式:输出n的阶乘。

示例:输入:5输出:120答案:```pythondef factorial(n):if n == 0:return 1else:return n * factorial(n - 1)n = int(input())print(factorial(n))```3. 题目三:寻找最大数问题描述:给定一个包含n个整数的数组,找出数组中的最大数。

输入格式:输入包含一个整数n,表示数组的大小,随后是n个整数。

输出格式:输出数组中的最大数。

示例:输入:5 1 2 3 4 5输出:5答案:```pythonn = int(input())numbers = list(map(int, input().split()))max_number = max(numbers)print(max_number)```4. 题目四:判断闰年问题描述:编写一个程序,输入一个年份,判断该年份是否为闰年。

输入格式:输入包含一个整数,表示年份。

输出格式:如果输入的年份是闰年,则输出"Yes",否则输出"No"。

示例:输入:2000输出:Yes答案:```pythonyear = int(input())if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):print("Yes")else:print("No")```5. 题目五:斐波那契数列问题描述:编写一个程序,输入一个非负整数n,输出斐波那契数列的第n项。

acm试题及答案python

acm试题及答案python

acm试题及答案pythonACM试题及答案(Python)1. 问题描述:给定一个整数数组,请编写一个Python函数,找出数组中第二大的数。

2. 输入格式:一个包含整数的列表。

3. 输出格式:一个整数,表示数组中第二大的数。

4. 示例:- 输入:[10, 5, 8, 20, 15]- 输出:155. 答案:```pythondef find_second_max(nums):first_max = float('-inf')second_max = float('-inf')for num in nums:if num > first_max:second_max = first_maxfirst_max = numelif num > second_max and num != first_max:second_max = numreturn second_max if second_max != float('-inf') else None# 示例测试nums = [10, 5, 8, 20, 15]print(find_second_max(nums)) # 输出应为15```6. 分析:此题要求找出数组中第二大的数。

我们可以通过遍历数组,使用两个变量分别记录当前找到的最大值和第二大值。

在遍历过程中,如果当前元素比第一大的元素大,则更新第二大的元素为当前第一大的元素,并将当前元素设为第一大的元素。

如果当前元素小于第一大的元素但大于第二大的元素,则更新第二大的元素。

最后返回第二大的元素。

7. 注意:如果数组中只有一个元素或所有元素都相等,则返回`None`。

第三届ACM程序设计大赛试题new

第三届ACM程序设计大赛试题new

计算机工程学院第三届ACM程序设计大赛试题Problem A BridgeDescriptionn people wish to cross a bridge at night. A group of at most two people may cross at any time, and each group must have a flashlight. Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return the flashlight so that more people may cross.Each person has a different crossing speed; the speed of a group is determined by the speed of the slower member. Your job is to determine a strategy that gets all n people across the bridge in the minimum time.InputThe first line of input contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1000 people and nobody takes more than 100 seconds to cross the bridge. OutputThe first line of output must contain the total number of seconds required for all n people to cross the bridge. The following lines give a strategy for achieving this time. Each line contains either one or two integers, indicating which person or people form the next group to cross. (Each person is indicated by the crossing time specified in the input. Although many people may have the same crossing time the ambiguity is of no consequence.) Note that the crossings alternate directions, as it is necessary to return the flashlight so that more may cross. If more than one strategy yields the minimal time, any one will do.Sample Input(Input file: pa.txt)412510Sample Output171 215 1021 2Problem B LottoDescriptionIn the German Lotto you have to select 6 numbers from the set {1,2,...,49}. A popular strategy to play Lotto - although it doesn't increase your chance of winning - is to select a subset S containing k (k > 6) of these 49 numbers, and then play several games with choosing numbers only from S. For example, for k=8 and S = {1,2,3,5,8,13,21,34} there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21],[1,2,3,5,8,34], [1,2,3,5,13,21], ... [3,5,8,13,21,34].Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.InputThe input will contain one or more test cases. Each test case consists of one line containing several integers separated from each other by spaces. The first integer on the line will be the number k (6 < k < 13). Then k integers, specifying the set S, will follow in ascending order. Input will be terminated by a value of zero (0) for k.OutputFor each test case, print all possible games, each game on one line. The numbers of each game have to be sorted in ascending order and separated from each other by exactly one space. The games themselves have to be sorted lexicographically, that means sorted by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below. The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.Sample Input(Input file: pb.txt)7 1 2 3 4 5 6 78 1 2 3 5 8 13 21 34Sample Output1 2 3 4 5 61 2 3 4 5 71 2 3 4 6 71 2 3 5 6 71 2 4 5 6 71 3 4 5 6 72 3 4 5 6 71 2 3 5 8 131 2 3 5 8 211 2 3 5 8 341 2 3 8 13 211 2 3 8 13 341 2 3 8 21 341 2 3 13 21 341 2 5 8 13 211 2 5 8 13 341 2 5 8 21 341 2 5 13 21 341 2 8 13 21 341 3 5 8 13 211 3 5 8 13 341 3 5 8 21 341 3 5 13 21 341 3 8 13 21 341 5 8 13 21 342 3 5 8 13 212 3 5 8 13 342 3 5 8 21 342 3 5 13 21 342 3 8 13 21 342 5 8 13 21 343 5 8 13 21 34Problem C EncryptChip and Dale have devised an encryption method to hide their (written) text messages. They first agree secretly on two numbers that will be used as the number of rows (R) and columns (C) in a matrix. The sender encodes an intermediate format using the following rules:1. The text is formed with uppercase letters [A-Z] and <space>.2. Each text character will be represented by decimal values as follows:<space> = 0, A = 1, B = 2, C = 3, ..., Y = 25, Z = 26The sender enters the 5 digit binary representation of the characters' values in a spiral pattern along the matrix as shown below. The matrix is padded out with zeroes (0) to fill the matrix completely. For example, if the text to encode is: "ACM" and R=4 and C=4, the matrix would be filled in as follows:A = 00001, C = 00011, M = 01101 (one extra 0)The bits in the matrix are then concatenated together in row major order and sent to the receiver. The example above would be encoded as: 0000110100101100Inputspace, and a string of binary digits that represents the contents of the matrix (R * C binary digits). The binary digits are in row major order.OutputFor each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and the decoded text message. You should throw away any trailing spaces and/or partial characters found while decoding.Sample Input(Input file: pc.txt)24 4 ACM5 2 HISample Output1 00001101001011002 0110000010Problem D ConnectionThere are N cities in the country and M bidirectional roads between the cities. The government wants to build some new roads such that for any pair of cities there is at least one path between them. Now you have to find out the minimal amount of roads that have to build.InputThe input may contain multiple test cases.For each test case, the first line is two integers N (1<=N<=100) and M (1<=M<=N*N/2),indicating the number of cities and the number of roads. Then the next M lines each contain two integers x and y (0<=x,y<n), meaning that there is a road between cities x and cities y.N=M=0 indicates the end of input.OutputFor each test case, print the answer in a single li ne.Sample Input(Input file: pd.txt)5 20 12 3Sample Output2Problem E GridlandBackgroundFor years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the "easy" problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the "hard" ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.ProblemThe president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North�CSouth or East�CWest is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 �� 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 �� 3-Gridland, the shortest tour has length 6.Figure 7: A traveling-salesman tour in 2 �� 3-Gridland.InputThe first line contains the number of scenarios.For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.OutputThe output for each scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. In the next line, print the length of the shortest traveling-salesman tour rounded to two decimal digits. The output for every scenario ends with a blank line.Sample Input(Input file: pe.txt)2Sample OutputScenario #1:4.00Scenario #2:6.00Problem F Digital RootsBackgroundThe digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.InputThe input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.OutputFor each integer in the input, output its digital root on a separate line of the output.Sample input(Input file: pf.txt)2439Sample Output63Problem G Counting NumbersStarting from a positive integer n (1<=n<=2001).On the left of the integer n ,you can place another integer m to form a new integer mn , where m must be less then or equal to half of the integer n ,If there is an integer k less then or equal to half of m, you can place integer k on the left of mn ,to form a new integer kmn,…,and so on .For Examole ,you can place 12 on the left of 30 to Form an integer 1230,and you can place 6 to the left of 1230 to form an integer 61230,…,and so onFor example , start from n=8.you can place integer 1,2,3and 4 to the left of 8 to get the integers 18,28,38,48.For number 18,you can not form a new integer using the procedure described as above.For number28 and 38,you can form new integers 128 and 138.For number 48 ,you can place 1 and 2 on the left of 48 to get new integers 148 and 248.For number 248,you can place 1 on the left of it to get a new integer 1248.In total, you can have the following 10 integers(includeing the integer you start with)8182838481281381482481248Give an integer n ,find the number of integers you can get using the procedure described above.InputAn integer nOutputAn integer witch represents the number of integer you can get.Sample input: (Input file: pg.txt)8Sample Output:10Problem H Buy Low, Buy LowerThe advice to "buy low" is half the formula to success in the stock market. But to be considered a great investor you must also follow this problems' advice:"Buy low, buy lower"That is, each time you buy a stock, you must purchase more at a lower price than the previous time you bought it. The more times you buy at a lower price than before, the better! Your goal is to see how many times you can continue purchasing at ever lower prices.You will be given the daily selling prices of a stock over a period of time. You can choose to buy stock on any of the days. Each time you choose to buy, the price must be lower than the previous time you bought stock. Write a program which identifies which days you should buy stock in order to maximize the number of times you buy.By way of example, suppose on successive days stock is selling like this:Day 1 2 3 4 5 6 7 8 9 10 11 12Price 68 69 54 64 68 64 70 67 78 62 98 87In the example above, the best investor (by this problem, anyway) can buy at most four times if they purchase at a lower price each time. One four day sequence (there might be others) of acceptable buys is:Day 2 5 6 10Price 69 68 64 62PROGRAM NAME: buylowInputLine 1: N (1 <= N <= 5000), the number of days for which stock prices are available.Line 2..etc: A series of N positive space-separated integers (which may require more than one line of data) that tell the price for that day. The integers will fit into 32 bits quite nicely.Outputthe length of the longest sequence of decreasing pricesSample input: (Input file: ph.txt)1268 69 54 64 68 64 70 67 78 62 98 87Sample Output:4注意事项:1、数据从文件输入,标准输出,注意输入文件名题中已经给出。

重庆科技学院第一届ACM程序设计大赛试题

重庆科技学院第一届ACM程序设计大赛试题

重庆科技学院第一届A C M程序设计大赛试题(0)(总5页)--本页仅作为文档封面,使用时请直接删除即可----内页可以根据需求调整合适字体及大小--重庆科技学院首届程序设计大赛暨重庆市第七届程序设计大赛选拔赛试题一、求矩阵主对角线、次角线上质数之和(难度系数:1)(输入文件:,输出文件:文本文件中有一个行列数相同的二维矩阵。

每组数据的第一行由空格分开的两个数分别为该二维矩阵的行数、和列数;行数和列数不超过100。

从第二行开始为该二维矩阵,各个元素间由空格分格。

求该二维矩阵主对角线与次对角线上所有质数之和并将该结果输出到文件中。

样例输入:4 45 3 4 33 6 7 85 6 9 99 4 3 7:22二、密码问题(难度系数:2)(输入文件:,输出文件:网上流传一句话:"常在网上飘啊,哪能不挨刀啊~"。

其实要想能安安心心地上网其实也不难,学点安全知识就可以。

首先,我们就要设置一个安全的密码。

那什么样的密码才叫安全的呢?一般来说一个比较安全的密码至少应该满足下面两个条件:(1).密码长度大于等于8,且不要超过16。

(2).密码中的字符应该来自下面“字符类别”中四组中的至少三组。

这四个字符类别分别为:1.大写字母:A,B,C...Z;2.小写字母:a,b,c...z;3.数字:0,1,2...9;4.特殊符号:~,!,@,#,$,%,^;给你一个密码,你的任务就是判断它是不是一个安全的密码。

Input输入数据第一行包含一个数M,接下有M行,每行一个密码(长度最大可能为50),密码仅包括上面的四类字符。

Output对于每个测试实例,判断这个密码是不是一个安全的密码,是的话输出YES,否则输出NO。

样例输入:3a1b2c3d4Linle@ACM^~^@^@!%NOYESNO三、扫雷游戏(难度系数:3)(输入文件:,输出文件:玩过扫雷游的朋友都知道,该游戏的目标是找出一个n*m矩阵内的所有的地雷,在本题中,你需要为每一个单元格统计出它周围地雷的个数,每个单元格最多有8个相邻单元格,如下图,4*4 的格子里,用“*”表示雷,用“^”表示没有雷。

ACM程序设计竞赛例题[1]

ACM程序设计竞赛例题[1]

备战ACM资料习题1. 0-1背包问题在0 / 1背包问题中,需对容量为c 的背包进行装载。

从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。

对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。

程序如下:#include <>void readdata();void search(int);void checkmax();void printresult();int c=35, n=10; ");printf("\n");}printf("\n");}6.素数环问题把从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数。

分析:用回溯算法,考察所有可能的排列。

程序如下:#include <>#include <>void search(int);void init(); 表示空格;’X’表示墙。

程序如下:#include <>#include <>void search(int,int);int canplace(int,int);void readdata(); Floodfill给一个20×20的迷宫和一个起点坐标,用广度优先搜索填充所有的可到达的格子。

提示:参考第2题。

2. 电子老鼠闯迷宫如下图12×12方格图,找出一条自入口(2,9)到出口(11,8)的最短路本题给出完整的程序和一组测试数据。

状态:老鼠所在的行、列。

程序如下:#include<>void readdata();a[i][j]=0; ....注:测试数据可在运行时粘贴上去(点击窗口最左上角按钮,在菜单中选则“编辑”/“粘贴”即可)。

想一想:此程序都存在哪些问题,如果openlen太小程序会不会出错,加入代码使程序能自动报出此类错误。

acm大赛历年程序题

acm大赛历年程序题

acm大赛历年程序题
ACM大赛是一项计算机竞赛,每年都会发布一系列的程序题供
参赛者解答。

这些题目涵盖了各个计算机科学领域的知识,包括数
据结构、算法、图论、动态规划、数学等等。

以下是一些历年ACM
大赛的程序题的例子:
1. 最短路径问题,给定一个有向带权图,求两个节点之间的最
短路径。

可以使用Dijkstra算法或者Floyd-Warshall算法来解决。

2. 字符串处理问题,给定一个字符串,要求对其进行特定的处理,比如反转、删除重复字符等。

可以使用字符串操作和遍历来解决。

3. 数组操作问题,给定一个数组,要求对其进行特定的操作,
比如排序、查找最大/最小值、计算数组的平均值等。

可以使用排序
算法、查找算法和遍历来解决。

4. 动态规划问题,给定一个问题和一组限制条件,要求找到满
足条件的最优解。

可以使用动态规划的思想,将问题拆分成子问题
并逐步求解。

5. 图论问题,给定一个图,要求对其进行特定的操作,比如查找连通分量、判断是否存在环等。

可以使用图的遍历和深度优先搜索或广度优先搜索来解决。

6. 数学问题,给定一个数学问题,要求求解或验证某个数学定理或公式。

可以使用数学运算和推导来解决。

这些只是一小部分例子,ACM大赛的题目类型非常多样化,每年都会有新的题目发布。

参赛者需要具备扎实的计算机科学基础知识和良好的编程能力,才能在规定时间内解决这些问题。

ACM比赛试题

ACM比赛试题

The 35th ACM-ICPC Asia Regional Contest (Hangzhou)Contest SectionOctober 24, 2010Sponsored by IBM & AlibabaZhejiang Sci-Tech UniversityThis problem set should contain 10 problems on numbered 24 pages. Please inform a runner immediately if something is missing from your problem set.Problem A. Naughty fairiesDescriptionOnce upon a time, there lived a kind of fairy in the world. Those fairies could hear the voice of fruit trees, and helped people with a harvest. But people did n’t know that fruits are also those fairies’ favorite food. After the fairies ate people’s fruits, they always did something to cover it up.One day a little fairy named Lily flew into an orchard and found a large peach tree. Hungry as Lily was, she started eating without thinking until her stomach was full. In the fairy world, when a fairy ate the fruits in a fruit tree, sometimes the fruit tree would feel honored and bore more fruits immediately. That’s why sometimes the number of fruits in a tree got increased after a fairy ate fruits of that tree.But the fairies didn’t want people to find out weird things such as fruits become more or less suddenly. Lily decided to use a magic spell so that the orchard owner couldn’t find the change of the number of p eaches.Suppose there were N peaches on a tree originally and there were M peaches left after Lily was full. M may be greater than, less than or equal to N. All M peaches were visible at first, and Lily wanted to make an illusion so that exactly N peaches are visible.Lily can do 3 kinds of spell to change the total visible number of peaches:1) “PAPADOLA”:This spell would increase the number of visible peaches by one.2) “EXPETO POTRONUM”:This spell would double the number of visible peaches.3) “SAVIDA LOHA”:This spell would decrease the number of visible peaches by one. Each spell would take one minute and Lily wanted to finish as fast as possible. Now please tell Lily the least time she needs to change the number of visible peaches to N.InputThere are several test cases, ended by “0 0”.For each test case, there are only one line containing two numbers separated by a blank, N and M, the original numbers of peaches and the numbers of peaches left(0<N,M<10500).There is no leading zero.OutputFor each test case, you should output just a number K indicating the minimum time (in minutes) Lily needed to finish her illusion magic.Sample Input5 21 9986 320 0Sample Output29812Problem B. Prison BreakDescriptionRompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him were put into jail, including our clever Micheal#1. Now it’s time to escape, but Micheal#1 needs an optimal plan and he contacts you, one of his human friends, for help.The jail area is a rectangle contains n×m little grids, each grid might be one of the following:1) Empty area, represented by a capital letter ‘S’.2) The starting position of Micheal#1, represented by a capital letter ‘F’.3) Energy pool, represented by a capital letter ‘G’. When entering a n energy pool, Micheal#1 can use it to charge his battery ONLY ONCE. After the charging, Micheal#1’s batt ery will become FULL and the energy pool will become an empty area. Of course, passing an energy pool without using it is allowed.4) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal#1 cannot step into a grid with a laser sensor.5) Power switch, represented by a capital letter ‘Y’. Once Micheal#1 steps into a grid with a Power switch, he will certainly turn it off.In order to escape from the jail, Micheal#1 need to turn off all the power switches to stop the electric web on the roof—then he can just fly away. Moving to an adjacent grid (directly up, down, left or right) will cost 1 unit of energy and only moving operation costs energy. Of course, Micheal#1 cannot move when his battery contains no energy.The larger the battery is, the more energy it can save. But larger battery means more weight and higher probability of being found by the weight sensor. So Micheal#1 needs to make his battery as small as possible, and still large enough to hold all energy he need. Assuming that the size of the battery equals to maximum units of energy that can be saved in the battery, and Micheal#1 is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.InputInput contains multiple test cases, ended by 0 0. For each test case, the first line contains two integer numbers n and m showing the size of the jail. Next n lines consist of m capital letters each, which stands for the description of the jail.You can assume that 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.OutputFor each test case, output one integer in a line, representing the minimum size of the battery Micheal#1 needs. If Micheal#1 can’t escape, output -1.Sample Input5 5GDDSSSSSFSSYGYSSGSYSSSYSS0 0Sample Output4Problem C. To Be an Dream Architect DescriptionThe “dream architect” is the key role in a team of “dream extractors” who enter other’s dreams to steal secrets. A dream architect is responsible for crafting the virtual world that the team and the target will dream into. To avoid the target noticing the world is artificial, a dream architect must have powerful 3D imagination.Cobb uses a simple 3D imagination game to test whether a candidate has the potential to be an dream architect. He lets the candidate imagine a cube consisting of n×n×n blocks in a 3D coordinate system as Figure 1. The block at bottom left front corner is marked (1, 1, 1) and the diagonally opposite block is marked (n, n, n). Then he tells the candidate that the blocks on a certain line are eliminated. The line is always parallel to an axis. After m such block eliminations, the candidate is asked to tell how many blocks are eliminated. Note that one block can only be eliminated once even if it is on multiple lines.Here is a sample graph according to the first test case in the sample input:InputThe first line is the number of test cases.In each test case, the first line contains two integers n and m( 1 <= n <= 1000, 0 <= m <= 1000).,meaning that the cube is n x n x n and there are m eliminations.Each of the following m lines represents an elimination in the following format:axis_1=a, axis_2=bwhere axis_i (i=1, 2) is ‘X’ or ‘Y’, or ‘Z’ and axis_1 is not equal to axis_2. a and b ar e 32-bit signed integers.OutputFor each test case output the number of eliminated blocks.Sample Input23 2Y=1,Z=3X=3,Y=110 2X=3,Y=3Y=3,Z=3Sample Output519Problem D. GomokuDescriptionYou are probably not familiar with the title, “Gomoku”, but you must have played it a lot. Gomoku is an abstract strategy board game and is also called Five in a Row, or GoBang. It is traditionally played with go pieces (black and white stones) on a go board (19x19 intersections). Nowadays, standard chessboard of Gomoku has 15x15 intersections. Black plays first, and players alternate in placing a stone of their color on an empty intersection. The winner is the first player to get an unbroken row of five or more stones horizontally, vertically, or diagonally.For convenience, we coordinate the chessboard as illustrated above. The left-bottom intersection is (0,0). And the bottom horizontal edge is x-axis, while the left vertical line is y-axis.I am a fan of this game, actually. However, I have to admit t hat I don’t have a sharp mind. So I need a computer program to help me. What I want is quite simple. Given a chess layout, I want to know whether someone can win within 3 moves, assuming both players are clever enough. Take the picture above for example. There are 31 stones on it already, 16 black ones and 15 white ones. Then we know it is white turn. The white player must place a white stone at (5,8). Otherwise, the black player will win next turn. After that, however, the white player also gets a perfect situation that no matter how his opponent moves, he will win at the 3rd move.So I want a program to do similar things for me. Given the number of stones and positions of them, the program should tell me whose turn it is, and what will happen within 3 moves.InputThe input contains no more than 20 cases.Each case contains n+1 lines which are formatted as follows.nx1 y1 c1x2 y2 c2......x n y n c nThe first integer n indicates the number of all stones. n<=222 which means players have enough space to place stones. Then n lines follow. Each line contains three integers: x i and y i and c i. x i and y i are coordinates of the stone, and ci means the color of the stone. If c i=0 the stone is white. If c i=1 the stone is black. It is guaranteed that 0<=x i,y i<=14, and c i=0 or 1. No two stones are placed at the same position. It is also guaranteed that there is no five in a row already, in the given cases.The input is ended by n=0.OutputFor each test case:First of all, the program should check whose turn next. Le t’s call the player who will move next “Mr. Lucky”. Obviously, if the number of the black stone equals to the number of white, Mr. Lucky is the black player. If the number of the black stone equals to one plus the numbers of white, Mr. Lucky is the white player. If it is not the first situation or the second, print “Invalid.”A valid chess layout leads to four situations below:1)Mr. Lucky wins at the 1st move. In this situation, print :Place TURN at (x,y) to win in 1 move.“TURN” must be replaced by “black” or “white” according to the situation and (x,y) is the position of the move. If there are different moves to win, choose theone where x is the smallest. If there are still different moves, choose the one where y is the smallest.2)Mr. Lucky’s opp onent wins at the 2nd move. In this situation, print:Lose in 2 moves.3)Mr. Lucky wins at the 3rd move. If so, print:Place TURN at (x,y) to win in 3 moves.“TURN” should replaced by “black” or “white”, (x,y) is the position where the Mr.Lucky should place a stone at the 1st move. After he place a stone at (x,y), no matter what his opponent does, Mr. Lucky will win at the 3rd step. If there are multiple choices, do the same thing as described in situation 1.4)Nobody wins within 3 moves. If so, print:Cannot win in 3 moves.Sample Input313 3 13 4 03 5 03 6 04 4 14 5 14 7 05 3 05 4 05 5 15 6 15 7 15 9 16 4 16 5 16 6 06 7 16 8 06 9 07 5 17 6 07 7 17 8 17 9 08 5 08 6 18 7 08 8 18 9 09 7 110 8 017 7 117 7 0Sample OutputPlace white at (5,8) to win in 3 moves. Cannot win in 3 moves.Invalid.Problem E. GunshotsDescriptionPresident Bartlet was shot! A group of terrorists shot to the crowd when President Bartlet waved to cheering people after his address. Many people were shot by the irrational bullets. Senior FBI agent Don Epps takes responsibility for this case. According to a series of crime scene investigation, including analyzing shot shells, replaying video from closed-circle television and collecting testimony by witnesses, Don keeps all the information about where and how the terrorists shot to crowd, as well as the location of every single person when the gun shoot happened. Now he wants to know how many gunshot victims are there in this case.Imagine that each target person can be regarded as a polygon (can be concave or self-intersecting) and each gunshot can be regarded as a half-line. The bullet will be stopped by the first person it shoots. A person can be shot in three ways:To simplify the problem, we assume that any two polygons can be completely separated by a line. Also each start point of the gunshot can be separated from each polygon by a line. Now given M people and N gunshots, please work out which person has been shot by each bullet.InputThere are multiple test cases in the input. The first line of the input file is an integer T demonstrating the number of test cases. (T<=10).For each test case, the first line is an integer N, representing the number of people (polygons). Following lines demonstrates the polygons. For the i th polygon (0<=i<N), the first line is an integer Q i , representing the number of edges of this polygon. In each of the following Q i lines, there are two real numbers x i and y i representing a point. Every pair of adjacent points demonstrate an edge of this polygon (i.e. (x i , y i ) to (x i+1, y i+1) is an edge, in which 0<=i<Q i -1), and (x Qi-1, y Qi-1) to (x 0, y 0) also demonstrates an edge of this polygon.PersonPerson Person 1. Normal shot 2. The bullet’s path isparallel to an edge 3. The bullet’s path is tangent to an vertexThen there is a line contains an integer M representing the number of gunshots. In the following M lines, each line contains four real numbers x, y, dx and dy, representing the start point (x, y) and direction vector (dx, dy) of that gunshot.In all test cases, we assume that 0< N<=100, 0<Q i<=1000, 0<M<=10000.OutputFor each test case, output contains M lines and the i th line demonstrates the result of the i th gunshot.If the i th gunshot shoots the j th polygon, the i th line contains “HIT j”, otherwise it contains a word “MISS” (means that it does not shoot any target). The polygons are numbered in the order of their appearance in the input file, and the numbers start from 0.At the end of each test case, please output a single line with “*****”.Sample Input1140 01 10 11 02-1 0 1 0-2 0 -1 0Sample OutputHIT 0MISS*****HintThe figure of the first case in the samples is as follows:Problem F. Rotational PaintingDescriptionJosh Lyman is a gifted painter. One of his great works is a glass painting. He creates some well-designed lines on one side of a thick and polygonal glass, and renders it by some special dyes. The most fantastic thing is that it can generate different meaningful paintings by rotating the glass. This method of design is called “Rotatio nal Painting (RP)” which is created by Josh himself.You are a fan of Josh and you bought this glass at the astronomical sum of money. Since the glass is thick enough to put erectly on the table, you want to know in total how many ways you can put it so that you can enjoy as many as possible different paintings hiding on the glass. We assume that material of the glass is uniformly distributed. If you can put it erectly and stably in any ways on the table, you can enjoy it.More specifically, if the polygonal glass is like the polygon in Figure 1, you have just two ways to put it on the table, since all the other ways are not stable. However, the glass like the polygon in Figure 2 has three ways to be appreciated.Pay attention to the cases in Figure 3. We consider that those glasses are not stable.InputThe input file contains several test cases. The first line of the file contains an integer T representing the number of test cases.For each test case, the first line is an integer n representing the number of lines of the polygon. (3<=n<=50000). Then n lines follow. The i th line contains two real number x i and y i representing a point of the polygon. (x i, y i) to (x i+1, y i+1) represents a edge of the polygon (1<=i<n), and (x n,y n) to (x1, y1) also represents a edge of the polygon. The input data insures that the polygon is not self-crossed.OutputFor each test case, output a single integer number in a line representing the number of ways to put the polygonal glass stably on the table.Sample Input240 0100 099 11 160 00 101 101 110 110 0Sample Output23HintThe sample test cases can be demonstrated by Figure 1 and Figure 2 in Description part.Problem G. Traffic Real Time Query System DescriptionCity C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, the mayor plans to build a RTQS (Real Time Query System) to monitor all traffic situations. City C is made up of N crossings and M roads, and each road connects two crossings. All roads are bidirectional. One of the important tasks of RTQS is to answer some queries about route-choice problem. Specifically, the task is to find the crossings which a driver MUST pass when he is driving from one given road to another given road.InputThere are multiple test cases.For each test case:The first line contains two integers N and M, representing the number of the crossings and roads.The next M lines describe the roads. In those M lines, the i th line (i starts from 1)contains two integers X i and Y i, representing that road i connects crossing X i and Y i (X i≠Y i).The following line contains a single integer Q, representing the number of RTQs. Then Q lines follows, each describing a RTQ by two integers S and T(S≠T) meaning that a driver is now driving on the road s and he wants to reach road t . It will be always at least one way from road s to road t.The input ends with a line of “0 0”.Please note that: 0<N<=10000, 0<M<=100000, 0<Q<=10000, 0<X i,Y i<=N, 0<S,T<=M OutputFor each RTQ prints a line containing a single integer representing the number of crossings which the driver MUST pass.Sample Input5 61 22 33 44 53 522 32 40 0Sample Output 01Problem H. National Day ParadeDescriptionThere are n×n students preparing for the National Day parade on the playground. The playground can be considered as a n×m grid. The coordinate of the west north corner is (1,1) , and the coordinate of the east south corner is (n,m).When training, every students must stand on a line intersection and all students must form a n×n square. The figure above shows a 3×8 playground with 9 students training on it. The thick black dots stand for the students. You can see that 9 students form a 3×3 square.After training, the students will get a time to relax and move away as they like. To make it easy for their masters to control the training, the students are only allowed to move in the east-west direction. When the next training begins, the master would gather them to form a n×n square again, and the position of the square doesn’t matter. Of course, no student is allowed to stand outside the playground.You are given the coordinates of each student when they are having a rest. Your task is to figure out the minimum sum of distance that all students should move to form a n×n square.InputThere are at most 100 test cases.For each test case:The first line of one test case contain two integers n,m. (n<=56,m<=200)Then there are n×n lines. Each line contains two integers, 1<=X i<=n,1<= Y i<=m indicating that the coordinate of the i th student is (X i , Y i ). It is possible for more than one student to stand at the same grid point.The input is ended with 0 0.OutputYou should output one line for each test case. The line contains one integer indicating the minimum sum of distance that all students should move to form a n×n square. Sample Input2 1682 1011 1271 1052 900 0Sample Output41Problem I. SearchlightsDescriptionThere is a piece of grids land of size n×m. Chandler and his team take responsibility to guard it. There are some searchlights on some pieces and each of them has a capability to lighten a distance towards four directions: north, south, east and west. Different searchlight has different lightening capability shown in levels. Searchlight with level k means that it can lighten k grids (including the gird that the searchlight stands in) along any of the four directions. Shown in following figure, there is a searchlight of level 3 and the shadow grids are ones that can be lightened by it. Particularly, searchlight of level 1 means that it can only lighten the grid in which the searchlight stands.Figure: A searchlight of Level 3Each searchlight has a maximum level. You can decrease a searchlight’s level to save the energy. A searchlight whose maximum level is k can be turned to level k, k-1, k-2, …, 1 and 0. Level 0 means turning off the searchlight.A grid is well-guarded if and only if at least one of the following two conditions is satisfied:1.There is a searchlight in this grid, and it is not switched to level 0 (the light is on).2.The grid is lightened by at least two searchlights. One lightens it in horizontaldirection (east or west), and another lightens it in vertical direction (north or south).Chandler asks you to help finding a solution that he can turn on some of the searchlights so that:1.All the grids are well-guarded.2.All the searchlights turned on are in a same level.3.That same level mentioned above is as small as possible.More specifically, if you choose a same level Q, then all the searchlights whose maximum level are less than Q have to be turned off. Please help him to find a solution with the minimum same level.InputThe input file contains several test cases.For each test case, the first line is two integers n and m, representing a grids land of size n×m. (0<n<=100, 0<m<=10000). Following n lines describe an n×m matrix in which a i,j means the maximum level of the searchlight in grid (i, j). a i,j can be zero, which means there is no searchlight on that grid. For all the cases, a i, j<=10000.The input file ends with a line containing two zeros.OutputFor each test case, output a single line with an integer, representing the minimum level you have found. If there is no such a solution, output “NO ANSWER!”Sampl e Input2 20 23 02 20 21 00 0Sampl e Output2NO ANSWER!Problem J. Infinite monkey theorem DescriptionCould you imaging a monkey writing computer programs? Surely monkeys are smart among animals. But their limited intelligence is no match for our human beings. However, there is a theorem about monkeys, and it states that monkeys can write everything if given enough time.The theorem is called “Infinite monkey theorem”. It states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type any given text, which of course includes the programs you are about to write (All computer programs can be represented as text, right?).It’s very easy to prove this theorem. A little calculation will show you that if the monkey types for an infinite length of time the probability that the output contains a given text will approach 100%.However, the time used is too long to be physically reasonable. The monkey will not be able to produce any useful programs even if it types until the death of the universe. To verify this and ensure that our human beings are not replaceable by monkeys, you are to calculate the probability that a monkey will get things right.InputThere will be several test cases.Each test case begins with a line containing two integers n and m separated by a whitespace (2<=n<=26, 1<=m<=1000). n is the number of keys on the typewriter and the monkey will hit these keys m times. Thus the typewriter will finally produce an output of m characters.The following n lines describe keys on the typewriter. Each line has a lower case letter and a real number separated by a whitespace. The letter indicates what the typewriter will produce if the monkey hits that key and the real number indicates the probability that the monkey will hit this key. Two hits of the monkey are independent of each other (Two different hits have the same probability for a same key), and sum of all the probabilities for each key is ensured to be 1.The last line of the test case contains a word composed of lower case letters. The length of the word will be less than or equal to 10.The input will end with a line of two zeros separated by a whitespace. This line should not be processed.OutputFor each test case, output one line containing the probability that the given word will appear in the typewriter’s output. The output should be in percentage format and numbers should be rounded to two digits after the decimal point.Sampl e Input4 10w 0.25o 0.25r 0.25d 0.25word2 10a 1.0b 0.0abc2 100a 0.312345b 0.687655abab0 0Sampl e Output2.73%0.00%98.54%。

acm大学生程序设计试题

acm大学生程序设计试题

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试题及答案

Acm试题及答案Acm试题及答案1001 Sum Problem (2)1089 A+B for Input-Output Practice (I) (3) 1090 A+B for Input-Output Practice (II) (4) 1091A+B for Input-Output Practice (III) (6) 1092A+B for Input-Output Practice (IV) (7) 1093 A+B for Input-Output Practice (V) (9) 1094 A+B for Input-Output Practice (VI) (11) 1095A+B for Input-Output Practice (VII) (12) 1096 A+B for Input-Output Practice (VIII) (14) 2000 ASCII码排序 (15)2001计算两点间的距离 (16)2002计算球体积 (17)2003求绝对值 (18)2004成绩转换 (19)2005第几天? (20)2006求奇数的乘积 (22)2007平方和与立方和 (23)2008数值统计 (24)2009求数列的和 (25)2010水仙花数 (27)2011多项式求和 (28)2012素数判定 (29)2014青年歌手大奖赛_评委会打分 (31)2015偶数求和 (32)2016数据的交换输出 (34)2017字符串统计 (35)2019数列有序! (37)2020绝对值排序 (38)2021发工资咯:) (40)2033人见人爱A+B (41)2039三角形 (43)2040亲和数 (44)1001 Sum ProblemProblem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.InputThe input will consist of a series of integers n, one integer per line.OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.Sample Input1100Sample Output15050AuthorDOOM III解答:#includemain(){int n,i,sum;sum=0;while((scanf("%d",&n)!=-1)){sum=0;for(i=0;i<=n;i++)sum+=i;printf("%d\n\n",sum);}}1089 A+B for Input-Output Practice(I)Problem DescriptionYour task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners.You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input1 510 20Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n",a+b);}1090 A+B for Input-Output Practice(II)Problem DescriptionYour task is to Calculate a + b.InputInput contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input21 510 20Sample Output630AuthorlcyRecommendJGShining解答:#include#define M 1000void main(){int a ,b,n,j[M],i;//printf("please input n:\n");scanf("%d",&n);for(i=0;i<n;i++)< bdsfid="216" p=""></n;i++)<> {scanf("%d%d",&a,&b);//printf("%d %d",a,b);j[i]=a+b;}i=0;while(i<n)< bdsfid="224" p=""></n)<>{printf("%d",j[i]); i++;printf("\n");}}1091A+B for Input-Output Practice (III) Problem DescriptionYour task is to Calculate a + b.InputInput contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input1 510 200 0Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;scanf("%d %d",&a,&b);while(!(a==0&&b==0)){printf("%d\n",a+b);scanf("%d %d",&a,&b);}}1092A+B for Input-Output Practice (IV)Problem DescriptionYour task is to Calculate the sum of some integers.InputInput contains multiple test cases. Each test case contains a integer N, and then N integers followin the same line. A test case starting with 0 terminates the input and this test case is not to be processed.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecommendJGShining解答:#includeint main(){int n,sum,i,t;while(scanf("%d",&n)!=EOF&&n!=0)sum=0;for(i=0;i<n;i++)< bdsfid="290" p=""></n;i++)<>{scanf("%d",&t);sum=sum+t;}printf("%d\n",sum); }1093 A+B for Input-Output Practice (V)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.Sample Input24 1 2 3 45 1 2 3 4 5Sample Output1015Authorlcy解答:#includemain()int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1){for(i=0;i<n;i++)< bdsfid="322" p=""></n;i++)<>{scanf("%d",&b);for(j=0;j<b;j++)< bdsfid="326" p=""></b;j++)<>{scanf("%d",&a);sum+=a;}printf("%d\n",sum); sum=0;}}}1094 A+B for Input-Output Practice(VI)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.OutputFor each test case you should output the sum of N integers in one line, and with one line of output for each line in input.Sample Input4 1 2 3 45 1 2 3 4 5Sample Output1015AuthorlcyRecommendJGShining解答:#includemain(){int n,a,b,i,j,sum;sum=0;while(scanf("%d\n",&n)!=-1) {for(j=0;j<n;j++)< bdsfid="362" p=""></n;j++)<>{scanf("%d",&a);sum+=a;}printf("%d\n",sum); sum=0;}}1095A+B for Input-Output Practice (VII)Problem DescriptionYour task is to Calculate a + b.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.Sample Input1 510 20Sample Output630AuthorlcyRecommendJGShining解答:#includemain(){int a,b;while(scanf("%d%d",&a,&b)!=EOF)printf("%d\n\n",a+b);}1096 A+B for Input-Output Practice(VIII)Problem DescriptionYour task is to calculate the sum of some integers.InputInput contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.OutputFor each group of input integers you should output their sumin one line, and you must note that there is a blank line between outputs.Sample Input34 1 2 3 45 1 2 3 4 53 1 2 3Sample Output10156AuthorlcyRecommendJGShining解答:int main(){int a,b,i,j,l[1000],k;scanf("%d",&i);getchar();for(j=1;j<=i;j++)l[j]=0;for(j=1;j<=i;j++){scanf("%d",&a);getchar();for(k=1;k<=a;k++){scanf("%d",&b);getchar();l[j]+=b;}}for(j=1;j<=i-1;j++)printf("%d\n\n",l[j]);printf("%d\n",l[i]);}2000 ASCII码排序Problem Description输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

河南acm试题及答案

河南acm试题及答案

河南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程序设计竞赛例题

ACM程序设计竞赛例题备战ACM资料一:知识点数据结构:1,单,双链表及循环链表2,树的表示与存储,二叉树(概念,遍历)二叉树的应用(二叉排序树,判定树,博弈树,解答树等)3,文件操作(从文本文件中读入数据并输出到文本文件中)4,图(基本概念,存储结构,图的运算)数学知识1,离散数学知识的应用(如排列组合、简单的图论,数理逻辑)2,数论知识3,线性代数4,组合代数5,计算几何二算法1,排序算法(冒抛法,插入排序,合并排序,快速排序,堆排序)2,查找(顺序查找,二分发)3,回溯算法4,递归算法5,分治算法6,模拟法7,贪心法8,简单搜索算法(深度优先,广度优先),搜索中的剪枝,A*算法9,动态规划的思想及基本算法10,高精度运算三、ACM竞赛的题型分析竞赛的程序设计一般只有16种类型,它们分别是:Dynamic Programming (动态规划)Greedy (贪心算法)Complete Search (穷举搜索)Flood Fill (不知该如何翻译)Shortest Path (最短路径)Recursive Search T echniques (回溯搜索技术)Minimum Spanning Tree (最小生成树)Knapsack (背包问题)Computational Geometry (计算几何学)Network Flow (网络流)Eulerian Path (欧拉回路)Two-Dimensional Convex Hull (不知如何翻译)BigNums (大数问题)Heuristic Search (启发式搜索)Approximate Search (近似搜索)Ad Hoc Problems (杂题)四ACM竞赛参考书《实用算法的分析与程序设计》(吴文虎,王建德著,电子工业出版社,竞赛类的黑宝书)《青少年国际和全国信息学(计算机)奥林匹克竞赛指导)――组合数学的算法和程序设计》(吴文虎,王建德著,清华大学出版社,参加竞赛组合数学必学)《计算机算法设计与分析》(王晓东编著,最好的数据结构教材)《数据结构与算法》(傅清祥,王晓东编著,我所见过的最好的算法教材)《信息学奥林匹克竞赛指导――1997-1998竞赛试题解析》(吴文虎,王建德著,清华大学出版社)《计算机程序设计技巧》 D.E.Kruth著,算法书中最著名的《葵花宝典》,大师的作品,难度大)《计算几何》周陪德著《ACM国际大学生程序设计竞赛试题与解析(一)》(吴文虎著,清华大学出版社)《数学建模竞赛培训教材》共三本叶其孝主编《数学模型》第二版姜启源《随机规划》《模糊数学》《数学建模入门》徐全智《计算机算法设计与分析》国防科大五常见的几个网上题库常用网站:1)信息学初学者之家:/doc/5a7376776.html/(2)大榕树编程世界:/doc/5a7376776.html/~drs/program/default.asp(3)中国教育曙光网:/doc/5a7376776.html/aosai/(4)福建信息学奥林匹克:/doc/5a7376776.html/fjas/index.htm(5)第20届全国青少年信息学奥林匹克竞赛:/doc/5a7376776.html/(6)第15届国际青少年信息学奥林匹克竞赛:/doc/5a7376776.html/(7)全美计算机奥林匹克竞赛:/doc/5a7376776.html/usacogate(8)美国信息学奥林匹克竞赛官方网站:/doc/5a7376776.html/(9)俄罗斯Ural州立大学:http://acm.timus.ru/(10)西班牙Valladolid大学:http://acm.uva.es/problemset (11)ACM-ICPC:/doc/5a7376776.html /icpc/(12)北京大学:/doc/5a7376776.html/JudgeOnline/index.acm(13)浙江大学:/doc/5a7376776.html/(14)IOI:http://olympiads.win.tue.nl/ioi/(15)2003年江苏省信息学奥林匹克竞赛夏令营:/doc/5a7376776.html(16)/doc/5a7376776.html(17)/doc/5a7376776.html(18)/doc/5a7376776.html(19)/doc/5a7376776.html/downldmanag/index.asp(20)/doc/5a7376776.htmlcolin_fox/colin_fox五如何备战ACM/ICPC1,个人准备(算法书,习题集,网上做题和讨论)2,1000题=亚洲冠军=世界决赛3,做好资料收集和整理工作实验一:递归与分治1. 二分查找2. 合并排序3. 快速排序实验二:回溯1. 0-1背包问题2. 装载问题3. 堡垒问题(ZOJ1002)4. *翻硬币问题5. 8皇后问题6. 素数环问题7. 迷宫问题8. *农场灌溉问题(ZOJ2412)9. *求图像的周长(ZOJ1047)10. *骨牌矩阵11. *字母转换(ZOJ1003)12. *踩气球(ZOJ1004)实验三:搜索1. Floodfill2. 电子老鼠闯迷宫3. 跳马4. 独轮车5. 皇宫小偷6. 分酒问题7. *找倍数8. *8数码难题实验四:动态规划1. 最长公共子序列2. 计算矩阵连乘积3. 凸多边形的最优三角剖分4. 防卫导弹5. *石子合并6. *最小代价子母树7. *旅游预算8. *皇宫看守9. *游戏室问题10. *基因问题11. *田忌赛马实验五:贪心与随机算法1. 背包问题2. 搬桌子问题3. *照亮的山景4. *用随即算法求解8皇后问题5. 素数测试实验一:递归与分治实验目的理解递归算法的思想和递归程序的执行过程,并能熟练编写递归程序。

ACM程序设计竞赛例题[1]

ACM程序设计竞赛例题[1]

备战ACM 资料习题1.0-1 背包问题在0 / 1 背包问题中,需对容量为 c 的背包进行装载。

从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。

对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。

程序如下:#include <stdio.h>void 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++){if(a[i]==1) // 如果选取了该物品{weight = weight + w[i]; // 累加重量value = value + v[i]; // 累加价值}}if(weight<=c) // 若为可行解if(value>max) // 且价值大于maxmax=value; // 替换m ax}void readdata(){int i;for(i=0;i<n;i++)scanf("%d%d",&w[i],&v[i]); // 读入第i 件物品重量和价值 }void printresult(){printf("%d",max);}2.装载问题是c1、c2,n 个集装箱,重量是wi (i=1⋯n),且所有集有两艘船,载重量分别c1+c2。

acm考试题目及答案

acm考试题目及答案

acm考试题目及答案1. 题目:给定一个整数数组,找出数组中没有出现的最小的正整数。

答案:首先,我们可以遍历数组,将每个元素与它的索引对应起来,即如果数组中存在数字`i`,则将其与索引`i-1`对应。

然后,我们可以遍历数组,检查索引`i`是否与数组中第`i`个元素相等。

如果不相等,则索引`i`对应的值就是没有出现的最小正整数。

如果所有元素都与其索引对应,则没有出现的最小正整数为数组长度加1。

2. 题目:实现一个函数,检查一个链表是否为回文结构。

答案:我们可以将链表的前半部分反转,然后比较反转后的前半部分与后半部分是否相同。

如果相同,则链表是回文的;如果不相同,则不是。

具体步骤如下:首先找到链表的中点,然后反转前半部分链表,接着比较反转后的前半部分与后半部分是否相同,最后将前半部分链表再次反转回来。

3. 题目:给定一个只包含 '(' 和 ')' 的字符串,判断字符串是否有效。

答案:我们可以使用一个栈来解决这个问题。

遍历字符串中的每个字符,如果遇到'(',则将其压入栈中;如果遇到')',则检查栈是否为空,如果为空,则字符串无效;如果不为空,则弹出栈顶元素。

遍历结束后,如果栈为空,则字符串有效;如果栈不为空,则字符串无效。

4. 题目:找出一个无序数组中第k大的元素。

答案:我们可以使用快速选择算法来解决这个问题。

首先,选择一个元素作为基准,然后将数组分为两部分:一部分是大于基准的元素,另一部分是小于基准的元素。

根据基准的位置,我们可以确定第k大的元素是在基准的左边还是右边,然后递归地在相应的部分中寻找第k大的元素。

重复这个过程,直到找到第k大的元素。

5. 题目:给定一个字符串,找出其中不含有重复字符的最长子串的长度。

答案:我们可以使用滑动窗口的方法来解决这个问题。

维护一个窗口,记录窗口内字符的出现情况。

遍历字符串,如果遇到重复的字符,则移动窗口的左边界,直到窗口内没有重复的字符。

ACM程序设计试题及参考答案

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<<"只。

acm大赛历年程序题

acm大赛历年程序题

acm大赛历年程序题ACM国际大学生程序设计竞赛(The ACM International Collegiate Programming Contest)是全球范围内最具声誉的大学生程序设计竞赛之一。

每年都有来自世界各地的顶尖大学参加这一比赛,他们将在规定的时间内解决一系列编程问题,以展示他们的算法和编程技巧。

历年来,ACM大赛的程序题目一直是各个大学的计算机科学学生学习和训练的重要素材。

ACM大赛历年程序题的设计旨在考察参赛者的算法设计与实现能力。

这些问题通常具有一定的难度,涵盖了多种算法和数据结构。

在ACM大赛中,选手需要在规定的时间内,根据给定的输入数据,编写程序解决问题,并输出正确的结果。

ACM大赛历年程序题通常分为多个分类,下面将列举几个常见的分类及其特点:1. 图论问题:图论是ACM大赛中常见的题目类型之一。

这类问题涉及到对图的建模和算法设计。

参赛者需要熟悉常见的图观念和算法,如图的遍历、最短路径、最小生成树等。

2. 动态规划问题:动态规划是ACM大赛中常用的解决问题的方法之一。

动态规划问题通常需要设计状态转移方程,并根据之前已经计算过的结果来推导最优解。

这类问题要求选手具备良好的逻辑思维和数学推导能力。

3. 贪心算法问题:贪心算法是一种简单而高效的算法思想。

贪心算法问题一般需要选手根据问题的特性,每次都选择当前情况下最优的解决方案。

这类问题在实际应用中非常常见,选手需要能够灵活地运用贪心策略解决问题。

4. 字符串处理问题:字符串处理问题涉及到对字符串进行各种操作,如匹配、查找、替换等。

选手需要熟练掌握字符串的各种操作和常见算法,如KMP算法、Boyer-Moore算法等。

5. 数学问题:数学问题在ACM大赛中也是常见的题目类型。

这类问题通常涉及到各种数学公式和算法,如排列组合、素数判定、快速幂等。

选手需要具备扎实的数学知识和计算能力。

ACM大赛历年程序题的学习对于计算机科学学生来说是非常重要的。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

搜集的南开大学的ACM试题与你共享[A]南开大学Onlinejudge 在线判题系统A.Lucy的新难题时间限制:2秒内存限制:32000KB不知不觉,南开大学第三届“我为程序狂”又要拉开帷幕了。

这天,Lucy也来到南开大学ACM协会,与大家共同欢庆NKPC的三周岁的日子。

谈笑间,ACM协会的主席拿了圆形的生日蛋糕。

大伙开心地唱完了生日歌,一起吹灭了蜡烛。

要分蛋糕了,大家都很兴奋。

本着公平的原则,每位到场的人员都要在蛋糕上切一刀。

ACM协会的主席事先知道有n位朋友会参与这个欢庆宴会。

为了方便大家切蛋糕,主席在订蛋糕的时候就嘱咐在蛋糕的边缘布置上2n朵小花。

每个人切蛋糕都会从蛋糕的边缘的一朵小花笔直地切到蛋糕的另一端的小花,来表达自己对NKPC的祝福。

为了尊重其他同学,每个人在切蛋糕一定不会和蛋糕上已有的切痕相交,也不会从别人已切过的小花作为切蛋糕的起点或终点。

同时,每位同学在切蛋糕的时候,都要保证后面所有的同学都能够按照上述的规则切蛋糕。

这样,蛋糕上就留下n条切痕。

Lucy眨巴眨巴眼睛,问,要是不考虑切蛋糕的先后顺序和谁切的哪一刀,这蛋糕切完了共有多少种切法呢?大家听了呵呵一笑,说,那就把这个问题留给NKPC3,作为《Lucy的新难题》吧。

相信聪明的你,一定能够帮Lucy解答她的难题的,对吗?输入包括多组测试数据,你应当处理到输入结束为止。

每组输入数据中,都只有一行,仅包含一个正整数n,且0<n≤30。

对于每组输入数据,输出两行。

对于第i组输入数据,输出的第一行为”Case i:”,输出的第二行为切蛋糕的方法数。

[B]南开大学Onlinejudge 在线判题系统B.保龄球时间限制:2秒内存限制:32000KB南开大学ACM协会的一个元老毕业后,开了家保龄球馆。

他需要为他的保龄球馆的计算机写一个记分的程序。

一局(GAME)保龄球分为10格,每格里有两次投球机会,如在第一次投球时没能全中,就有需要投第二球。

每一格可能出现三种情况:1.失球(MISS)无论何种情况,在一格的两次投球时,未能击倒10个瓶,此格的分数为击倒的瓶数。

如果一次击球中未击倒一个瓶,则用一个’-’标记。

2.补中(SPARE)要一次击倒十个瓶子并非那么容易的!如果在第一次掷球后,你还有一次机会来击倒该格第一球所留下的情致。

当第二次投球击倒该格第一球余下的全部瓶子,称为补中,用一个‘/’符号表示。

补中的记分是10分加上下一次投球击倒的瓶数。

3.全中(STRIKE)当每一格的第一次投球击倒全部竖立的十个瓶时,称为全中,用一个(×)符号表示。

全中的记分是10分(击倒的瓶)加该球员下两次投球击倒的瓶数。

但在第十格中情况比较特殊:(1)如第二次投球未补中,则第十格得分为第九格得分加上第十格所击倒瓶数。

(2)如第二次投球补中,则追加一次投球机会,第十格得分为第九格得他加上10加上追加一次投球击倒瓶数。

(3)如第一球为全中,则追上加二次投球机会,第十格得分为第九格得分加上10加追加二次投球击倒的瓶数。

因此从第一格到第十格的两次追加投球,都为全中,则为12个全中,得分为满分300分。

每组输入数据中,都只有一行,包含一局的记分符号,相邻的两个符号之间以一个空格隔开。

记分的符号仅包括…-‟(不含对于每组输入数据,输出两行。

对于第i组输入数据,输出的第一行为”Case i:”,输出的第二行为10个整数,表示每格的累计得分。

相邻的两个得分以一个空格隔开。

[C]南开大学Onlinejudge 在线判题系统C.计算机硬件评分系统时间限制:2秒内存限制:32000KB小C听说微软新推出的一款操作系统Windows Vista可以对电脑的配置进行评分,很感兴趣。

由于小C对硬件软件方面都很了解,就通过一系列的市场调查与实践,制作了一款自己的计算机硬件评分系统。

此系统可以对CPU、内存、硬盘、主板、显卡五部分分别进行评分,分数为不超过100的正整数,并以这五个分数中的最低分作为对计算机的总体评分。

同寝室的小D打算最近购置一台新电脑,他请小C给他当参谋,小C就提供了一些当前的CPU、内存、硬盘、主板、显卡五种硬件的品牌、价格以及每个硬件由他所制作的系统所评价出来的分数。

小D准备至多用N元来购买这五种硬件并且他还希望能够得到一台电脑有尽量高的总体评分。

作为寝室长的你主动要写一个程序来帮助小D购买电脑。

表示资料中所提供的这种硬件品种数目M。

下面M行每一行都描述了一个品种的情况,包括两个整数,以空格隔开。

第一个费。

[D]南开大学Onlinejudge 在线判题系统D.朋友们的距离时间限制:2秒内存限制:32000KB要放假了,考完了所有试就等着上火车回家的Butterfly0923 面对着自己心爱的计算机,无聊的逛着我爱南开BBS,眼看着在线好友列表的长度一天天减少。

“来自”一栏中的IP 都还是校内的,大多以10 开头。

Butterfly0923 看到大部分人跟自己同在21 宿,还有15 宿的老乡、13 宿的狐朋狗友们、西区的研究生、12 宿的音乐狂人和8 宿的可爱小女生。

虽然现在大家仅仅相隔几百米,但是很快彼此将远隔万水千山,分散在祖国各地。

如果那时大家一起上线,各地IP 大展览,一定有趣,Butterfly0923 这样想。

不过祖国这么大,不知道大家到底相距多远。

于是Butterfly0923 给所有在线的ID寄语信鸽,询问他/她的家乡与学校之间的距离,他想知道自己的朋友中谁离学校最远。

Butterfly0923 知道自己的家距学校520km,seaeagle 也知道,所以他回信说:我家离学校的距离比你的四倍还多200km。

不过doraemonok 不知道,他比较务实,直接回信说:12km,公交1hour。

迷糊的Butterfly0923 想了想,觉得seaeagle 比较远一点。

但是随着问的人越来越多,问题变得复杂起来。

unusualwater 说:我的距离是你的十三分之一。

aliao 说:我刚问过gnr,你们的旅途合在一起比我少194km。

而gnr 老师的回信说:我的路程正好是你的两倍。

这回,Butterfly0923 算了好大一会,才知道aliao 最远。

Butterfly0923 最后寄语信鸽了爱好编程的你,不过不是问距离,他告诉你他从各个ID 得到的信息,希望你能告诉他最远的地方有多少公里,是谁在那里。

会超过26。

接下来n 行是这n 个在线ID,每行一个。

按照我爱南开BBS 站的规定,ID 由2-12 个英文字母组成,大小写不敏感。

再往下n 行是从各人回信中得到的信息。

方便起见,Butterfly0923 把每条信息写成等式的形式。

等号两边有一些项,每一项都是num*ID 的形式,num 是一个正整数,没有符号,多项之间以加号分隔,最后一项可能是一个常数。

Butterfly0923 自己也可能出现在等式中,注意它不是一个ID ——它太长了。

Butterfly0923 保证在一个等式中不会出现相同的ID,每个人的距离(如果能算得出的话)都是整数。

所有ID 和等式中都不会出现任何前导、后缀以及符号两侧的空格。

如果朋友们给Butterfly0923 的信息不足以确定全部人的距离,就在第二行输出“I‟m not sure about someone‟s distance.”否则第二行输出你计算出的最远距离,第三行输出跟最远距离对应的ID,如果这样的ID 不止一个,按照它们在[E]南开大学Onlinejudge 在线判题系统E.羽毛球轨迹估算时间限制:2秒内存限制:32000KBButterfly0923 喜欢打羽毛球,尽管打得不是很好。

某节杨明老师的羽毛球课上,刚刚打赢了Butterfly0923 的英语系老陈得意的问:“嘿,你知道羽毛球的英文叫做什么?”“嗯……feather ball 吧……”“No, badminton. Do you know why? 这是羽毛球起源的地方,是英国的一个小镇。

”这时杨老师开始讲课:“大家看,羽毛球的飞行轨迹是很有特点的,跟排球篮球足球不一样,羽毛球向上升到最高点,然后几乎垂直落下来。

”Butterfly0923 突然对老陈说:“告诉我初始的速度,高度和角度,我可以告诉你这个球可以飞多远。

”Butterfly0923 的想法是这样的:假定飞行的羽毛球只受到竖直向下的重力G,和与飞行速度v方向相反的空气阻力f,且空气阻力的大小f = k v,其中k 为常数。

设t = 0时球具有初速度v0,速度与水平方向夹角为α,击球高度为y0。

比如,当k = 0.018 时,一只重10g 的羽毛球在离地面2m的地方斜向上与水平方向成45°角,以20m/s 的速度飞出,按照这个模型,经过2.34 秒,羽毛球会落在大约7.74m 远的地方,如下图所示:Butterfly0923 对自己的模型还是很满意的,他正在尝试以不同的角度和力度打一些不同的球,老陈帮忙测量实际距离。

OK,你的任务是计算一下按照这个模型,Butterfly0923 打出去的球会落在离他多远的地方。

每组数据由五个数组成。

第一个数是击球的角度α,杨老师刚刚教了Butterfly0923 如何扣杀,所以α 可能是[-90, 90] 内的任何整数。

接下来是球的初速v0 (m/s)、击球点的高度y0 (m)、球的质量m (kg) 和阻力系数k,都是非负的浮点数,[F]南开大学Onlinejudge 在线判题系统F.找出公共区域时间限制:2秒内存限制:32000KBBetty喜欢玩剪纸。

现在她有两张已经剪好的凸多边形需要粘在一起。

她打算在两张纸的公共区域涂满浆糊,你能计算出这个公共区域有多大吗?输入由两部分组成,每部分的第一行为一个整数介于3和30之间,表示该凸多边形的顶点数目。

下面各行每行包含两个实数,表示凸多边形顶点的横纵坐标。

实数的小数部分为六位且绝对值均小于1000。

顶点顺序按逆时针给出。

请输出一个包含两位小数的实数,表示两个凸多边形的公共区域面积。

[G]南开大学Onlinejudge 在线判题系统G.游戏时间限制:2秒内存限制:32000KB我们在休闲娱乐的时候常常会去玩一些益智小游戏,像扫雷、祖玛、俄罗斯方块之类的恐怕已经没有几个人没玩过了吧。

像这样的游戏能让我们在学习的紧张过后得到一点放松,而且又能活动一下脑子。

现在许多RPG风格的游戏也逐渐的加入一些小游戏的元素来丰富游戏内容,像GBA游戏《被狙击的名侦探》,PC游戏《双星物语》等等就都出现了类似于下面这种样式的小游戏:在一个n×n的方格中,每个方格有一个棋子,棋子或为黑色、或为白色,如图中所示。

现在对棋盘进行操作,每步操作只能选中一个方格,之后这个方格及其上下左右四个相邻的方格(如果有的话)中的棋子均变为相反的颜色。

相关文档
最新文档