2015年6月30日 星期二

Codeforces Round #306 (Div. 2), problem: (C) Divisibility by Eight

因為最多只需要三個,所以直接搜。

比較好奇假如是'k'倍數的話要如何處理。

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

typedef long long int int64;

int gg(char a){return a-'0';}

int main(){
    char buf[1000]; scanf("%s", buf);
    int n = strlen(buf);
    for(int lx = n-1;lx >=0;lx--)
        buf[lx+2] = buf[lx];
    buf[1] = '0', buf[0] = '0';
    n += 2;
    for(int lx = 0;lx < n;lx++){
        for(int ly = lx+1;ly < n;ly++){
            for(int lz = ly+1;lz < n;lz++){
                int test = gg(buf[lx])*100 + gg(buf[ly])*10 + gg(buf[lz]);
                if(test%8 == 0){
                    printf("YES\n%d\n", test);
                    return 0;
                }
            }
        }
    }
    puts("NO");
    return 0;
}

沒有留言:

張貼留言