UVA133 The Dole Queue
2014-02-03 23:55
393 查看
Background
In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen
as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1's left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting
m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until
no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.
Input
Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the
end of data will be signalled by three zeroes (0 0 0).
Output
For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official first. Separate successive
pairs (or singletons) by commas (but there should not be a trailing comma).
Sample Input
Sample output
4
8,
9
5,
3
1,
2
6,
10,
7
where
represents a space.
解题思路:
可以用数组模拟圆圈,然后逆时针,顺时针遍历,有点类似约瑟夫环,用flag[ ]来标志改元素是否已被遍历过,然后注意输出格式!
附上代码:
In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen
as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1's left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting
m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until
no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.
Input
Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the
end of data will be signalled by three zeroes (0 0 0).
Output
For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official first. Separate successive
pairs (or singletons) by commas (but there should not be a trailing comma).
Sample Input
10 4 3 0 0 0
Sample output
4
8,
9
5,
3
1,
2
6,
10,
7
where
represents a space.
解题思路:
可以用数组模拟圆圈,然后逆时针,顺时针遍历,有点类似约瑟夫环,用flag[ ]来标志改元素是否已被遍历过,然后注意输出格式!
附上代码:
#include<iostream> #include<iomanip> #include<cstdio> #include<cstring> #include<map> using namespace std; int flag[22]; int main() { int N,K,M,n; int i,count,first,second,temp=0; while(cin >> N >> K >> M) { temp=0;//temp记录被带走的人数 if(N==0&&K==0&&M==0)//结束输入的判断 break; for(i=1;i<=N;i++) flag[i]=1;//标志 n=N; first=1; second=N; while(n) { count=0; for(i=first;;i++)//i=first,并不是每一次遍历都是从1开始的,而是上一回合结束的地方开始 { if(flag[i]==1) count++; if(count==K) { flag[i]=0;//置为0表示,该位置的人已被带走 first=i; cout << setw(3) << first;//宽度为3 temp++; break; } if(i==N) i=0; } count=0; for(i=second;;i--) { if(flag[i]==1||i==first)//注意:有可能逆时针和顺时针带走的是同一个人 count++; if(count==M) { flag[i]=0; second=i; if(second!=first) { cout << setw(3) << second; temp++; } break; } if(i==1) i=N+1; } //对n进行减去带走的人数,当n=0时结束while if(first==second) n--; else n-=2; if(temp<N) cout << ","; else cout << endl; } } return 0; }
相关文章推荐
- UVa133 - The Dole Queue
- The Dole Queue UVA - 133
- UVa133 - The Dole Queue
- 救济金发放 (The Dole QUEUE UVa 133)
- 救济金发放The Dole QueueUVA 133
- UVa 133 - The Dole Queue
- uva133(The Dole Queue)—线性表
- uva133 The Dole Queue 循环队列模拟
- UVa133 The Dole Queue
- The Dole Queue(UVA 133)
- Uva 133 The Dole Queue 双向约瑟夫环
- UVA 133 The Dole Queue
- UVa-133 The Dole Queue
- uva 133 - The Dole Queue
- uva-133 The Dole Queue
- uva-133 - The Dole Queue
- UVa133 - The Dole Queue
- uva133救济金发放The Dole Queue
- uva - 133 The Dole Queue(成环状态下的循环走步方法)
- Uva - 133 - The Dole Queue