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

http://codeforces.com/contest/550/problem/C

  给定一个数字(最多100位),可以任意去掉其中的数(每一位数的相对关系不变), 使得剩下的数能够被8整除.最后至少要有一位数.不能满足的话就没有存在的答案.

  根据能被8整除的数的特征:只要最低的三位能被整除,这个数便能被整除.

abcdef=abc1000+def

8|1000, 只要8|def,那么就有 8|abcdef.

这题是SPJ的,只要找出一个满足的答案就可以了.那么其实只要来一个 O(n3) 的方法就可以过了.就是枚举最后三位,再注意一下前导零.

#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <iostream>
#define LL long long
#define pb push_back
#define lb lower_bound
#define eps 1e-8
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1005;
char s[N];
int best[3];
void input()
{
    scanf("%s", s+3);
    s[0] = s[1] = s[2] = '0';
    int len = strlen(s);
    int i, j, k, cnt = 0;
    bool flag = 0;
    for(i=len-1; i>=3; i--){
        for(j=i-1; j>=0; j--){
            for(k=j-1; k>=0; k--){
                int num = s[i]-'0' + (s[j]-'0')*10 + (s[k]-'0')*100;
                if(num % 8 == 0){
                    best[0] = k;
                    best[1] = j;
                    best[2] = i;
                    flag = 1;
                    break;
                }
            }
            if(flag) break;
        }
        if(flag) break;
    }
    if(!flag){
        puts("NO");
        return ;
    }
    puts("YES");
    bool isz = 0;
    for(int i=0; i<3; i++){
        if(!isz && s[best[i]] == '0' && i<2) continue;
        isz = 1;
        putchar(s[best[i]]);
    }
}
int main(void)
{
    input();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值