HDUOJ 1216 - Assistance Required
2016-07-28 12:39
405 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case specified by n output on a single line the n-th lucky number.
[align=left]Sample Input[/align]
1
2
10
20
0
[align=left]Sample Output[/align]
2
3
29
83
//用链表进行数表的构建很快,不会超时
//数组直接将数表存入再查询也可以
#include <cstdio>
#include <list>
#include <iterator>
using namespace std;
int main()
{
list<int> l;
list<int>::iterator pos = l.begin();
int a[3005];
for (int i = 2; i <= 35000; ++i)
l.push_back(i);
for (int i = 1; i <= 3000; ++i)
{
a[i] = l.front();
int key = a[i];
pos = l.begin();
while (pos != l.end())
{
pos = l.erase(pos);
int num = key - 1;
while (num--)
{
if (pos == l.end()) break;
++pos;
}
}
}
int n;
while (scanf("%d", &n) != EOF && n != 0)
printf("%d\n", a
);
return 0;
}
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case specified by n output on a single line the n-th lucky number.
[align=left]Sample Input[/align]
1
2
10
20
0
[align=left]Sample Output[/align]
2
3
29
83
//用链表进行数表的构建很快,不会超时
//数组直接将数表存入再查询也可以
#include <cstdio>
#include <list>
#include <iterator>
using namespace std;
int main()
{
list<int> l;
list<int>::iterator pos = l.begin();
int a[3005];
for (int i = 2; i <= 35000; ++i)
l.push_back(i);
for (int i = 1; i <= 3000; ++i)
{
a[i] = l.front();
int key = a[i];
pos = l.begin();
while (pos != l.end())
{
pos = l.erase(pos);
int num = key - 1;
while (num--)
{
if (pos == l.end()) break;
++pos;
}
}
}
int n;
while (scanf("%d", &n) != EOF && n != 0)
printf("%d\n", a
);
return 0;
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 2015年acm国内排名
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002