电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

acm算法经典例题

79页
  • 卖家[上传人]:小**
  • 文档编号:89488852
  • 上传时间:2019-05-25
  • 文档格式:DOC
  • 文档大小:810.90KB
  • / 79 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 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 example, 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

      2、 with a positive integer P which indicates the number of test cases. Then on the following P lines,each line consists 2 positive integer m and n(0m,n2147483648).输出For each input m n, if safe holes exist, 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就安全。输入第一行一个数字p,表示下面有p组测试数据,每组测试数据2个数m n(0m,n2147483648)输出每组数据输出一行,如果存在安全洞穴,输出YES,否则输出NO思路/方法:你是不是觉得总是无法通过,看看下面的解析假

      3、设有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的循环,此时没有安全洞穴。解题关键:这题就转化成了判断两个数是否有公约数。代码:#include using namespace std;long long greatestCommonDivisor(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(greatestCommonDivisor(m, n) = 1)/公约数为1说明互斥,没有安全洞穴 cout NOn; else cout YESn;

      4、return 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次方,发现周期都是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+

      5、) t = t * a; t = t % 10; printf(%dn, t); return 0;3: 筛选法求素数描述请使用筛选法输出a, b之间的所有素数。输入输入数据有多组,每组数据占一行,每行2个正整数a和b,其中2=a=b=1000000。输出每组数据按从小到大的顺序输出a, b中所有的素数,每行最多输出10个素数。每组数据之后空一行。样例输入2 32 50样例输出2 3 2 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;3.把素数的倍数挖掉,改为0。(从2开始到根号1000000之间的素数的倍数挖掉)for(i = 2; i 1000; i+)/小于根号1000000 if(si)/判断是否为素数

      6、,只有素数的倍数才挖掉 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也是素数 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(

      7、%d, i); printf(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 the line. Others have to leave the line. They continue doing this till the number of people in the line is less than m. For example, 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 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 on

      《acm算法经典例题》由会员小**分享,可在线阅读,更多相关《acm算法经典例题》请在金锄头文库上搜索。

      点击阅读更多内容
    最新标签
    发车时刻表 长途客运 入党志愿书填写模板精品 庆祝建党101周年多体裁诗歌朗诵素材汇编10篇唯一微庆祝 智能家居系统本科论文 心得感悟 雁楠中学 20230513224122 2022 公安主题党日 部编版四年级第三单元综合性学习课件 机关事务中心2022年全面依法治区工作总结及来年工作安排 入党积极分子自我推荐 世界水日ppt 关于构建更高水平的全民健身公共服务体系的意见 空气单元分析 哈里德课件 2022年乡村振兴驻村工作计划 空气教材分析 五年级下册科学教材分析 退役军人事务局季度工作总结 集装箱房合同 2021年财务报表 2022年继续教育公需课 2022年公需课 2022年日历每月一张 名词性从句在写作中的应用 局域网技术与局域网组建 施工网格 薪资体系 运维实施方案 硫酸安全技术 柔韧训练 既有居住建筑节能改造技术规程 建筑工地疫情防控 大型工程技术风险 磷酸二氢钾 2022年小学三年级语文下册教学总结例文 少儿美术-小花 2022年环保倡议书模板六篇 2022年监理辞职报告精选 2022年畅想未来记叙文精品 企业信息化建设与管理课程实验指导书范本 草房子读后感-第1篇 小数乘整数教学PPT课件人教版五年级数学上册 2022年教师个人工作计划范本-工作计划 国学小名士经典诵读电视大赛观后感诵读经典传承美德 医疗质量管理制度 2 2022年小学体育教师学期工作总结 2022年家长会心得体会集合15篇
    关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
    手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
    ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.