您的位置:首页 > 其它

A. Divisible by Seven----打表暴力/数学思维

2017-08-23 15:01 288 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: