您的位置:首页 > 其它

CodeForces 375A Divisible by Seven

2015-12-14 16:39 351 查看
A. Divisible by Seven

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.

Sample test(s)

input

1689

output

1869

input

18906

output

18690

题目传送门

题意:给一个很大的数字,对这个数字进行重新排列,使其能整除7,而且这个书里面必定包含1 6 8 9.

这道题就是找规律,可以把这个数字分成三部分,0放在最后,1689这个数字的组合放在中间,其余数字放在最前面。

0放在最后对整个运算不影响,所以不管。

现在可以说是相当于 (其余数字)和(1689组合数字) 对7的运算,就把这个数字分成万位及以上和万位以下。万位以上数字单独处理,用来对7取余,然后留下一个余数,这个余数可能是0 1 2 3 4 5 6。相当于是找出 (10000+(1689组合))%7 == 0,(20000+(1689组合))%7 == 0…………

((10000+(1689组合))%7 == 0

->(10000%7 + (1689组合)%7 )%7 == 0

然后找出 10000%7 + (1689组合)%7 == 7 就行了

然后就随便算一下就有了

我理了几个

1689%7 ==2

1698%7 == 4

1869%7 == 0

1968%7 == 1

1896%7 == 6

1986%7 == 0

6189%7 == 1

6198%7 == 3

8169%7 == 0

9168%7 == 5

8196%7 == 6

9186%7 ==2

CODE

#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"string.h"
#include"math.h"
#include"stdlib.h"
const int maxn = 1e6+10;
using namespace std;

char s[maxn];
int main(void)
{
while(scanf("%s",s) !=EOF)
{
int num = 0;
int len = strlen(s);
int flag[10] = {0};
for(int i = 0;i < len;i++)
{
if(s[i] == '0') flag[0]++;
else if(s[i] == '1' && !flag[1]) flag[1]++;  ///1689这个用于组合的数字每一位都只需要一个,其他的直接输出就是
else if(s[i] == '6' && !flag[6]) flag[6]++;
else if(s[i] == '8' && !flag[8]) flag[8]++;
else if(s[i] == '9' && !flag[9]) flag[9]++;
else
{
printf("%c",s[i]);
num = num*10+s[i]-'0';
num %= 7;
}
}
int t;
if(num == 0) t = 1869;       ///1869%7 == 0
else if(num == 1) t = 6198;  ///10003%7 == 0
else if(num == 2) t = 1896;  ///20006%7 == 0
else if(num == 3) t = 1689;  ///30002%7 == 0
else if(num == 4) t = 1986;  ///40005%7 == 0
else if(num == 5) t = 1968;  ///50001%7 == 0
else if(num == 6) t = 1698;  ///60004%7 == 0
printf("%d",t);
while(flag[0]--)
{
printf("0");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: