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

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
 1 #include <iostream>
2 #include <math.h>
3
4 using namespace std;
5
6 void find(long int N)
7 {
8 int a[10];
9 int b[10];
10
11 for(int x=10;x<N;x++)
12 {
13 // cout<<x<<"\t";
14 long int total=0;
15 a[0]=x/pow(10,9); //数x存在数组a中
16 for(int i=1;i<10;i++)
17 {
18 total+=a[i-1]*pow(10,10-i);
19 a[i]=(x-total)/pow(10,(9-i));
20 }
21
22 for(i=0;i<10&&a[i]==0;i++); //数x实际从第sign位开始
23 int sign=i;
24 int len_x=10-sign; //len_x是数x实际再数组中存储的位数
25
26 for(int k=sign;k<10;k++)
27 {
28 long int y=0;
29 for(int j=0,i=sign;j<len_x-1;j++,i++) //将数y存在数组b中
30 {
31 if(k-sign==j)
32 i++;
33 b[j]=a[i];
34 y+=b[j]*pow(10,len_x-2-j);
35 }
36 // cout<<y<<"\t";
37 if(x+y==N)
38 {
39 cout<<x<<"+";
40 for(j=0;j<len_x-1;j++)
41 cout<<b[j];
42 cout<<"="<<N<<endl;
43 break;
44 }
45 }
46 // cout<<endl;
47 }
48 }
49
50 int main()
51 {
52 long int N;
53 cout<<"input sum N(10<N<10^9):";
54 cin>>N;
55 while(N<10||N>pow(10,9))
56 {
57 cout<<"error!input again!"<<endl;
58 cin>>N;
59 }
60
61 find(N);
62
63 return 0;
64 }

 

转载于:https://www.cnblogs.com/2011-9-13/archive/2011/11/08/2241583.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值