您的位置:首页 > 其它

sum is equal to the given integer number N and the second number

2011-11-08 19:20 375 查看
Description

You are to find all pairs of integers such that their sum is equal to the given integer number N and the second number
results from the first one by striking out one of its digits. The first integer always has at least two digits and starts
with a non-zero digit. The second integer always has one digit less than the first integer and may start with a zero digit.

Input

The input file consists of a single integer N (10 <= N <= 10^9).

Output

On the first line of the output file write the total number of different pairs of integers that satisfy the problem statement.
On the following lines write all those pairs. Write one pair on a line in ascending order of the first integer in the pair.
Each pair must be written in the following format:

X + Y = N

Here X, Y, and N, must be replaced with the corresponding integer numbers. There should be exactly one space on both sides of '+' and '='
characters.

Sample Input

302
Sample Output

5
251 + 51 = 302
275 + 27 = 302
276 + 26 = 302
281 + 21 = 302
301 + 01 = 302

代码:

View Code

#include <iostream>
#include <math.h>

using namespace std;

void find(long int N)
{
int a[10];
int b[10];

for(int x=10;x<N;x++)
{
//        cout<<x<<"\t";
long int total=0;
a[0]=x/pow(10,9);             //数x存在数组a中
for(int i=1;i<10;i++)
{
total+=a[i-1]*pow(10,10-i);
a[i]=(x-total)/pow(10,(9-i));
}

for(i=0;i<10&&a[i]==0;i++);   //数x实际从第sign位开始
int sign=i;
int len_x=10-sign;            //len_x是数x实际再数组中存储的位数

for(int k=sign;k<10;k++)
{
long int y=0;
for(int j=0,i=sign;j<len_x-1;j++,i++)  //将数y存在数组b中
{
if(k-sign==j)
i++;
b[j]=a[i];
y+=b[j]*pow(10,len_x-2-j);
}
//        cout<<y<<"\t";
if(x+y==N)
{
cout<<x<<"+";
for(j=0;j<len_x-1;j++)
cout<<b[j];
cout<<"="<<N<<endl;
break;
}
}
//        cout<<endl;
}
}

int main()
{
long int N;
cout<<"input sum N(10<N<10^9):";
cin>>N;
while(N<10||N>pow(10,9))
{
cout<<"error!input again!"<<endl;
cin>>N;
}

find(N);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐