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.
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.
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.
1689
1869
18906
18690
#include <iostream>
#include <iomanip>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<algorithm>
#include<vector>
#define inf 1000000007
#define LL long long int
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
char a[1000005];
int vis[10];
int main()
{
mem(vis,0);
int mod=0,sum=0;
scanf("%s",a);
int n=strlen(a);
for(int i=0;i<n;i++)
{
//判断第一次的1,6,8,9,第二次遇到1,6,8,9直接输出,然后判断余数
if(a[i]=='1'&&!vis[1])
{
vis[1]=1;
continue;
}
else if(a[i]=='6'&&!vis[6])
{
vis[6]=1;
continue;
}
else if(a[i]=='8'&&!vis[8])
{
vis[8]=1;
continue;
}
else if(a[i]=='9'&&!vis[9])
{
vis[9]=1;
continue;
}
else if(a[i]=='0')
sum++;
else
{
mod=(mod*10+(a[i]-'0'))%7;
printf("%c",a[i]);
}
}
//由于1,6,8,9占了四位,所以要乘10000
mod=(mod*10000)%7;
//计算1689的各种组合,从0~6余数,输出
if(mod==0)
printf("1869");
else if(mod+1==7)
printf("1968");
else if(mod+2==7)
printf("1689");
else if(mod+3==7)
printf("6198");
else if(mod+4==7)
printf("1698");
else if(mod+5==7)
printf("1986");
else if(mod+6==7)
printf("1896");
for(int i=0;i<sum;i++)
printf("0");
}