zoukankan      html  css  js  c++  java
  • Divisible by Seven CodeForces

    You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.

    Number a doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.

    Input

    The first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains digits: 1, 6, 8, 9. Number a doesn't contain any leading zeroes. The decimal representation of number a contains at least 4 and at most 106 characters.

    Output

    Print a number in the decimal notation without leading zeroes — the result of the permutation.

    If it is impossible to rearrange the digits of the number a in the required manner, print 0.

    Example
    Input
    1689
    
    Output
    1869
    
    Input
    18906
    
    Output
    18690
    //这道题的思路就是(1).如果在没有0的情况下把1689的任意组合放在后四位,而且1689的组合对1,6,8,9都只取一次
    //然后把剩下的数放在前面,这些数对7取余只有0~6这几种情况,所以把这些数对7取余之后的余数再与
    //1689的任意组合放在一起,看看哪种组合可以对7取余等于0.
    //(2).当输入的数中有0的话,计算一共有多少个0,然后再进行1中的过程,不过把0最后输出就行了
    #include<queue>
    #include<stack>
    #include<vector>
    #include<math.h>
    #include<stdio.h>
    #include<numeric>//STL数值算法头文件
    #include<stdlib.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<functional>//模板类头文件
    using namespace std;
    
    const int INF=1e9+7;
    const int maxn=1010000;
    
    char str[maxn],a[maxn];
    char xsk[10][5]= {"1869","1968","1689","6198","1698","1986","1896"};//对7取余,余数为0~6的组合情况
    int s1,s2,s3,s4;
    int main()
    {
        while(~scanf("%s%*c",str))
        {
            s1=s2=s3=s4=1;
            int t=0,sum1=0,sum2=0;
            int len=strlen(str);
            for(int i=0; i<len; i++)
                if(str[i]=='0')
                    sum1++;
            for(int i=0; i<len; i++)
            {
                if(str[i]=='0') continue;
                if(str[i]=='1'&&s1)
                {
                    s1--;
                    continue;
                }
                if(str[i]=='6'&&s2)
                {
                    s2--;
                    continue;
                }
                if(str[i]=='8'&&s3)
                {
                    s3--;
                    continue;
                }
                if(str[i]=='9'&&s4)
                {
                    s4--;
                    continue;
                }
                a[t++]=str[i];
                sum2=(sum2*10+str[i]-'0')%7;
            }
            printf("%s",a);
            if(sum2==0)printf("%s",xsk[0]);
            if(sum2==1)printf("%s",xsk[3]);
            if(sum2==2)printf("%s",xsk[6]);
            if(sum2==3)printf("%s",xsk[2]);
            if(sum2==4)printf("%s",xsk[5]);
            if(sum2==5)printf("%s",xsk[1]);
            if(sum2==6)printf("%s",xsk[4]);
            for(int i=0; i<sum1; i++)
                printf("0");
            printf("
    ");
        }
        return 0;
    }

  • 相关阅读:
    mysql相关
    java注解@Valid@Validated表单验证
    驼峰参数、下划线["_"]参数,属性互传
    redis,windows设置记录
    Java入门第一季学习总结
    计算属性calc()的运算规则
    swiper实现翻页,页面高度超出可滚动
    git常用命令
    Linux下svn常用命令
    图片高度不够一页,如何覆盖全屏
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264859.html
Copyright © 2011-2022 走看看