UVA12290 Counting Game【数学模拟】

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值