Codeforces 488A Giga Tower

Giga Tower

题目链接:

http://codeforces.com/problemset/problem/488/A

解题思路:

Codeforces官方题解:

The answer b is very small (usually no larger than 10), because one of a + 1, a + 2, ..., a + 10 has its last digit be 8.

However, b can exceed 10 when a is negative and close to 0. The worst case is a =  - 8, where b = 16.

Anyway b is rather small, so we can simply try b from 1, and check whether a + b has a digit 8.

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int ans = 1,t,x,flag = 0;
        while(1){
            n++;
            t = n;
            while(t){
                x = abs(t)%10;
                if(x == 8){
                    flag = 1;
                    break;
                }
                t/=10;
            }
            if(flag){
                printf("%d\n",ans);
                break;
            }
            else
                ans++;
        }
    }
    return 0;
}




你可能感兴趣的:(codeforces)