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\n",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("\n");
45     }
46     return 0;
47 }
View Code

 

转载于:https://www.cnblogs.com/fanminghui/p/4133600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值