URAL1095

题意给你N个正整数,它们都含有数码1、2、3、4,不超过20位,要求你调整每个正整数中数字的顺序,使它能被7整除。
暴力:妥妥tle
然后就是,可以构造这个东西,你就安排前面几位,然后最后mol%7,有7个数,0 ,1 ,2, 3, 4, 5 ,6, 然后用1,2,3,4构造出来7个数,来填补他们就行了,别忘了用(x % n + i)%n = (10*x + i)%n然后就行了
我是用的4123 , 1324 , 4321 , 2341 , 1432 , 2413 , 4213 ,4123。不要忘了最后排一下0,然后就是一定要打到7,因为,可能最后now是0,然后7-0,我一开始就没考虑这种情况。。。。QAQ

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int cou[10];
int rest[8] = {4123 , 1324 , 4321 , 2341 , 1432 , 2413 , 4213 ,4123};
char str[22];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        memset(cou , 0 , sizeof(cou));
        scanf("%s",str);
        for(int i = 0 ; i < strlen(str) ; i++){
            cou[str[i] - '0']++;
        }
        for(int i = 1 ; i <= 4 ; i++)
            cou[i]--;
        long long now = 0;
        for(int i = 9 ; i > 0 ; i--){
            for(int j = 0 ; j < cou[i] ; j++){
                printf("%d",i);
                now = (now * 10 + i) % 7;
            }
        }
        printf("%d",rest[7 - (now * 10000) % 7]);
        for(int i = 1 ; i <= cou[0] ; i++) printf("0");
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值