废话不多说 源代码中有注释
/*
File : ABN.c
Date : 2015/4/4
version : 1.0
platform : windows 7 x86_64
Function :
A + B = N
1 <= N <= 10^9
B 是A去掉一个数字后得到
例如 A + B = 34
A可以是27或31
特例:
12 = 11 + 1 会执行两次 11 十位个位一样,去掉十位和个位一样。
故可以用一个数组保存结果最后输出。
*/
#include <stdio.h>
int main(int argc , char *argv[])
{
int arr[10] = {0 , };
int nSrc , nChg , nRes;
int j;
scanf("%d" , &nRes);
if(nRes <= 10)
{
printf("%d\n" , nRes);
return 0;
}
for(nSrc = 10; nSrc <= nRes; ++nSrc)
{
int nTmp = nSrc , nIndex;
/*
初始化数组
*/
for(j = 9; nTmp > 0 && j >= 0; --j)
{
arr[j] = nTmp % 10;
nTmp /= 10;
}
/*
保存索引
*/
nIndex = j + 1;
/*
对数组每一元素处理 8765
*/
for(j = 9 ; j >= nIndex; --j)
{
nChg = 0;
for(nTmp = nIndex; nTmp <= 9; ++nTmp)
{
if(nTmp != j)
{
nChg = nChg * 10 + arr[nTmp];
}
}
if(nChg + nSrc == nRes)
{
printf("%d + %d = %d\n" , nSrc , nChg , nRes);
}
}
/*
还原数组值供下一次循环使用
*/
for(j = 0; j < 10; ++j)
{
arr[j] = 0;
}
}
return 0;
}
运行结果
87654
78827 + 8827 = 87654
79677 + 7977 = 87654
79686 + 7968 = 87654
79687 + 7967 = 87654
79692 + 7962 = 87654
79727 + 7927 = 87654
79827 + 7827 = 87654
83827 + 3827 = 87654
34
27 + 7 = 34
31 + 3 = 34
32 + 2 = 34