把A分解成3个部分 a, b, c, 去掉第 t+1 位的数字,b为被去掉的数字。
A = 10*a*10^t + b*10^t + c
B = a*10^t + c
N = A + B = 11*a*10^t + b*10^t + 2*c
N 除以 10^t 求出 11*a+b,然后把得到的结果除以 11 商为 a 余数为 b,因为 2*c 可能大于 10 导致进位,所以此时 b 的值可能是 b+1,a 的值是唯一的,求出 c = (N - 11*a*10^t - b*10^t) / 2,把 a, b, c 带入判断是否与N相等,把得到的结果排序输出。
#include <iostream>
#include <algorithm>
using namespace std;
int ans[100000];
int main()
{
int N;
while (cin >> N)
{
if (N == 0)
break;
int count = 0; //符合要求的解的数量
int a, b, c;
for (int t = 1; t <= N; t *= 10)
{
a = (N / t) / 11;
b = (N / t) % 11;
if ((a + b) != 0 && b < 10)
{
c = (N - 11 * a * t - b * t) / 2;
if (11 * a * t + b * t + 2 * c == N)
{
ans[count++] = 10 * a * t + b * t + c;
}
}
--b;
if ((a + b) != 0 && b >= 0)
{
c = (N - 11 * a * t - b * t) / 2;
if (11 * a * t + b * t + 2 * c == N)
{
ans[count++] = a * 10 * t + b * t + c;
}
}
}
sort(ans, ans + count);
if (count == 0)
cout << "No solution." << endl;
else
{
cout << ans[0];
for (int i = 1; i < count; i++)
if (ans[i] != ans[i - 1])
cout << " " << ans[i];
cout << endl;
}
}
return 0;
}