数字排列

来源:互联网 发布:三菱系统手动编程实例 编辑:程序博客网 时间:2024/06/02 19:00

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;}