HDU 1271 整数对(思路题)

假设删除第k位,把整数A表示成如下形式:

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

则: B = a * 10^k + c;

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

显然:

11*a+b = N / (10^k)

2*c = N % (10^k)

但是c有可能产生进位,产生的影响为:

11*a+b+1 = N/(10^k)【b+1最多为10,不会影响到11*a的值】

2*c = N % (10^k) + 10^k;

把这两种情况分别考虑一下。

注意一下细节:

1.a和b不能同时为零

2.b的取值范围是0~9,如果b的值等于10,一定是产生进位的情况

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int MAXN = 10010;
int ans[MAXN];
int Ten[20];

void init()
{
    Ten[0] = 1;
    for ( int i = 1; i < 20; ++i )
        Ten[i] = Ten[i - 1] * 10;
    return;
}

int GetBit( int N )
{
    for ( int i = 0; i < 20; ++i )
        if ( Ten[i] > N ) return i;
    return -1;
}

int main()
{
    init();
    int N;
    while ( scanf( "%d", &N ) != EOF && N != 0 )
    {
        int cnt = 0;
        int limit = GetBit(N);
        for ( int k = 0; k < limit; ++k )
        {
            int a, b, c;
            int mi = Ten[k];
            int temp = N / mi;
            a = temp / 11;
            b = temp % 11;
            c = ( N % mi ) / 2;

            if ( ( a || b ) && b < 10 && a*mi*10 + b*mi + c + a*mi + c == N )
                ans[cnt++] = a*mi*10 + b*mi + c;

            --b;
            c = ( N % mi + mi ) / 2;
            //b>=0不小心写成了b>0
            if ( ( a || b ) && b >= 0 && a*mi*10 + b*mi + c + a*mi + c == N )
                ans[cnt++] = a*mi*10 + b*mi + c;
        }

        sort( ans, ans + cnt );
        cnt = unique( ans, ans + cnt ) - ans;

        if ( cnt == 0 ) puts("No solution.");
        else
        {
            for ( int i = 0; i < cnt; ++i )
            {
                if ( i ) putchar(' ');
                printf( "%d", ans[i] );
            }
            puts("");
        }
    }
    return 0;
}
    
原文地址:https://www.cnblogs.com/GBRgbr/p/3399967.html