HDOJ 1216 Assistance Required
2015-12-17 22:29
459 查看
Assistance Required
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1782 Accepted Submission(s): 943
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
题意就是i从2开始 若i没被标记则每隔i个没被标记的数就标记下
模拟链表 AC CODE #include #include #include #include using namespace std; int main(){ list l; //空链表 list::iterator pos = l.begin(); // int lucky[3001]; int i(2); //建一个含默认值是2的元素的链表 for ( ; i != 35000; ++i) l.push_back(i); for (i = 0; i < 3001; ++i){ lucky[i] = l.front(); //front()返回第一个元素的引用 int key = lucky[i]; pos = l.begin(); // begin()返回第一个元素的指针 while (pos != l.end()){ pos = l.erase(pos); // 删除一个元素 int k = key - 1; while (k--) { if (pos == l.end()) //end()返回最后一个元素的下一位置的指针 break; ++pos; } } } int n; while (scanf("%d", &n), n){ printf("%d\n", lucky[n - 1]); } return 0; } 打表 AC CODE #include #define N 35000 int main(){ int arr = {0}; int lucky ; int i, j, k, n; n = 0; for (i = 2; i < N; i++){ if (arr[i] == 0){ lucky[n++] = i; k = i + 1; j = 0; while (k < N){ if (arr[k] == 0) j++; if (j == i){ arr[k] = 1; j = 0; } k++; } } } while (scanf("%d", &i), i){ printf("%d\n", lucky[i - 1]); } return 0; }
相关文章推荐
- HDOJ 2062 Subset sequence
- Unique Binary Search Trees,Unique Binary Search Trees II
- Scala之ListBuffer、ArrayBuffer、Queue及Stack介绍
- day04 循环 随机值 break和continue 循环嵌套 while 循环 do while
- Duilib源码分析(六)整体流程
- 特殊权限:SUID、SGID、STICKY
- How to use kingshard building a MySQL cluster
- UI基础-MVC、通知
- leetcode Unique Binary Search Trees II 答案详解
- 黑马程序员_java基础_GUI
- Your build settings specify a provisioning profile with the UUID
- 基础控件之UILabel、UIButton、UITextField、UIAlertView、 UIImageView
- UNIX入门之常用头文件apue.h (附带去除文档空格的代码)
- UIColor,CGColor,CIColor三者 区别和联系
- UIScrollView的滚动,停止(UIScrollViewDelegate)
- Sequence Trigger
- UIView 视图切换
- UICollectionView自定义cell详解+demo
- [Leetcode]Longest Consecutive Sequence
- UILabel的缩放动画效果