四川理工学院2012年ACM程序设计赛试题

合集下载

2012秋C程序设计试卷+答案

2012秋C程序设计试卷+答案
voidmain()
{ int t,d;
float p;
scanf("%d,%f",&t,&p);
switch(04)){
case 0: d=0;break;
case 1: d=2;break;
case 2:
case 3:d=5;break;
case 4:
case 5:
case 6: d=8;break;
C)有逻辑类型,但没有构造类型D)没有逻辑类型也没有构造类型9;\"'B)'\031' C)'\b' D)'\0xa5'
24、已知梯形的上底为a,下底为b,高为h,用C语言写的正确的面积公式是()。
A)1/2*(a+b)*h B)1.0/2*(a+b)*h C)1.0/2.0(a+b)h D)1.0\2*a+b*h
B)main函数可以在任何地方出现。一个C程序必须有且仅有一个main函数
C)main函数必须出现在所有函数之后。一个C程序只能有一个main函数
D)main函数出现在固定位置。一个C程序可以有多个main函数
22、C语言中,下列叙述正确的是()。
A)没有逻辑类型,但有构造类型B)既有逻辑类型也有构造类型
34、若有int s[4]={1,2,3,4};,则s[s[0]+2*s[2]-s[3]]的值是()
A)1 B)2 C)3 D)4
35、C语言程序的三种基本结构是()
A)嵌套结构、递归结构、循环结构B)递归结构、循环机构、转移结构
C)顺序结构、选择结构、循环结构D)循环结构、转移结构、顺序结构
36、若函数的调用形式如下:

ACM试题及参考答案

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语言程序设计2012 (含答案)(1)

C语言程序设计2012 (含答案)(1)

12年的试题比13年的难很多,笔者费了一早上加一中午才全部做完。

如果对答案有疑问或者觉得哪里有问题请联系我qq553915228 李添锐一、写出下列程序段的运行结果(40分)1、(4分)char a='D'-'A',b='\010',c,d;c = ++a || b++;d = a-- && b--;printf("%d,%d,%d,%d",a,b,c,d);输出:3,7,1,12、(4分)char a=0x123FFF;printf("%d,",a);a=127;printf("%d",++a);输出:-1,-1283、(4分)int a=5,y=0;int main(){while(a=5){a+=5;y+=a;printf("%d,%d,", a, y);if (y>18) break;}return 0;}输出:10,10,10,204、(4分)int i=1, j=2, k=4;printf("%f,", i-(float)j/k);printf("%d,", j>1<k);k+=5/10*i ;printf("%d,",k);printf("%d", (j=1)?2:0);输出:0.500000,1,4,25、(4分)int a[][2]={(1,2),(3,4)};printf("%d",a[0][1]);输出:46、(4分)int ave(int x, int y){return (x+y)/2;}int main(){int a=1, b=2, c=3;printf("%d\n",ave(ave(a,b), ave(b,c)));return 0;}输出:1(\n)7、(4分)void exc(int x, int *y){int t;t=x; x=*y; *y=t;}int main(){int a=5,b=8;exc(a,&b);printf("a=%d,b=%d",a,b); return 0;}输出:a=5,b=58、(4分)char str[10]="believe";char *p=str;*(str+5)=0;puts(2+p);输出:lie9、(4分)struct pt{int x; int y;} p[2]={1,3,5,7}; printf("%.3f",p[1].y/(float)p[0].y+ p[1].x/p[0].x);输出:7.33310、(4分)FILE *fp;char c=49;int d=50, e;fp=fopen("a.tmp","w"); fprintf(fp,"%c%d", c, d); fclose(fp);fp=fopen("a.tmp","r"); fscanf(fp,"%d",&e); printf("%d\n",e);输出:150(\n)二、改错题(20分)以下程序实现:从键盘输入若干个学生的成绩数据,将这些数据存到磁盘文件上,并求这些学生的最高成绩。

2012acm ICPC金华区域赛题目

2012acm ICPC金华区域赛题目

Problem A. MinesDescriptionTerrorists put some mines in a crowded square recently. The police evacuate all people in time before any mine explodes. Now the police want all the mines be ignited. The police will take many operations to do the job. In each operation, the police will ignite one mine. Every mine has its "power distance". When a mine explodes, any other mine within the power distance of the exploding mine will also explode. Please NOTE that the distance is Manhattan distance here.More specifically, we put the mines in the Cartesian coordinate system. Each mine has position (x,y) and power distance d.The police want you to write a program and calculate the result of each operation.InputThere are several test cases.In each test case:Line 1: an integer N, indicating that there are N mines. All mines are numbered from 1 to N.Line 2…N+1: There are 3 integers in Line i+1 (i starts from 1). They are the i-th mine’s position (xi,yi) and its power distance di. There can be more than one mine in the same point.Line N+2: an integer M, representing the number of operations.Line N+3...N+M+2 : Each line represents an operation by an integer k meaning that in this operation, the k-th mine will be ignited. It is possible to ignite a mine which has already exploded, but it will have no effect.1<=M<=N<=100000,0<=xi,yi<=10^9,0<=di<=10^9Input ends with N=0.OutputFor each test case, you should print ‘Case #X:’ at first, which X is the case number starting from 1. Then you print M lines, each line has an integer representing the number of mines explode in the correspondent operation.Sample Input30 0 01 1 22 2 23123Sample OutputCase #1:12Problem B. BatteryDescriptionRecently hzz invented a new kind of solar battery. The battery is so amazing that the electric power generated by it can satisfy the entire village. People in the villager are all very happy since they can get free and green energy from now on. But the manager of a power company is sorrow about this. So he plans to take some action to obstruct the battery.The battery can be regarded as a segment of L meters. And the manager plans to build n pillars on the battery.Like the picture above, the distance between pillar i and the battery's left end is Xi, and its height is Hi. The thickness of all pillars can be ignored. When the sunlight is slant, some part of the battery will be sheltered by the pillars.One meter battery exposed in the vertical sunlight for one hour will generate one unit of energy. If the sunlight is slant, the amount of energy generated should be multiplied by sinβ(βis the angle between sunlight and horizontal line).The sun rises from the infinite far left end of the horizon at 6 o’clock and goes down at the infinite far right end of the horizon at 18 o’clock. The sun is always infinite far away. So theπ at 12 o′clock.sunlight is parallel, and βis12Please calculate the amount of energy generated by the battery between t1 o’clock and t2 o’clock(6 ≤t1<t2≤18).InputThere are multiple test cases.For each test case:The first line contains two integer L(10≤L≤100,000)and N(4≤N≤1000),indicating the length of the battery and the number of pillars.The second line contains two integers, above mentioned t1 and t2(6≤t1<t2≤18).Then N lines follow, each containing two integers Xi(0≤Xi≤L) and Hi(1≤Hi≤1000), indicating the position and height of a pillar.It is guaranteed that no two pillars will be in the same position. It is also guaranteed that there is a pillar on both end of the battery.The input end with L=0, N=0.OutputFor each test case, you should output a line with the energy described above. Output should be rounded to 5 digits after decimal point.Sample Input10 414 170 25 18 310 10 0Sample Output7.97188Problem C. Magic Board DescriptionSths is a happy boy~Sths has got a Magic Board for his birthday gift! A Magic board is an N*M sized grids which were painted by black and white. According to the parity of N and M, the Magic Board would be a little different, but it can be guaranteed that any two adjacent grids are painted by different colors, and the amount of black grid is not less than white ones.The Magic Board is called MAGIC because it is formed by a Chain. A Chain is a set of grids in which a grid has at most 2 adjacent grids and there are only 2 grids which has only 1 adjacent grid. Those two special grids are called the endpoints of the Chain.The joint between two adjacent grids is very flexible. It can be rotate by any angle. Here’s an example of transforming a Chain into a Magic BoardThe Chain was connected by a Magic String. The existence of Magic String relies on the power of fengshui. But recently the fengshui in Beijing was ruined because there is a university installing air-conditionor (which also cause the HUGE RAIN in Beijing).So the Magic String disappears, and the Magic Board is totally fell apart.Sths feels upset, because he really likes the Magic Board (since it can form a lot of things). So he is thinking about how to reconstruct it. The only thing Sths has got now is the separated grids. But surprisingly, Sths finds out that there are differences between these grids.1.There are black grids and white grids.2.There are three different grids in the same color because the Magic String goes throughit in 3 different ways shown below:So there are 6 different kinds of grids. Now Sths has counted the amount of each kind of grids, he wants to know: by using the grids in his hand, how many kinds of legal Chains (which can form an N*M sized Magic Board) can be constructed.We shall say two Chains is the same if and only if the standard expression of these two Chains is the same.The standard expression is a set of numbers which decided by following method:1.Starting from one of a Chain's endpoint.2.Write down the color of the grids (1 for black and 0 for white) before direction changing.3.Write down 2 then change direction and repeat Step 2 until reaching another endpointof the Chain.4.Choose the expression which lexicographical lower between the two expressions justgenerated since there are two endpoints.For example, the standard expression of the example of “N=M=3” is “10120120120212”(another expression is “10212012012012”, which is lexicographical greater than standard expression). And the standard expression of the example of “N=M=4” is“01012010210120120120212”.InputThere are Multiple Test CasesFor each case, there will be six integer numbers in one line, N, M, BO, BA, WO, and WA, indicating the number of rows and columns, the amount of “Black and Opposite” grids, the amount of “Black and Adjacent” grids, the amount of “White and Opposite” grids, the amount of “White and Adjacent” grids.2<=N*M<=30The input end with a line of 0 0 0 0 0 0.OutputFor each case, output the kinds of legal Chains that can be constructed by given grids.Sample Input3 3 1 2 2 23 3 0 3 3 15 5 56 5 70 0 0 0 0 0Sample Output114Problem D. A very hard Aoshu problem DescriptionAoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students:Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations.InputThere are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END".OutputFor each test case , output a integer in a line, indicating the number of equations you can get.Sample Input1212123456661235ENDSample output22Problem E. WormsDescriptionWorms is a series of turn-based computer games. Players control a small platoon of earthworms across a deformable landscape, battling other computer- or player-controlled teams. The game feature bright and humorous cartoon-style animation and a varied arsenal of bizarre weapons.During the course of the game, players take turns selecting one of their worms. They then use whatever tools and weapons available to attack and kill the opponents’worms. Over fifty weapons and tools may be available each time a game is played, and differing selections of weapons and tools can be saved into a “scheme” for easy selection in future games.When most weapons are used, they cause explosions that deform the terrain, creating circular cavities. If a worm is hit by a weapon, the amount of damage dealt to the worm will be removed from the worm’s initial amount of health. When a worm fall into the water or its health is reduced to zero, it dies.In this problem, the terrain of a stone can be described as a simple polygon. The worms only use the time bombs. Once a time bomb is thrown, it is only attracted by the force of gravity. In other words, the flying track of the bomb is a parabola. When it reaches the stone (the polygon), the bomb does not blow off or stop immediately. It will still fly along the parabola regardless of the resistance of the stone due to its special character. The time bomb can only be triggered by the timer. When the preset time is used up, the bomb blows up and eliminates all the materials within its explosion range. You need to calculate the area of the eliminated materials of one explosion.InputThere are multiple test cases.The first line of a test case contains seven floating numbers x0, y0, v0, θ, t, g, R. (x0, y0) is the position of the worm who throws the bomb, which could be inside the polygon or outside the polygon (even in the sky or under the water). The initial value of velocity is v0 which forms aθ(0≤θ<90) a ngle with the positive direction of x-axis. The bomb is always thrown upwards. The preset time is t, which is also the time of flying. The value of acceleration of gravity of the worms’planet is g. The direction of gravity is the negative direction of y-axis. The explosion range is a circle and R is the radius.The second line contains an intege r n (3≤n≤100), which indicates the number of edges of the stone(simple polygon). The following n lines contain two real numbers xi and yi each, which describe the coordinates of a vertex. Two vertexes in adjacent lines are adjacent on the polygon.The input contains multiple test cases. It is ended by “0 0 0 0 0 0 0”.OutputFor each test case, output one line containing the area of the eliminated materials of the explosion rounded to two digits to the right of the decimal point.Sample Input0 0 15 45 10 2 103100 0200 0100 1000 0 0 0 0 0 0Sample Output228.74Problem F. Aeroplane chess DescriptionHzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the numbers on the faces are 1,2,3,4,5,6). When Hzz is at grid i and the dice number is x, he will moves to grid i+x. Hzz finishes the game when i+x is equal to or greater than N.There are also M flight lines on the chess map. The i-th flight line can help Hzz fly from grid Xi to Yi (0<Xi<Yi≤N)without throwing the dice. If there is another flight line from Yi, Hzz can take the flight line continuously. It is granted that there is no two or more flight lines start from the same grid.Please help Hzz calculate the expected dice throwing times to finish the game.InputThere are multiple test cases.Each test case contains several lines.The first line contains two integers N(1≤N≤100000) and M(0≤M≤1000).Then M lines follow, each line contains two integers Xi,Yi(1≤Xi<Yi≤N).The input end with N=0, M=0.OutputFor each test case in the input, you should output a line indicating the expected dice throwing times. Output should be rounded to 4 digits after decimal point.Sample Input2 08 32 44 57 80 0Sample output1.16672.3441Problem G. GPADescriptionGPA(Grade-Point Average) is one way to measure students’ academic performance in PKU. Each course has an integer credit, ranges from 1 to 99. For each course, you will get a score at the end of the semester, which is an integer ranges from 0 to 100. Then you can calculate the Grade-Point of this course with the following formula. (Your score is x and your Grade-Point is p, using real arithmetic)p=4−3100−x2,60≤x≤100 p=0, 0≤x≤60Then you can get the GPA with the following formula (the Grade-Point of course i is p i, and the credit of course i is w i).GPA=p i∙w i mi=1w i mi=1Now it is not far from the final exam, if you do not review, you can only get a basic score in each course.You have n days to review. There are K classes in each day. For each class, only one course can be reviewed. After the review, your score in this course will exactly increase by 1. You can get more increment by spending more classes in this course. But the score may not exceed 100.For some reasons, not any course can be reviewed in any class. Each day you can only review some of the courses.Now you want your GPA to be as high as possible, and at the same time, you do not want to fail in any course. Please calculate the highest GPA you can get.InputThe input consists of several test cases. Each test case begins with 3 integers N (0<=N<=40), K(0<K<=20), M (0<M<=20), representing the number of days, the number of classes in each day and the number of courses. Next line contains M integers representing credits of each course and M integers representing basic scores of each course (0<=score<=100). Next N lines contain an N*M matrix, the j th element in i th row means whether you can review course j in i th day, 1 means you can review course j in i th day, 0 means you cannot. The Input ends with 0 0 0.OutputFor each test case, output the highest possible GPA, round to 6 digits after decimal point. If you have to fail a course, output 0.000000 instead.Sample Input2 10 31 1 250 60 901 1 01 0 12 20 41 1 1 150 50 50 401 1 1 00 0 0 10 0 0Sample Output2.7578130.000000Problem H. SumDescriptionXXX is puzzled with the question below:1, 2, 3, ..., n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds.Operation 1: among the x-th number to the y-th number (inclusive), get the sum of the numbers which are co-prime with p( 1 <=p <= 400000).Operation 2: change the x-th number to c( 1 <=c <= 400000).For each operation, XXX will spend a lot of time to treat it. So he wants to ask you to help him.InputThere are several test cases.For each case, the first line begins with two integers --- the above mentioned n and m.Each the following m lines contains an operation.Operation 1 is in this format: "1 x y p".Operation 2 is in this format: "2 x c".OutputFor each operation 1, output a single integer in one line representing the result.Sample Input13 32 2 31 1 3 41 2 3 6Sample Output7Problem I. Minimum Spanning Tree DescriptionXXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertexes and m (0<=m<=1000) edges, he wants to know the number of minimum spanning trees in the graph.InputThere are no more than 15 cases. The input ends by 0 0 0.For each case, the first line begins with three integers --- the above mentioned n, m, and p. The meaning of p will be explained later. Each the following m lines contains three integers u, v, w (1<=w<=10), which describes that there is an edge weighted w between vertex u and vertex v( all vertex are numbered for 1 to n) . It is guaranteed that there are no multiple edges and no loops in the graph.OutputFor each test case, output a single integer in one line representing the number of different minimum spanning trees in the graph.The answer may be quite large. You just need to calculate the remainder of the answer when divided by p (1<=p<=1000000000). p is above mentioned, appears in the first line of each test case.Sample Input5 10 122 5 32 4 23 1 33 4 21 2 35 4 35 1 34 1 15 3 33 2 30 0 0Sample Output 4Problem J. Family Name List DescriptionKong belongs to a huge family. Recently he got a family name list which lists all men (no women) in his family over many generations.The list shows that the whole family has a common ancestor, let's call him Mr. X. Of course, everybody except Mr.X in the list is Mr. X's descendant. Everybody's father is shown in the list except that Mr. X's father is not recorded. We define that Mr. X's generation number is 0. His son's generation number is 1.His grandson's generation number is 2, and so on. In a word, everybody's generation number is 1 smaller than his son's generation number. Everybody's generation number is marked in some way in the list.Now Kong is willing to pay a lot of money for a program which can re-arrange the list as he requires ,and answer his questions such as how many brothers does a certain man have, etc. Please write this program for him.InputThere are no more than 15 test cases.For each test case:The first line is an integer N( 1 <= N <= 30,000), indicating the number of names in the list.The second line is the name of Mr. X.In the next N-1 lines, there is a man's name in each line. And if the man's generation number is K, there are K dots( '.') before his name.Please note that :1) A name consists of only letters or digits( '0'-'9').2)All names are unique.3)Every line's length is no more than 60 characters.4)In the list, a man M's father is the closest one above M whose generation number is 1 lessthan M.5)For any 2 adjacent lines in the list, if the above line's generation number is G1 and the lowerline' s generation number is G2, than G2 <= G1 +1 is guaranteed.After the name list, a line containing an integer Q(1<=Q<=30,000) follows, meaning that there are Q queries or operations below.In the Next Q lines, each line indicates a query or operation. It can be in the following 3 formats: 1)LPrint the family list in the same format as the input, but in a sorted way. The sorted way means that: if A and B are brothers(cousins don’t count), and A's name is alphabetically smaller than B's name, then A must appear earlier than B.2) b namePrint out how many brothers does "name" have, including "name" himself.3) c name1 name2Print out the closest common ancestor of "name1" and "name2". "Closest" means thegeneration number is the largest. Since Mr. X has no ancestor in the list, so it's guaranteed that there is no question asking about Mr. X's ancestor.The input ends with N = 0.OutputAlready mentioned in the input.Sample input9Kongs.son1..son1son2..son1son1...sonkson2son1...son1son2son2..son1son3...son1son3son1.son07Lb son1son3son1b son1son2b sonkson2son1b son1c sonkson2son1 son1son2son2c son1son3son1 son1son2Sample outputKongs.son0.son1..son1son1...son1son2son2 ...sonkson2son1 ..son1son2..son1son3...son1son3son1 1322son1son1son1。

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竞赛基础训练题.docx

ACM竞赛基础训练题.docx

ACM竞赛基础训练题H【题目]】N皇后问题(八皇后问题的扩展)【题ri 2】排球队员站位问题【题H 3]把自然数N分解为若干个自然数之和。

【题H 4]把自然数N分解为若干个自然数之积。

【题H 5】马的遍历问题。

【题H 6】加法分式分解【题H 7】地图着色问题【题H 8]在n和的正方形屮放置长为2,宽为1的长条块,【题H 9】找迷宫的最短路径。

(广度优先搜索算法)【题Fl 10]火车调度问题【题H 11]农夫过河【题FI 12]七段数码管问题。

【题H 13]把1-8这8个数放入下图8个格中,要求相邻的格(横,竖,对角线)上填的数不连续. 【题FI 14]在4X4的棋盘上放置8个棋,要求每一行,毎一列上只能放置2个.【题H 15]迷宫问题.求迷宫的路径.(深度优先搜索法)【题H 16]一笔画问题【题H 17]城市遍历问题.【题H 18]棋子移动问题【题H 19]求集合元索问题(l,2x+l,3X+l类)【题RI N皇后问题(含八皇后问题的扩展,规则同八皇后):在N粒的棋盘上, 放置N个皇后,要求每一横行每一列,每一对角线上均只能放置一个皇后,问可能的方案及方案数。

const max=8;var i,j:integer;a: array [1.. max] of 0.. max; {放皇后数组}b:array[2.. 2*max] of boolean; {/对角线标志数组}c:array[-(max-1).. max-1 ] of boolean; {\对角线标志数组}col:array [1.. max] of boolean; {列标志数组}total: integer; {统计总数}procedure output; {输出}var i:inte^er;beginwrite (' No.' :4,' [', total+1:2,']');for i :=1 to max do write (a [i] :3) ;write(, ');if (to tal+1) mod 2 =0 t hen writ eln; inc (to tai);end;function ok(i, dep: integer) :boolean; {判断第dep 行第i 列可放否}begin ok:=false;if ( b[i+dep]二true) and ( c[dep-i]二true) {and (a[dep]=0)} and(col[i]=true) then ok:二trueend;procedure try (dep:integer);var i,j:integer;beginfor i:=l to max do 侮一行均有max种放法}i f ok (i, dep) then begi na [dep]:=i;b[i+dep]:二false; {/对角线已放标志}c[dep-i]:二false; {\对角线已放标志}col [i]:二false; {列已放标志}if dep二max then outputelse try (dep+1) ; {递归下一层}a[dep] :=0; {取走皇后,回溯}b[i+dep]:二true; {恢复标志数组}c[dep-i]:二true;col[i]:二true;end;end;beginfor i:=1 to max do begin a[i]:=0;col[i]:=true;end;for i:=2 to 2*max do b[i]:二true;for i:=-(max-1) to max~l do c[i]:=true;total:二0;try(l); writelntotal , total);end.【测试数据】n二8八皇后问题No.[ 1] 1 5 8 6 3 7 2 4 No. [ 2] 1 6 83 7 4 2 5 No.[ 3] 1 7 4 6 8 2 5 3 No. [ 4] 1 7 58 2 4 6 3 No.[ 5] 2 4 6 8 3 1 7 5 No. [ 6] 2 5 71 3 8 6 4 No.[ 7] 2 5 7 4 1 8 6 3 No. [ 8] 2 6 17 4 8 3 5 No.[ 9] 2 6 8 3 1 4 7 5 No. [10] 2 7 36 8 5 1 4No. [11] 2 7 5 8 1 4 6 3 No. [12] 2 8 61 3 5 7 4 No. [13] 3 1 7 5 8 2 4 6 No. [14] 3 5 28 1 7 4 6 No. [15] 3 5 2 8 6 4 7 1 No. [16] 3 5 71 4 2 8 6 No. [17] 3 5 8 4 1 7 2 6 No. [18] 3 6 25 8 1 7 4 No. [19] 3 6 2 7 1 4 8 5 No. [20] 3 6 27 5 1 8 4 No. [21] 3 6 4 1 8 5 7 2 No. [22] 3 6 42 8 5 7 1 No. [23] 3 6 8 1 4 7 5 2 No. [24] 3 6 81 5 7 2 4 No. [25] 3 6 8 2 4 1 7 5 No. [26] 3 7 28 5 1 4 6 No. [27] 3 7 2 8 6 4 1 5 No. [28] 3 8 47 1 6 2 5 No. [29] 4 1 5 8 2 7 3 6 No. [30] 4 1 58 6 3 7 2 No. [31] 4 2 5 8 6 1 3 7 No. [32] 4 2 73 6 8 1 5 No. [33] 4 2 7 3 6 8 5 1 No. [34] 4 2 75 1 8 6 3 No. [35] 4 2 8 5 7 1 3 6 No. [36] 4 2 86 1 3 5 7 No. [37] 4 6 1 5 2 8 3 7 No. [38] 4 6 82 7 1 3 5 No. [39] 4 6 8 3 1 7 5 2 No. [40] 4 7 18 5 2 6 3 No. [41] 47 3 8 2 5 1 6 No. [42] 4 7 52 6 1 3 8 No. [43] 4 7 5 3 1 6 8 2 No. [44] 4 8 13 6 2 7 5 No. [45] 4 8 1 5 7 2 6 3 No. [46] 4 8 53 1 7 2 6 No. [47] 5 1 4 6 8 2 7 3 No. [48] 5 1 84 2 7 3 6 No. [49] 5 1 8 6 3 7 2 4 No. [50] 5 2 46 8 3 1 7 No. [51] 5 2 47 3 8 6 1 No. [52] 5 2 61 7 48 3 No. [53] 5 2 8 1 47 3 6 No. [54] 5 3 16 8 2 4 7 No. [55] 5 3 1 7 28 6 4 No. [56] 5 3 84 7 1 6 2 No. [57] 5 7 1 3 8 6 4 2 No. [58] 5 7 14 2 8 6 3 No. [59] 5 7 2 4 8 1 3 6 No. [60] 5 7 26 3 1 4 8 No. [61] 5 7 2 6 3 1 8 4 No. [62] 5 7 41 3 8 6 2 No. [63] 5 8 4 1 3 6 2 7 No. [64] 5 8 41 7 2 6 3 No. [65] 6 1 5 2 8 3 7 4 No. [66] 6 2 71 3 5 8 4 No. [67] 6 2 7 1 4 8 5 3 No. [68] 6 3 17 5 8 2 4 No. [69] 6 3 1 8 4 2 7 5 No. [70] 6 3 18 5 2 4 7 No. [71] 6 3 5 7 1 4 2 8 No. [72] 6 3 58 1 4 2 7 No. [73] 6 3 7 2 48 1 5 No. [74] 6 3 72 8 5 1 4 No. [75] 6 3 7 4 1 8 2 5 No. [76] 6 4 15 8 2 7 3 No. [77] 6 4 2 8 5 7 1 3 No. [78] 6 4 71 3 5 2 8 No. [79] 6 4 7 1 8 2 5 3 No. [80] 6 8 24 1 7 5 3 No. [81] 7 1 3 8 6 4 2 5 No. [82] 7 2 41 8 5 3 6 No. [83] 7 2 6 3 1 4 8 5 No. [84] 7 3 16 8 5 2 4 No. [85] 7 3 8 2 5 1 6 4 No. [86] 7 4 25 8 1 3 6 No. [87] 7 4 2 8 6 1 3 5 No. [88] 7 5 31 6 8 2 4 No. [89] 8 2 4 1 7 5 3 6 No. [90] 8 2 53 1 7 4 6 No. [91] 8 3 1 6 2 5 7 4 No. [92] 8 4 13 6 2 7 5 total:92 对于N皇后:【题H 】排球队员站位问题I ----------------- 1图为排球场的平面图,其屮一、二、三、四、五、六为位 置编号, I |二、三、四号位置为前排,一、六、五号位为后排。

理工oj题目.(DOC)

理工oj题目.(DOC)

零起点学算法80——逆序输出(数组练习)Time Limit:1000MS Memory Limit:65536KTotal Submit:2994 Accepted:1369Description数组是在程序设计中,为了处理方便,把具有相同类型的若干变量按有序的形式组织起来的一种形式。

这些按序排列的同类数据元素的集合称为数组数组类型说明在C语言中使用数组必须先进行类型说明。

数组说明的一般形式为:类型说明符数组名[常量表达式],……;其中,类型说明符是任一种基本数据类型或构造数据类型。

数组名是用户定义的数组标识符。

方括号中的常量表达式表示数据元素的个数,也称为数组的长度。

例int a[10]; 说明整型数组a,有10个元素。

float b[10],c[20]; 说明实型数组b,有10个元素,实型数组c,有20个元素。

char ch[20]; 说明字符数组ch,有20个元素。

Input多组测试数据。

第一行输入一个整数T表示测试数据组数每组首先输入1个整数n,然后输入n个整数(不大于20)Output对于每组测试数据按照输入相反的顺序输出n个数据Sample Input231 2 352 3 1 4 5Sample Output3 2 15 4 1 3 2Source零起点学算法81——找出数组中最大元素的位置(下标值)Time Limit:1000MS Memory Limit:65536KTotal Submit:4376 Accepted:1535Description找出数组中最大的元素的下标。

Input多组测试,每组先输入一个不大于10的整数n然后是n个整数Output输出这n个整数中最大的元素及下标值Sample Input41 4 5 6Sample Output6 3Source零起点学算法82——数组中查找数Time Limit:1000MS Memory Limit:65536KTotal Submit:3001 Accepted:1393Description在给定的数组中查找一个数Input多组测试,每组第一行输入1个整数n(n<20),然后是n个整数第二行输入1个整数mOutput查找在第一行的n个整数中第一次出现数字m的下标位置并输出,如果没有找到则输出NoSample Input3 4 5 654 2 2 2 22Sample Output1Source零起点学算法83——数组中删数Time Limit:1000MS Memory Limit:65536KTotal Submit:2938 Accepted:961Description在给定的数组中删除一个数Input多组测试,每组第一行输入1个整数n(n<20),然后是n个整数第二行输入1个整数m删除在第一行的n个整数中第一次出现数字m并删除,然后按照顺序输出剩下的数,Sample Input4 1 2 3 43Sample Output1 2 4Hintm有可能在原数组中找不到,找不到则输出原数组Source零起点学算法84——数组中删数IITime Limit:1000MS Memory Limit:65536KTotal Submit:1411 Accepted:754Description在给定的数组中删除数Input多组测试,每组第一行输入1个整数n(n<20),然后是n个整数第二行输入1个整数mOutput删除在第一行的n个整数中的数字m(多个的话都要删除),然后按照顺序输出剩下的数,Sample Input5 1 2 3 4 33Sample Output1 2 4零起点学算法85——数组中插入一个数Time Limit:1000MS Memory Limit:65536KTotal Submit:1566 Accepted:546Description给定有序数组(从小到大),再给你一个数,要求插入该数到数组中并保持顺序Input多组测试,每组第一行输入一个整数n,然后是n个有序的整数第二行输入1个整数m和1个整数KOutput将整数m插入到原数组中保持顺序是升序,然后输出2行第一行是插入以后的数组第二行是插入以后的数组中下标值是K的数n m k不超过20Sample Input3 1 2 53 1Sample Output1 2 3 52Source零起点学算法87——打印所有低于平均分的分数Time Limit:1000MS Memory Limit:65536KTotal Submit:4940 Accepted:1717Description输入n个成绩,打印出所有低于平均分的分数(注意:ave = s/n中s 为float或ave = (float)s/n)。

2012年全国软件大赛真题及其答案

2012年全国软件大赛真题及其答案

2012年全国软件大赛真题及其答案第一题/*微生物增殖假设有两种微生物X 和YX出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍)。

一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1个Y。

现在已知有新出生的X=10, Y=89,求60分钟后Y的数目。

如果X=10,Y=90 呢?本题的要求就是写出这两种初始条件下,60分钟后Y的数目。

*/#include <stdio.h>int main(void){_int64 x = 10;_int64 y = 90;int time = 60;int time_t = time * 10;for (int t = 5; t <= time_t; t += 5){if (y <= 0){y = 0;break;}if (t % 5 == 0 && t % 10 != 0){y = y - x;}if (t % 30 == 0){x = x * 2;}if (t % 20 == 0){y = y * 2;}}printf("y = %d\n", y);return 0;}答案:094371840第二题:/*古堡算式福尔摩斯到某古堡探险,看到门上写着一个奇怪的算式:ABCDE * ? = EDCBA他对华生说:“ABCDE应该代表不同的数字,问号也代表某个数字!”华生:“我猜也是!”于是,两人沉默了好久,还是没有算出合适的结果来。

请你利用计算机的优势,找到破解的答案。

把ABCDE 所代表的数字写出来。

答案写在“解答.txt”中,不要写在这里!*/#include <stdio.h>const int TRUE = 1;const int FALSE = 0;int main(void){for (int i = 10000;i < 1000000; i++){int a[5] = {0};a[4] = i % 10;a[3] = i /10 %10;a[2] = i /100 % 10;a[1] = i / 1000 % 10;a[0] = i / 10000 % 10;bool Flag = TRUE;for (int j = 0; j < 5 && Flag; j++){for(int k = 0; k < 5 && Flag; k++){if (j != k && a[j] == a[k] ){Flag = FALSE;}}}if (!Flag)continue;int num = 10000 * a[4] + 1000 * a[3] + 100 * a[2] + 10 * a[1] + a[0];for (int j = 2; j < 10; j++){if ( i * j == num){printf("%d * %d = %d\n", i, j, num);}}}return 0;}答案:21978 * 4 = 87912第三题:/*/*比酒量有一群海盗(不多于20人),在船上比拼酒量。

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

2012年四川理工学院“达内杯”大学生程序设计竞赛试题Problem 1:SortingThere are 16 random sorting numbers from 0 to 15, and they could be converted to different binary numbers, which have 4 digits. Please code algorithm to sort them by the order that the first there digits of the former number is same to the last there digits of the following. The first number of the sorted is always the first given number. Sample Input:1 3 5 7 9 11 13 15 02 4 6 8 10 12 14Sample Output:1 2 4 9 3 7 15 14 13 10 5 11 6 12 8 0Problem 2:Solution of EquationIn the interval [0,1], please programming to give the real root, of which error is less than 10-3, of equation ax3+bx2+cx+d=0, where a, b ,c and d are real number. And print the real root or the character “no solution”.Sample Input:1:a =1 b =-1 c =-2 d =12:a =1 b =1 c =1 d =1Sample Output:1:x =0.4443359372:no solutionProblem 3:The TriangleDescription73 88 1 02 7 4 44 5 2 6 5(Figure 1 Number Triangle)Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.InputYour program is to read from standard input. The first line contains one integer n: the number of rows in the triangle. The following n lines describe the data of the triangle. The number of rows in the triangle is >= 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.OutputYour program is to write to standard output. The highest sum is written as an integer. Sample Input573 88 1 02 7 4 44 5 2 6 5Sample Output30Problem 4:Common SubsequenceDescriptionA subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = {x1, x2, ..., x m }, another sequence Z = { z1, z2, ..., z k } is a subsequence of X if there exists a strictly increasing sequence { i1, i2, ..., i k } of indices of X such that for all j = 1,2,...,k, z j= x ij. For example, Z = { B, C, D,B } is a subsequence of X = { A, B, C, B, D, A, B } with index sequence { 2, 3, 5, 7 }. Given two sequences X and Y ,the problem is to find the length of the maximum-length common subsequence of X and Y.InputThe program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.OutputFor each set of data, the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line. Sample InputABCFBC ABFCABPROGRAMMING CONTESTABCD MNPSample Output42Problem 5:When Can We Meet?The ICPC committee would like to have its meeting as soon as possible to address every little issue of the next contest. However, members of the committee are so busy maniacally developing (possibly useless) programs that it is very difficult to arrange their schedules for the meeting. So, in order to settle the meeting date, the chairperson requested every member to send back a list of convenient dates by E-mail. Your mission is to help the chairperson, who is now dedicated to other issues of the contest, by writing a program that chooses the best date from the submitted lists. Your program should find the date convenient for the most members. If there is more than one such day, the earliest is the best.InputThe input has multiple data sets, each starting with a line containing the number of committee members and the quorum of the meeting.N QHere, N, meaning the size of the committee, and Q meaning the quorum, are positive integers. N is less than 50, and, of course, Q is less than or equal to N.N lines follow, each describing convenient dates for a committee member in the following format.M Date1 Date2 ... DateMHere, M means the number of convenient dates for the member, which is an integer greater than or equal to zero. The remaining items in the line are his/her dates of convenience, which are positive integers less than 100, that is, 1 means tomorrow, 2 means the day after tomorrow, and so on. They are in ascending order without any repetition and separated by a space character. Lines have neither leading nor trailing spaces.A line containing two zeros indicates the end of the input.OutputFor each data set, print a single line containing the date number convenient for the largest number of committee members. If there is more than one such date, print the earliest. However, ifno dates are convenient for more than or equal to the quorum number of members, print 0 instead. Sample Input3 22 1 43 34 83 24 15 8 93 2 5 95 2 4 5 7 93 32 1 43 2 5 92 2 43 32 1 23 1 2 92 2 40 0Sample Output452Problem 6:Frame Polygonal LineYou are going to read a sequence of pairs of integer numbers. Each pair represents the Cartesian coordinates of a point in a 2-dimentional plane. The first number is the x coordinate, while the second is that of y. The sequence represents a polygonal line. Your task is to draw a rectangle with minimal length of sides that exactly surrounds the polygonal line. The sides of the rectangle are parallel to x- and y-axis, respectively.InputInput consists of several test cases. For each case, a sequence of coordinates is given. Each pair of x and y occupies a line, with |x| and |y| less than 2^31. The sequence is terminated with a pair of 0's. Note that (0, 0) will never be considered as a point on any of the polygonal lines. An empty polygonal line signals the end of input.OutputFor each test case, print in one line two pairs of numbers, which are the south-west and north-east corners of the surrounding rectangle. The numbers must be separated by one space as is indicated in the samples.Sample Input12 5623 5613 100 012 340 00 0Sample Output12 10 23 5612 34 12 34Problem 7:The Symbolic NumDescriptionWe represent a decimal numbers use the ten Arabic numeral 0~9 , but some people who live in a faraway tribe representing number use three marks …*‟,‟#‟,and …-… . The three marks represent the value 0, 1 and -1 respective. And In their system, every digital is three times larger than the digital on the right. So, the number ‘#*-‘ is 8 (because 8=1×9+0×3+-1×1),and the num‘-#’ is -2 ( -2= -1×3+1×1 ).Now you are request to program to read a number between -231~231-1 and output the form that used in the tribe.InputOne decimal number every lineOutputThe string that represent the number used in the tribe corresponding to the input Sample Input102-1742-2147483648Sample Output#*##--#*##---*-#*##*#***##---#-#--#----------------------------------------------------------------------------------------------------------------------#include<stdio.h>main(){int}Problem 8:Target numberYou get five integers as the operand, and another integer as the target number. Now you are required to perform proper arithmetic operation use the five numbers to let the result great than or equal the target number. And we need the optimalizing result which is the most close to the target number. You can use + - * / and ( ) to caculate, and all the intermediate result are required be integer , so some division is illegal. ( such as (2*2)/4 is legal , and 2*(2/4) is illegal. )InputThere is only one line which has 6 numbers in the input file. The front five numbers are operand Mi, 1<=Mi<=100, the last number is the target number T, 1<=T<=1000.OutputOnly one number, is the optimize targetSample input:1 2 3 7 100 573Sample output:573。

相关文档
最新文档