数字排列

Write an algorithm to find the number of six digit numbers where the sum of the first three digits is equal to the sum of the last three digits.

num3[i] 指三个位上的三个数相加为i的个数

num2[i] 指最高位为0, 剩下两位的两个数相加为i的个数


(num3[i]-num2[i])是指最高位不等于0的三位数的三个数字相加为i的个数 (可以看做是高三位)

void permutation() {
    int num3[28];
    int num2[28];

    memset(num3,0,sizeof(num3));
    memset(num2,0,sizeof(num2));

    for (int i = 0; i < 10; ++i) 
        for (int j = 0; j < 10; ++j) {
            for (int k = 0; k < 10; ++k)
                num3[i+j+k]++;//三个数字三个循环
            num2[i+j]++;//假设第一个数字为0
        }
        
    int ans = 0;
    for (int i = 0; i <= 27; ++i)
        ans += (num3[i] - num2[i]) * num3[i];

    cout <<ans<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值