UVa 12290 Counting Game

来源:互联网 发布:服务器防火墙端口设置 编辑:程序博客网 时间:2024/05/16 06:45

~~~题目链接~~~


题目大意:有编号为1~n的n个人, 现在他们从左到右在从右到左依次的数数, 当前这个人如果数的数能被7整除或包含数字7他就用拍手来代替, 下一个人接着下一个数数。


思路:第i个人开始时数的数为i, 他数的下两个数必然是, i+2*(n-i)和i+2*(n-i)+2*(i-1)。边界注意判断一下。


code:

#include <stdio.h>#include <string.h>using namespace std;int n = 0, m = 0, k = 0, sum = 0;int judge(int x){    if(x%7 == 0 ) return 1;    while(x)    {        if(x%10 == 7) return 1;        x /= 10;    }    return 0;}int f(){    int sum = m, cnt = 0;    while(1)    {        if(m-1 != 0 && judge(sum)) cnt++;        if(cnt == k) return sum;        sum += 2*(n-m);        if(n-m != 0 && judge(sum)) cnt++;        if(cnt == k) return sum;        sum += 2*(m-1);    }    return 0;}int main(){    while(scanf("%d %d %d", &n, &m, &k) , n+m+k != 0)    {        printf("%d\n", f());    }    return 0;}


原创粉丝点击