您的位置:首页 > 其它

PE 145 How many reversible numbers are there below one-billion? (暴力)

2016-12-27 19:51 375 查看


How many reversible numbers are there below one-billion?


Problem 145

Some positive integers n have the property that the sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. For instance, 36 + 63 = 99 and 409 + 904 = 1313. We will call such numbers reversible;
so 36, 63, 409, and 904 are reversible. Leading zeroes are not allowed in either n or reverse(n).

There are 120 reversible numbers below one-thousand.

How many reversible numbers are there below one-billion (109)?

题意:

有多少小于十亿的可逆数?

有些正整数n满足这样一种性质,将它的数字逆序排列后和本身相加所得到的和[ n + reverse(n) ]的十进制表示只包含有奇数数字。例如,36 + 63 = 99 以及409 + 904 = 1313。我们称这样的数是可逆的;因此36,63,409和904都是可逆的。无论是n还是reverse(n)都不允许出现前导0。

在小于一千的数中,一共有120个可逆数。

在小于十亿(109)的数中,一共有多少个可逆数?

题解:感觉没什么好说的....直接暴力....

其实一亿到10亿之间是没有可逆数的。然后暴力到一亿就可以了。

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int reverse(ll x)
{
ll r=0;
while (x>0)
{
r=r*10+(x%10);
x/=10;
}
return r;
}

int check_odd(ll x)
{
int flag=0;
while (x>0)
{
if ((x%10)%2==0){
flag=1;
break;
}
x/=10;
}
if (flag) return 0;
else return 1;
}

int main()
{
ll count=0;
//在 1 亿到 10亿之间没有可逆数
for (ll i=10;i<=100000000;i+=2)
{
if (check_odd(i+reverse(i)) && i%10!=0)
count++;
}
cout<<count*2<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: