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
问题链接:UVA12290 Counting Game
问题简述:(略)
问题分析:
数学模拟题,不解释。使用函数判定一个数是否为7的倍数或包含数字7,也可以考虑用打表(如果数据规模不大的话)来实现。两个方向迭代模拟同时进行。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA12290 Counting Game */
#include <bits/stdc++.h>
using namespace std;
bool is7(int n)
{
if(n % 7 == 0) return true;
else {
while(n) {
if(n % 10 == 7) return true;
n /= 10;
}
return false;
}
}
int main()
{
int n, m, k;
while(~scanf("%d%d%d", &n, &m, &k) && (n || m || k)) {
int sum = m, cnt = 0;
for(;;) {
if(is7(sum) && m != 1) cnt++;
if(cnt == k) break;
sum += 2 * (n - m);
if(is7(sum) && n != m) cnt++;
if(cnt == k) break;
sum += 2 * (m - 1);
}
printf("%d\n", sum);
}
return 0;
}