Codeforces Round #306 (Div. 2) C,

來源:互聯網
上載者:User

Codeforces Round #306 (Div. 2) C,
題意

給一個不超過100的整數字串,從其中拿去任意個數(也可以不拿)。問有沒有可能剩下的可以被8整除。如果可以,輸出YES和任意一個剩下的數的情況。如果不可以,輸出NO。

思路

想法題。首先觀察到,1000可以整除8。也就是說我們可以不關心4位元的情況。只要有可能存在題目說的數,那麼它一定可以表示成3位元或更低的位元。那麼就好辦了,直接枚舉即可。
先看是否有0.
再看是否有8.
接著枚舉兩位元的情況,最後枚舉三位元的情況。

代碼
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int maxn = 110;char s[maxn];int ans;int len;bool chk2(int x)//檢查兩位元{    char c1 = '0'+x/10;    char c2 = '0'+x%10;    for(int i = 0 ; i < len-1 ; i ++) {        for(int j = i+1 ; j < len ; j ++) {            if(s[i] == c1 && s[j] == c2) {                ans = x;                return true;            }        }    }    return false;}bool chk3(int x)//檢查三位元{    char c1 = '0'+x/100;    char c2 = '0'+(x%100)/10;    char c3 = '0'+x%10;    for(int i = 0 ; i < len-2 ; i ++) {        for(int j = i+1 ; j < len-1 ; j ++) {            for(int t = j+1 ; t < len ; t ++) {                if(s[i] == c1 && s[j] == c2 && s[t] == c3) {                    ans = x;                    return true;                }            }        }    }    return false;}int main(){    scanf("%s",s);    len = strlen(s);    for(int i = 0 ; i < len ; i ++) {        if(s[i] == '0') {            printf("YES\n0\n");            return 0;        }    }    for(int i = 0 ; i < len ; i ++) {        if(s[i] == '8') {            printf("YES\n8\n");            return 0;        }    }    for(int i = 2 ; i <= 12 ; i ++) {        if(chk2(8*i)) {            printf("YES\n%d\n",ans);            return 0;        }    }    for(int i = 13 ; i <= 124 ; i ++) {        if(chk3(8*i)) {            printf("YES\n%d\n",ans);            return 0;        }    }    printf("NO\n");    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.