cf C. Divisible by Seven

http://codeforces.com/contest/376/problem/C

题意:给你一个大数最多含有10^6个数字,这里面必须含有1,6,8,9,然后重新排列找出一个能被6整除的数。

思路:1,6,8,9排列组成的四位数取余7为0,1,2,3,4,5,6;

所以可以这样排列前面的数是其它的数+1,6,8,9排列组成的数+多个0;

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 1000010
 5 using namespace std;
 6 
 7 char str[maxn];
 8 int cnt[10];
 9 int num[] = {1869,6198,1896,1689,1986,1968,1698};
10 
11 
12 int main()
13 {
14     for(int i=0; i<7; i++)
15     {
16         printf("%d
",num[i]%7);
17     }
18     while(scanf("%s",str)!=EOF)
19     {
20         memset(cnt,0,sizeof(cnt));
21         int k=strlen(str);
22         for(int i=0; i<k; i++)
23         {
24             cnt[str[i]-'0']++;
25         }
26         cnt[1]--;
27         cnt[6]--;
28         cnt[8]--;
29         cnt[9]--;
30         int c=0;
31         for(int i=1; i<=9; i++)
32         {
33             for(int j=1; j<=cnt[i]; j++)
34             {
35                 printf("%d",i);
36                 c=(c*10+i)%7;
37             }
38         }
39         printf("%d",num[c]);
40         for(int i=1; i<=cnt[0]; i++)
41         {
42             printf("0");
43         }
44         printf("
");
45     }
46     return 0;
47 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4133600.html