acm算法经典例题

上传人:大米 文档编号:510782141 上传时间:2023-05-29 格式:DOC 页数:82 大小:297.50KB
返回 下载 相关 举报
acm算法经典例题_第1页
第1页 / 共82页
acm算法经典例题_第2页
第2页 / 共82页
acm算法经典例题_第3页
第3页 / 共82页
acm算法经典例题_第4页
第4页 / 共82页
acm算法经典例题_第5页
第5页 / 共82页
点击查看更多>>
资源描述

《acm算法经典例题》由会员分享,可在线阅读,更多相关《acm算法经典例题(82页珍藏版)》请在金锄头文库上搜索。

1、-一、数论1: Wolf and Rabbit描述There is a hill with n holes around. The holes are signed from 0 to n-1.A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For e*ample,

2、m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes.输入The input starts with a positive integer P which indicates the number of test cases. Then on the following P lines,

3、each line consists 2 positive integer m and n(0m,n2147483648).输出For each input m n, if safe holes e*ist, you should output YES, else output NO in a single line.样例输入21 22 2样例输出NOYES翻译:描述一座山有n个洞,洞被标记为从0到n-1。兔子必须藏在一个洞中。狼会逆时针方向搜索兔子。狼一开场在洞0,然后他会每m个洞进去一次。例如:m=2,n=6,狼就会依次进入洞0 2 4 0。如果兔子藏在1 3 5就平安。输入第一行一个数字

4、p,表示下面有p组测试数据,每组测试数据2个数m n(0m,n2147483648)输出每组数据输出一行,如果存在平安洞穴,输出YES,否则输出NO思路/方法:你是不是觉得总是无法通过,看看下面的解析假设有n=6个洞 0 1 2 3 4 5当m=4时,狼进的洞是0 4 2 0,也就是形成了一个循环,永远也到不了其他洞穴当m=5时,狼进的洞是0 5 4 3 2 1 0,这时所有的洞都遍历了一遍。思考:当m=4和m=5时,到底有什么区别?当n和m有公约数(非1)时,就会形成一个数字个数小于n的循环当n和m无公约数时,就会形成一个数字个数为n的循环,此时没有平安洞穴。解题关键:这题就转化成了判断两个

5、数是否有公约数。代码:#include using namespace std;long long greatestmonDivisor(long long a, long long b)/最大公约数 long long t; while(b) t = a % b; a = b; b = t; return a;int main() int i, p; long long m, n; cin p; for(i = 0; i m n; if(greatestmonDivisor(m, n) = 1)/公约数为1说明互斥,没有平安洞穴 cout NOn; else cout YESn; retur

6、n 0;2: ab描述给定a和b,输出ab的最后一个数字。输入输入数据有多组,每组数据占一行,每行为a和b的值0a,b=230输出对每组输入数据,输出ab的最后一位数字,每组数据占一行。样例输入2 23 4样例输出41思路/方法:如果你模拟ab次肯定会超时,而且数字还会超出Int围题目说只要最后一个数字,回想一下小学时学的乘法过程,貌似乘数最后一位和前面的无关我们大胆的尝试一下用a的个位代替a,然后我们发现循环b次还是会超时,这是我们要想方法减少循环的次数,试一下是不是有周期规律这时我们来列举一下2的n次方的个位:2 4 8 6 2 4 8 6我们发现周期为4,我们在试试1-9的n次方,发现周

7、期都是4,所以,我们可以用b%4代替b,需要注意的是,当b%4=0时,我们需要给他加上4,不然就不循环了。代码:#include int main() int a, b, i, t; while(scanf(%d %d, &a, &b) != EOF) b = b % 4; if(b = 0) b = 4; a = a % 10; t = 1; for(i = 0; i b; i+) t = t * a; t = t % 10; printf(%dn, t); return 0;3: 筛选法求素数描述请使用筛选法输出a, b之间的所有素数。输入输入数据有多组,每组数据占一行,每行2个正整数a和

8、b,其中2=a=b=1000000。输出每组数据按从小到大的顺序输出a, b中所有的素数,每行最多输出10个素数。每组数据之后空一行。样例输入2 32 50样例输出2 32 3 5 7 11 13 17 19 23 2931 37 41 43 47思路/方法:这题测试的数据量很大,所以尽量少循环,尽量少判断,要非常精简才能通过。1.定义一个全局变量short s1000001;/全局变量默认为02.把数组中下标为奇数的值改为1,偶数不用改,因为除了2,其他偶数都不是素数s2 = 1;/2也是素数for(i = 3; i 1000001; i = i + 2)/把奇数全部假设为素数 si = 1

9、;3.把素数的倍数挖掉,改为0。(从2开场到根号1000000之间的素数的倍数挖掉)for(i = 2; i 1000; i+)/小于根号1000000 if(si)/判断是否为素数,只有素数的倍数才挖掉 for(j = i * 2; j 1000001; j = j + i)/把i的倍数的值改成0 sj = 0;4.最后一点是输出格式,每组之间一个空行,另外一行最多10个。定义一个变量来记录输出了多少个,到达十个就输出换行。具体看代码。代码:#include short s1000001;/全局变量默认为0int main() int t, a, b, i, j; s2 = 1;/2也是素数

10、 for(i = 3; i 1000001; i = i + 2)/把奇数全部假设为素数 si = 1; for(i = 2; i 1000; i+)/小于根号1000000 if(si) for(j = i * 2; j 1000001; j = j + i)/把i的倍数的值改成0 sj = 0; while(scanf(%d %d, &a, &b) != EOF) t = 0; for(i = a; i = b; i+) if(si)/素数就输出 if(t) if(t = 10) printf(n); t = 0; else printf( ); t+; printf(%d, i); pr

11、intf(nn); return 0;4: The ones to remain描述There are N soldiers standing in one line. They are marked from 1 to N, from right to left. And they are given a number m. Then the soldiers numbered off, straight from the right-hand man. The one who reported a number that is the multiple of m was kept in t

12、he line. Others have to leave the line. They continue doing this till the number of people in the line is less than m. For e*ample, if there are 10 soldiers, and m = 3. For the first time the soldiers who are marked 3, 6, 9 remain in the line. For the second time the soldier who is marked 9 remains

13、in the line. Because the number of soldiers in the line is less than m, so the soldier marked 9 was the only one to remain in the line. Now we want to know who will be the ones to remain, can you tell us ?输入There are several test cases in the input. Each test cases is only one line, contains two integers n and m.(3 = n = 109, 2 = m = n). The input ends when n = 0 and m = 0.输出For each test case, output

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号