A. Divisible by Seven----打表暴力/數學思維

A. Divisible by Seven
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
1689
output
1869
input
18906
output
18690

題目鏈接:http://codeforces.com/contest/375/problem/A


題目的意思是說給你一個字符串,裏面一定包括1,6,8,9這四個數,讓你任意組合這個字符串,使它成爲7的倍數。

題目既然這樣說了,那麼肯定就和這四個數有關係,除以7,餘數只能是0~6,我們針對每一種情況,暴力出來一個答案,其餘的把0放在最後面,剩下的直接輸出。

代碼:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char s[1000010];
int a[10];
int main(){
    int len,k;
    scanf("%s",&s);
    len=strlen(s);
    k=0;
    for(int i=0;i<len;i++){
        if(s[i]=='0')
            a[0]++;
        else if(s[i]=='1'&&!a[1]){
            a[1]=1;
        }
        else if(s[i]=='6'&&!a[6]){
            a[6]=1;
        }
        else if(s[i]=='8'&&!a[8]){
            a[8]=1;
        }
        else if(s[i]=='9'&&!a[9]){
            a[9]=1;
        }
        else{
            printf("%c",s[i]);
            k=k*10+s[i]-'0';
            k%=7;
        }
    }
    if(k==0){
            printf("1869");
        }
        else if(k==1){
            printf("6198");
        }
        else if(k==2){
            printf("1896");
        }
        else if(k==3){
            printf("6918");
        }
        else if(k==4){
            printf("1986");
        }
        else if(k==5){
            printf("1968");
        }
        else if(k==6){
            printf("1698");
        }
        //cout<<"-----"<<endl;
        while(a[0]){
            printf("0");
            a[0]--;
        }
        cout<<endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章