隐藏页面特效

POJ 1117 Pairs of Integers

Pairs of Integers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4133   Accepted: 1062

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

Source

 

【题意】

给出一个数N,求X+Y = N的所有数对(X,Y),X,Y有如下要求,Y是X这个数删除一位所得到的数,X不能含有前导0,但是Y可以含有前导0.

 

【分析】

将数分三段,可以把X看成三部分:HSL,高位H,低位L,中间被strike掉的位S

所以Y 就是HL

X = (H*10 + S)*10^i + L, (i = 0, 1, 2 ... 最多log10(N),i代表L是几位数)

Y = H*10^i + L

X + Y = (H*11 + S) * 10^i + 2*L = N

N/(10^i) = (11H+S) + 2L/(10^i),其中2L/(10^i)只可能为0和1,再加上i的不到10种取值,共20种不到的组合
 

所以我们通过枚举L的值,来推导出H和S。


当N是奇数的时候,只可能是删除X的最后一位得到,(原因是如果L存在,则2*L%(10^i)取余

是N的后i位,因为2*L是偶数,所以N必然四偶数)。此时变成H*11 + S = N

由于S是一个数字,其值只能是0~9,故当N%11 != 10的时候是有解的。



1.N是奇数,N%11 != 10,有一个解。

2.N是偶数,还是需要考虑删除的是最后一位的情况,该情形和奇数的是一样的。

3.当枚举L的时候,又分为两种情况,2*L有进位,和2*L无进位,

即(2*L)%(10^i) = N%(10^i)

举个例子吧:

假设L是一位数,发现N的末尾是2,

则我们可以猜测的是,L = 1, 2*L = 2, 2*L无进位

然而L = 6,也是满足条件的,2*L = 12, 2*L%(10^i) = 2,即2*L向前进了1,其余数为2.

 

最后这样求解还可能存在重复的结果,所以我们map去一下重

 

【代码】

#include<map> #include<cstdio> #include<iomanip> #include<iostream> using namespace std; int n,H,S,L,X;map<int,int>res,ri; int main(){ cin>>n; for(int i=0,I=1;I<=n;i++,I*=10){ if(n%I%2) continue; H=n/I/11; S=n/I%11; L=n%I/2; if(S<=9){ X=(H*10+S)*I+L; if(H+S) res[X]=H*I+L; ri[X]=i; } L=(n%I+I)/2; S=n/I%11-1; if(S>=0&&L){ X=(H*10+S)*I+L; if(H+S) res[X]=H*I+L; ri[X]=i; } } cout<<res.size()<<endl; for(map<int,int>::iterator it=res.begin();it!=res.end();it++) cout<<it->first<<" + "<<setw(ri[it->first])<<setfill('0')<<it->second<<" = "<<n<<endl; return 0; }
 
 

 

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/10371901.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(223)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2017-02-13 股神小L 2016Vijos省选集训 day1
2017-02-13 兔子的晚会 2016Vijos省选集训 day1
2017-02-13 1194: [HNOI2006]潘多拉的盒子
2017-02-13 Circling Round Treasures CodeForces - 375C
2017-02-13 POJ3182 The Grove[射线法+分层图最短路]
2017-02-13 大白第一章第四节dp例题
点击右上角即可分享
微信分享提示