C语言acm竞赛习题集锦.doc
杭州电子科技大学 acm 习题精选 第 1 页 共 21 页 目录 1、 数塔问题 2 2、 并查集类问题 4 3、 递推类问题 9 4、 动态规划系列 10 5、 概率类题型 13 6、 组合数学类题型 15 7、 贪心策略 16 8、 几何问题 .19 杭州电子科技大学 acm 习题精选 第 2 页 共 21 页 数塔类问题 数塔 Problem Description 在讲述 DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少已经告诉你了,这是个 DP 的题目,你能 AC 吗 输入数据首先包括一个整数 C,表示测试实例的个数,每个测试实例的第一行是一个整数 N1 include define MAX 101 int arrMAXMAX2; void res int n; int i,j; memsetarr,0,MAX*MAX*sizeofint; scanf“d“, fori0;i0;i forj0;jarri1j11 arrij1arri1j1; else arrij1arri1j11; printf“dn“,arr001; int main int num; scanf“d“, 杭州电子科技大学 acm 习题精选 第 3 页 共 21 页 whilenum res; return 0; 免费馅饼 Problem Description 都说天上不会掉馅饼,但有一天 gameboy 正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来 gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的 10 米范围内。馅饼如果掉在了地上当然就不能吃了,所以 gameboy 马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于 gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条 小径如图标上坐标 为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在 0-10 这 11 个位置。开始时 gameboy 站在 5这个位置,因此在第一秒,他只能接到 4,5,6 这三个位置中期中一个位置上的馅饼。问 gameboy 最多可能接到多少个馅饼(假设他的背包可以容纳无穷多个馅饼) 输入数据有多组。每组数据的第一行为以正整数 n0 include define MAX 100001 int arrMAX13; int Maxint n1,int n2,int n3 int max; maxn1n2n1n2; maxmaxn3maxn3; return max; void resint num int i,j; int n,m,count-1; memsetarr,0,MAX*13*sizeofint; 杭州电子科技大学 acm 习题精选 第 4 页 共 21 页 fori0;i0;i forj1;j include define MAX 2000 int n,m; int arrMAX2; int resMAX; int set; void proc int i,j; int rest; set1; 用来统计集合个数 memsetres,0,n1*sizeofint; resarr00resarr011; fori1;i define MAX 2000 int n,m; int startMAX,endMAX; int res; int arrMAX; int len; int Mempty int i; fori0;i int main int64 i,arr51; int64 num; arr00; arr13; arr26; arr36; fori4;i int mainvoid int i, m, n; int64 a212 1,0,1,0,2,1,6,2; for i 4; i include define MAX 200 int arrMAX4; char strMAX; int letterchar ch ifchA ifarri3arri-123arri3 arri3arri-123; ifarri-13 ifarri1arri-132arri1 arri1arri-132; ifarri2arri-132arri2 arri2arri-132; else ifarri-10 arri1arri-102; arri2arri-102; ifarri-11 arri0arri-111; arri3arri-113; ifarri-12 ifarri1arri-122arri1 arri1arri-122; ifarri2arri-122arri2 arri2arri-122; ifarri-13 ifarri0arri-131arri0 arri0arri-131; ifarri3arri-133arri3 arri3arri-133; min3*MAX; ifletterstrlen-1 ifarrlen-10 tmparrlen-101; iftmp include define MAX 20000 int arrMAX; int main int num,temp,i,flage; int sum,start,end,max-32768; scanf“d“, whilenum0 memsetarr,0,MAX*sizeofint; fori0;i0 flage1; sumarri; ifmax include const double EPS 1e-12; inline void solveint n, int m, double p, double q ifn0 printf“0.00n“; else ifm0 printf“1.00n“; else ifp0.0q1.0 printf“0.00n“; else double lamda q*1-p/p*1-q; iffabslamda-1.0 include define MAX 1000 int arrMAX; long solveint n,int m int i,j; arr01; fori1;i-1;j ifj0ij arrj1; else arrjarrj-1; return arrm; int main 杭州电子科技大学 acm 习题精选 第 16 页 共 21 页 int num; int n,m; scanf“d“, memsetarr,0,MAX*sizeofint; whilenum scanf“dd“, printf“ldn“,solven,m; return 0; 贪心策略 FatMouse Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains Ji pounds of JavaBeans and requires Fi pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get Ji* a pounds of JavaBeans if he pays Fi* a pounds of cat food. Here a is a real number. Now he is assigning this homework to you tell him the maximum amount of JavaBeans he can obtain. The consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers Ji and Fi respectively. The last test case is followed by two -1s. All integers are not greater than 1000. Output For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain. Sample 5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1 Sample Output 13.333 31.500 include include define MAX 1000 int main 杭州电子科技大学 acm 习题精选 第 17 页 共 21 页 int i,j,m,n,temp; int JMAX,FMAX; double PMAX; double sum,temp1; scanf“dd“, whilem-1 memsetJ,0,MAX*sizeofint; memsetF,0,MAX*sizeofint; memsetP,0,MAX*sizeofdouble; fori0;i define MAX 200 int arrMAX2; int num; void res int i,j; int start; int count0,max-1; forinum-1;inum/2;i ifcountmax maxcount; count1; startarri0; forji-1;j0;j ifarrj1arrj0 arri0arrj0; arrj0arri0-arrj0; arri0arri0-arrj0; 杭州电子科技大学 acm 习题精选 第 19 页 共 21 页 arri1arrj1; arrj1arri1-arrj1; arri1arri1-arrj1; ifarri0arrj0 arrj1arri1-arrj1; arri1arri1-arrj1; res; int main scanf“d“, whilenum prc; scanf“d“, return 0; 几何问题 Rectangles Problem Description Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY . The first line of is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the two points on the first rectangle arex1,y1,x2,y2;the other two points on the second rectangle are x3,y3,x4,y4. Output Output For each case output the area of their intersected part in a single line., accurate up to 2 decimal places. Sample 1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50 Sample Output 1.00 56.25 include void resdouble x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4 double tmpx1,tmpy1,tmpx2,tmpy2; 杭州电子科技大学 acm 习题精选 第 20 页 共 21 页 double tmpx,tmpy; tmpxx1x2; x1x1x2x2x1; x2tmpx-x1; tmpyy1y2; y1y1y2y2y1; y2tmpy-y1; tmpxx3x4; x3x3x4x4x3; x4tmpx-x3; tmpyy3y4; y3y3y4y4y3; y4tmpy-y3; tmpx1x1x3x1x3; tmpx2x2x4x4x2; tmpy1y1y3y1y3; tmpy2y2y4y4y2; iftmpx1 include include using namespace std; int mainvoid int n, x3, y3; double s; scanf“d“, while n swapy0, y1; swapx2, y2; if x2 y2 printf“.3fn“, sqrtpowx0-x1, 2powy0-y1, 2; else s sqrtdouble2.0*x2 x1 - x0 y2*y2-1/2.0 - x2*x21/2.0; for ; x2 y2; x2 s sqrtdouble2*x2*x22*x21; printf“.3fn“, s; return 0;