博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler Problem 92 Square digit chains
阅读量:5928 次
发布时间:2019-06-19

本文共 2100 字,大约阅读时间需要 7 分钟。

Problem 92

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.

For example,

44 → 32 → 13 → 10 → 11

85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89

Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.

How many starting numbers below ten million will arrive at 89?

C++(Faster):

#include 
#include
#include
using namespace std;const int MAXN = 10000000;int arrive[MAXN], chainno[MAXN];int no;int nextval(int n){ int sum, v; sum = 0; while(n) { v = n % 10; sum += v * v; n /= 10; } return sum;}void makechain(int n){ no++; while(n != 1 && n != 89) { if(chainno[n] > 0) { n = arrive[chainno[n]]; break; } chainno[n] = no; n = nextval(n); } arrive[no] = n;}int main(){ int n; while(cin >> n && n <= MAXN) { memset(arrive, 0, sizeof(arrive)); memset(chainno, 0, sizeof(chainno)); chainno[1] = 1; arrive[1] = 1; chainno[89] = 2; arrive[2] = 89; no = 2; for(int i=1; i

C++:

#include 
#include
#include
using namespace std;const int MAXN = 10000000;int arrive[MAXN];int nextval(int n){ int sum, v; sum = 0; while(n) { v = n % 10; sum += v * v; n /= 10; } return sum;}void makechain(int n){ stack
s; while(n != 1 && n != 89) { if(arrive[n] > 0) { n = arrive[n]; break; } s.push(n); n = nextval(n); } while(!s.empty()) { int val = s.top(); s.pop(); arrive[val] = n; }}int main(){ int n; while(cin >> n && n <= MAXN) { memset(arrive, 0, sizeof(arrive)); arrive[1] = 1; arrive[89] = 89; for(int i=1; i

转载于:https://www.cnblogs.com/tigerisland/p/7564005.html

你可能感兴趣的文章
许可证( License LicenseLicenseLicenseLicenseLicense)服务器配置
查看>>
windows server 2012 dhcp 配置故障转移
查看>>
OutLook2016修改注册表迁移.ost文件数据
查看>>
我的友情链接
查看>>
exchange2013 owa-outlook界面语言
查看>>
Redis集群监控RedisClusterManager
查看>>
S5PC100基于I2C子系统的lm75驱动流程图
查看>>
我的友情链接
查看>>
【个人笔记】关于IO类中流的整理
查看>>
迁移SVN注意事项及操作方法
查看>>
利用percona-toolkit工具检查MySQL数据库主从复制数据的一致性,以及修复。
查看>>
UVA 11090 Going in Cycle!! 二分答案 + bellman-ford
查看>>
MFC的Button和Static控件
查看>>
应用环境下的TIME_WAIT和CLOSE_WAIT
查看>>
我的友情链接
查看>>
实战~~整个网络无法浏览,提示网络不存在或者尚未启动
查看>>
powershell实现设置程序相关性脚本
查看>>
Linux的FHS(文件系统结构标准)剖析
查看>>
我的友情链接
查看>>
zabbix2.2升级到zabbix3.0.2
查看>>