Uva 12289 - Counting Game
你为何这么叼!
Counting Game
Time limit: 1.000 seconds
There are n people standing in a line, playing a famous game called ``counting". When the game begins, the leftmost person says ``1" loudly, then the second person (people are numbered 1 to n from left to right) says ``2" loudly. This is followed by the 3rd person saying ``3" and the 4th person say ``4", and so on. When the n-th person (i.e. the rightmost person) said ``n" loudly, the next turn goes to his immediate left person (i.e. the (n - 1)-th person), who should say ``n + 1" loudly, then the (n - 2)-th person should say ``n + 2" loudly. After the leftmost person spoke again, the counting goes right again.
There is a catch, though (otherwise, the game would be very boring!): if a person should say a number who is a multiple of 7, or its decimal representation contains the digit 7, he should clap instead! The following tables shows us the counting process for n = 4 (`X' represents a clap). When the 3rd person claps for the 4th time, he's actually counting 35.
| Person | 1 | 2 | 3 | 4 | 3 | 2 | 1 | 2 | 3 |
| Action | 1 | 2 | 3 | 4 | 5 | 6 | X | 8 | 9 |
| Person | 4 | 3 | 2 | 1 | 2 | 3 | 4 | 3 | 2 |
| Action | 10 | 11 | 12 | 13 | X | 15 | 16 | X | 18 |
| Person | 1 | 2 | 3 | 4 | 3 | 2 | 1 | 2 | 3 |
| Action | 19 | 20 | X | 22 | 23 | 24 | 25 | 26 | X |
| Person | 4 | 3 | 2 | 1 | 2 | 3 | 4 | 3 | 2 |
| Action | X | 29 | 30 | 31 | 32 | 33 | 34 | X | 36 |
Given n, m and k, your task is to find out, when the m-th person claps for the k-th time, what is the actual number being counted.
Input
There will be at most 10 test cases in the input. Each test case contains three integers n, m and k ( 2
n
100, 1
m
n, 1
k
100) in a single line. The last test case is followed by a line with n = m = k = 0, which should not be processed.
Output
For each line, print the actual number being counted, when the m-th person claps for the k-th time. If this can never happen, print `-1'.
Sample Input
4 3 1 4 3 2 4 3 3 4 3 4 0 0 0
Sample Output
17 21 27 35
The Seventh Hunan Collegiate Programming Contest
Problemsetter: Rujia Liu, Special Thanks: Yiming Li & Jane Alam Jan
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int n,m,k; 6 int ok(int x){ 7 if(x%7==0)return 1; 8 while(x){ 9 if((x%10)==7)return 1; 10 x/=10; 11 } 12 return 0; 13 } 14 int main(){ 15 while(scanf("%d%d%d",&n,&m,&k)&&(n+m+k)){ 16 int k1=0,i=0,j; 17 for(j=m;i<k;k1++){ 18 if(ok(j)){i++;if(i>=k)break;} 19 if(((k1&1)||n==m)&&m!=1)j=j+2*m-2; 20 else j=j+2*n-2*m; 21 } 22 printf("%d\n",j); 23 } 24 return 0; 25 }
浙公网安备 33010602011771号