Assistance Required(打表)
来源:互联网 发布:2017淘宝小号注册 编辑:程序博客网 时间:2024/05/16 17:11
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.
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
1210200
Sample Output
232983
Source
University of Ulm Local Contest 2002
Recommend
Eddy
打表即可 ,很多网友说用链表模拟,我是直接用数组,效率也不低,140MS。
用一个数组ans储存3000个幸免参赛者的编号,设置布尔型数组que用以标记队伍中的哪些参赛者已经被征役去打杂,que[i] == false表明编号为i(注意第一个人的编号是2)的参赛者还未被征役,否则表明已经被选去洗碟子。
AC CODE
#include <iostream>#include <cstdio>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<<endl; while(scanf("%d", &i) && i) { printf("%d\n", ans[i-1]); } return 0;}
- Assistance Required(打表)
- HDOJ1216 Assistance Required 打表
- hdu 1216 Assistance Required(数学:打表)
- Assistance Required
- Assistance Required
- Assistance Required
- Assistance Required
- HDU1216--Assistance Required HDU(92)
- HDU 1216 Assistance Required(模拟)
- Assistance Required 特殊数组
- hdu 1216 Assistance Required
- HDU 1216 Assistance Required
- HDU 1216 Assistance Required
- HDOJ 1216 Assistance Required
- HDUOJ 1216 - Assistance Required
- hdu-1216-Assistance Required
- hdu 1216 Assistance Required
- ZOJ 1978 && HDU 1216 && POJ 2552 Assistance Required (筛选)
- 在一串字符中查找到手机号码,并打印出来
- 23种设计模式的通俗理解
- CppUnit测试框架在VC2008中使用的注意事项
- 0 0
- exit()函数详解与Exit() 和 Return() 的区别
- Assistance Required(打表)
- 23种设计模式的通俗理解
- 关于 could not read symbols: File in wrong format 问题的解决方案
- 搜索比赛C题
- 从3DS MAX建模到Ogre实时动画的制作过程
- 单链表
- 删除oracle数据库用户的dba权限
- PostgreSQL查询优化:查询条件优化一(条件分类)
- SVN仓库迁移(将一个仓库从一台机器上上转移到另一台机器上)