轻松周赛赛题:能否被8整除


  • 发布公司:
  • 有 效 期:
  • CSDN
  • 2014-11-072015-11-04
  • 难 度 等 级:
  • 答 题 时 长:
  • 编程语言要求:
  • 120分钟
  • C C++ Java C#
题目详情

给定一个非负整数,问能否重排它的全部数字,使得重排后的数能被8整除。

输入格式:

多组数据,每组数据是一个非负整数。非负整数的位数不超过10000位。

输出格式

每组数据输出一行,YES或者NO,表示能否重排它的全部数字得到能被8整除的数。注意: 重排可以让0开头。


代码附上:


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define Max 10005
#define Inf 1<<30
int Ncon[10];
int com[10];
char num[Max];
bool low(int *a,int *b){
    for(int i=0;i<10;i++){
        if(a[i]<b[i]) return false;
    }
    return true;
}
int main(){
    int i,len,shu;
    bool able;
    while((scanf("%s",num))!=EOF){
        able = false;
        len = strlen(num);
        memset(Ncon,0,sizeof(Ncon));
        for(i=0;i<len;i++){
            Ncon[num[i]-'0']++;
        }
        if(len<2){if(Ncon[0]||Ncon[8]) able = true;}
        else if(len<3){
            for(i=0;i<100;i++){
               if(i%8) continue;
               memset(com,0,sizeof(com));
               shu = i;
               com[shu/10]++;
               shu = shu %10;
               com[shu]++;
               if(low(Ncon,com)) {able = true;break;}
            }
        }
        else{
            for(i=0;i<1000;i++){
               if(i%8) continue;
               memset(com,0,sizeof(com));
               shu = i;
               com[shu/100]++;
               shu = shu %100;
               com[shu/10]++;
               shu = shu %10;
               com[shu]++;
               if(low(Ncon,com)) {able = true;break;}
            }
        }
        printf("%s\n",able?"YES":"NO");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值