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;
    }

  • 相关阅读:
    Erlang学习记录:转义
    Erlang学习记录:运算符
    Erlang学习记录:语法和特性
    Erlang学习记录:相关工具和文档
    IDEA快捷键(收集自网络后整理)
    Redis学习
    SQL_server_2008_r2和visual studio 2010旗舰版的安装(2013-01-16-bd 写的日志迁移
    oracle 11gR2 for win7旗舰版64安装以及连接plsql和NaviCat(win64_11gR2_database) (2012-12-31-bd 写的日志迁移
    win7在某个盘或文件夹中出现右键只能新建文件夹的情况 (2012-12-28-bd 写的日志迁移
    宏基笔记本升级bios(2012-12-28-bd 写的日志迁移
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264859.html
Copyright © 2011-2022 走看看