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

一时失言乱红尘 2022-07-13 05:53 183阅读 0赞

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亿之间是没有可逆数的。然后暴力到一亿就可以了。

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int reverse(ll x)
  5. {
  6. ll r=0;
  7. while (x>0)
  8. {
  9. r=r*10+(x%10);
  10. x/=10;
  11. }
  12. return r;
  13. }
  14. int check_odd(ll x)
  15. {
  16. int flag=0;
  17. while (x>0)
  18. {
  19. if ((x%10)%2==0){
  20. flag=1;
  21. break;
  22. }
  23. x/=10;
  24. }
  25. if (flag) return 0;
  26. else return 1;
  27. }
  28. int main()
  29. {
  30. ll count=0;
  31. //在 1 亿到 10亿之间没有可逆数
  32. for (ll i=10;i<=100000000;i+=2)
  33. {
  34. if (check_odd(i+reverse(i)) && i%10!=0)
  35. count++;
  36. }
  37. cout<<count*2<<endl;
  38. return 0;
  39. }

发表评论

表情:
评论列表 (有 0 条评论,183人围观)

还没有评论,来说两句吧...

相关阅读