鱼C论坛

 找回密码
 立即注册
查看: 4009|回复: 12

题目55:10000以下有多少Lychrel数?

[复制链接]
发表于 2019-7-21 16:12:59 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-4-18 12:25 编辑

Python:
  1. def isLychrel(n):
  2.     count=50
  3.     n+=int(str(n)[::-1])

  4.     while count:
  5.         reversed=int(str(n)[::-1])

  6.         if n==reversed:
  7.             return False

  8.         n+=reversed
  9.         count-=1
  10.         
  11.     return True

  12. print(sum(map(isLychrel,range(1,10000))))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-18 12:00:12 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-6-27 09:37 编辑

C++ 11ms
  1. //249 10ms
  2. #include<iostream>
  3. using namespace std;
  4. using ull=unsigned long long;


  5. ull numreverse(ull n) {
  6.     ull res = 0;

  7.     while (n) {
  8.         res = res * 10 + n % 10;
  9.         n /= 10;
  10.     }

  11.     return res;
  12. }


  13. int main() {
  14.     unsigned short res = 0;
  15.     unsigned char count;
  16.     ull i = 9999, j, reversed;

  17.     while (i) {
  18.         j = i + numreverse(i);
  19.         count = 50;

  20.         while (count--) {
  21.             reversed = numreverse(j);

  22.             if (reversed == j) {
  23.                 goto next;
  24.             }

  25.             j += reversed;
  26.         }

  27.         ++res;

  28.     next:
  29.         --i;
  30.     }

  31.     cout << res << endl;
  32.     return 0;
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-10 03:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表