Assistance Required

来源:互联网 发布:淘宝网店标志图片 编辑:程序博客网 时间:2024/05/17 01:03
Assistance Required(http://acm.hrbeu.edu.cn/index.php?act=problem&id=1087)TimeLimit: 1 Second   MemoryLimit: 32 MegabyteTotalsubmit: 437   Accepted: 184  DescriptionAfter 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.InputThe 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.OutputFor each test case specified by n output on a single line the n-th lucky number.Sample Input1210200Sample Output232983SubmitStatus



#include <iostream>using namespace std;#define N 35000//程序是网上的,我加了注释//这个题的意思是:我是个开校车的,现在有一堆学生想上我的车,每个学生都已经把车_票_钱给我了,//只是我还没给他们车_票呢(学生信任我这个开校车的).//但我的车放不下那么多人,怎么办呢?//我是绝对不会给他们退钱的!////我给假_票!//学生们已经自觉地排好队了,等着我发票,我让他们从头开始报数,第一个人报2,接下去。//我发假_票的规则是这样的:如果序号为2的学生拿到了真票,那么从序号为2的同学后那些没拿到票的第2个//同学、第2+2个同学、第2+2+2个同学...给假_票。  如果序号为5的同学拿到了真票,那么从序号为5的同学//后那些没拿到票的第5个、第5+5个同学...给假_票...int main(){int arr[N]={0};//arr[]就是这堆学生,arr[0]和arr[1]空着没人,从arr[2]开始。               //如果arr[i] = 0,表示我还没给arr[i]发票。               //如果arr[i] = 1,表示我给了他一张假_票,我发完票要验过票才让你上车,假_票是不能上车的! int lucky[N];  //这个是拿到真票的学生的序号,lucky[0] = 2:第0个拿到真票的学生的序号是2。               //待会上车的时候,学生要老实交代自己的序号,如果序号在这个记录里,我就让他上车,               //如果不再这个记录里,我就说他的票的假的,不让上车,他跟我争论说票明明是我给的...               //我不理他,后面的同学急着要上车,就把他推到一边去了,还MA他连开校车的师傅都不放过!int i,j,k,n;n = 0;for (i=2;i<N;i++)//我从序号为2的学生开始发票,{if(0 == arr[i])//如果arr[i]==0,即他还没拿到票,(这个解释可能有点问题...){lucky[n++] = i;//那么他是幸运滴,我给他一张真票,k = i+1;//但是从他后面的第一个人开始,就不一定了。j = 0;//这时心里开始计数,while(k<N)//从这个幸运的同学的后面第一位开始到最后一位,我开始发假_票!{if(arr[k]==0)//如果你还没拿到票,{j++;//那我心里的计数器就加个1,看看你跟上一位拿到票(不管是假的还是真的)的人距离是多少。}if(i==j)//如果你距离上一个拿到票(不管是假的还是真的)的同学arr[i]的距离是那个同学的序号i,我就笑着给你一张假_票,你高兴地在没拿到票的同学面前炫耀着...{arr[k] = 1;//你拿着假_票,一会我不让你上车的!!j = 0;//把计数器清零,下来就从这个拿票的同学开始数。}k++;//下一位~}}}while (scanf("%d",&n)!=EOF&&n!=0){printf("%d\n",lucky[n-1]);}return 0;}