HDU 1271 整数对

题解:考虑整数的分解情况:

A = a + b * 10^k + c * 10^(k+1)

B = a + c * 10^k

N = A + B = 2 * a + b * 10^k + c * 10^k * 11

那么,只要求出a,b,c,我们就可以得到答案了,所以枚举k,就可以检验得到答案了。

#include <cstdio>
#include <algorithm>
using namespace std;
int cmp(int a,int b)
{
    return a<b;
}
int main()
{
    int n,a,b,c,count,k,s[100],i;
    while(scanf("%d",&n)!=EOF && n)
    {
        count=0;
        for(k=1;k<=n;k*=10)
        {
            c=(n/k)/11;
            b=n/k-c*11;
 
            if((b!=0 || c!=0) && b<10)
            {
                    a=(n-b*k-c*11*k)/2;
                    if(2*a+b*k+c*11*k==n)
                    {
                    count++;
                    s[count]=a+b*k+c*10*k;
                    }
            }
            b--;
            if((b!=0 || c!=0) && b>=0)
            {
                a=(n-b*k-c*11*k)/2;
                if(2*a+b*k+c*11*k==n)
                {
                    count++;
                    s[count]=a+b*k+c*10*k;
                }
            }
        }
        if(count==0) printf("No solution.
");
        else
        {
            sort(s+1,s+count+1,cmp);
            printf("%d",s[1]);
            for(i=2;i<=count;i++)
            {
                if(s[i]!=s[i-1])
                printf(" %d",s[i]);
            }
            printf("
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/forever97/p/3496212.html