整数之循环节点求和

整数之循环节点求和_第1张图片

#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
#define MAX 10000
void sort(int * A,int l)
{
    for (int i = 0; i < l; i++)
    {
        for (int j = i+1; j < l; j++)
        {
            if (A[i]>A[j])
            {
                int t = A[i];
                A[i] = A[j];
                A[j] = t;
            }
        }
    }
}
long long big(int *A,int l)
{
    long long res = 0;
    for (int i = l - 1; i >= 0; i--)
    {
        res += A[i] * pow(10, i);
    }
    return res;
}
long long small(int *A, int l)
{
    long long res = 0;
    for (int i = 0; i 1] * pow(10, i);
    }
    return res;
}
int main()
{
    int t1 = clock();
    long long n;
    long long sub[MAX];
    //int mark[MAX];
    //memset(mark, 0, MAX*sizeof(int));
    long cnt = 0;
    long long sum = 0;
    cin >> n;
    int part[MAX];
    int i;//i位数
    for (i = 0; n > 0; i++)
    {
        part[i] = n % 10;
        n = n / 10;
    }
    sort(part,i);
    long long mmax = big(part, i);
    long long mmin = small(part, i);
    if (mmax == mmin)
    {
        cout << 0 << endl;
        int t2 = clock();
        cout << t2 - t1;
        return 0;
    }
    sub[cnt++] = mmax - mmin;
    int flag = 0;
    while (flag == 0)
    {
        int k;//i位数
        long long t = sub[cnt-1];

        for (k = 0; t > 0; k++)
        {
            part[k] = t % 10;
            t = t / 10;
        }
        sort(part, k);
        if (k < i)
        {
            int l = i - k;
            while (l>0)
            {
                part[k++] = 0;
                l--;
            }
        }
        sort(part, k);
        long long mmax = big(part, k);
        long long mmin = small(part, k);
        long long ssub = mmax - mmin;
        for (int m = 0; m < cnt; m++)
        {
            if (sub[m] == ssub)
            {
                flag = 1;
                for (int p = m; p < cnt; p++)
                {
                    sum += sub[p];
                    cout << sub[p] << endl;
                }

            }

        }       
        sub[cnt++] = mmax - mmin;

    }

    if (flag == 1) cout << sum << endl;
    int t2 = clock();
    cout << t2 - t1;
    return 0;
}

你可能感兴趣的:(华为OJ测试题)