solution.cpp 1.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <bits/stdc++.h>
using namespace std;
int bei[10]; //备用
map<long long, int> mp;
int main()
{
    int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int res = 0;
    do
    {
        for (int i = 1; i < 9; i++)
        {
            memset(bei, 0, sizeof(bei)); //分配空间
            long long int ans, left = 0, right = 0, t = 0, x = 0, y = 0;
            for (int j = 0; j <= i; j++)
            {
                left = left * 10 + a[j];
            }
            x = left;
            x = x * 10;
            for (int k = i + 1; k < 9; k++)
            {
                right = right * 10 + a[k];
                x = x * 10 + a[k];
            }
            y = right;
            y = y * 10;
            for (int j = 0; j <= i; j++)
                y = y * 10 + a[j];

            ans = left * right;
            long long int ff = ans;
            while (ans > 0)
            {
                int x = ans % 10;
                ans = ans / 10;
                if (bei[x] == 0 && x != 0)
                {
                    bei[x] = 1;
                    t++;
                }
            }
            if (t == 9 && mp.count(x) == 0 && mp.count(y) == 0)
            {
                res++;
                mp[x] = 1;
                mp[y] = 1;
            }
        }
    } while (next_permutation(a, a + 9)); //全排列
    cout << res << endl;
    return 0;
}