博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Assistance Required(打表)
阅读量:6761 次
发布时间:2019-06-26

本文共 2552 字,大约阅读时间需要 8 分钟。

Assistance Required

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 719    Accepted Submission(s): 384


Problem Description
After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty dishes. The contestants would line up in a queue, one behind the other. Each contestant got a number starting with 2 for the first one, 3 for the second one, 4 for the third one, and so on, consecutively. 
The first contestant in the queue was asked for his number (which was 2). He was freed from the washing up and could party on, but every second contestant behind him had to go to the kitchen (those with numbers 4, 6, 8, etc). Then the next contestant in the remaining queue had to tell his number. He answered 3 and was freed from assisting, but every third contestant behind him was to help (those with numbers 9, 15, 21, etc). The next in the remaining queue had number 5 and was free, but every fifth contestant behind him was selected (those with numbers 19, 35, 49, etc). The next had number 7 and was free, but every seventh behind him had to assist, and so on. 
Let us call the number of a contestant who does not need to assist with washing up a lucky number. Continuing the selection scheme, the lucky numbers are the ordered sequence 2, 3, 5, 7, 11, 13, 17, etc. Find out the lucky numbers to be prepared for the next contest party. 
 

Input
The input contains several test cases. Each test case consists of an integer n. You may assume that 1 <= n <= 3000. A zero follows the input for the last test case. 
 

Output
For each test case specified by n output on a single line the n-th lucky number. 
 

Sample Input
 
1 2 10 20 0
 

Sample Output
 
2 3 29 83
 

Source
 

Recommend
Eddy
 

打表即可 ,很多网友说用链表模拟,我是直接用数组,效率也不低,140MS。

用一个数组ans储存3000个幸免参赛者的编号,设置布尔型数组que用以标记队伍中的哪些参赛者已经被征役去打杂,que[i] == false表明编号为i(注意第一个人的编号是2)的参赛者还未被征役,否则表明已经被选去洗碟子。

AC CODE

#include 
#include
using namespace std;int ans[3001] = {0};bool que[40010] = {1, 1, 0};int main(){ int k, i, j, cnt; for(k = 0; k < 3001; k++) { //这个for loop找到下一个未被选中的人 for(i = ans[k-1]+1; que[i]; i++){} ans[k] = i; for(j = i+1, cnt = i; j < 40010; j++) { if(!que[j] && !--cnt) { que[j] = 1; cnt = i; } } } // for(i = 1; i < 20; i++) cout<< ans[i] << " "; // cout<

转载地址:http://apfeo.baihongyu.com/

你可能感兴趣的文章
gradle下载及配置
查看>>
maven中央仓库、远程仓库地址
查看>>
PRD
查看>>
JAVASCRIPT 转换字符串杂记
查看>>
【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(7.1) 模块管理,验证权限,展示模块列表...
查看>>
jquery鼠标悬停事件hover()
查看>>
hql查询语句 内存中的情况,fetch迫切查询关键字
查看>>
RabbitMQ安装,Windows下
查看>>
0415评论博客
查看>>
CSAPP buffer lab记录——IA32版本
查看>>
Hyperledger fabric多机的环境部署
查看>>
关于sqlserver2008 bcp根据数据表导出xml格式文件的小记
查看>>
总结:栈和队列的学习
查看>>
装个centos虚拟机之设置桥接网络
查看>>
线段树(可能还会有树状数组吧)
查看>>
dedecms2
查看>>
O365批量重置用户密码
查看>>
Gartner:安全服务市场预测2011-2015
查看>>
约瑟夫·奈:透视网络空间
查看>>
在VS2012中实现Ext JS的智能提示太简单了
查看>>