当前位置: 首页 > news >正文

Project Euler Problem 92 Square digit chains

Square digit chains

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 <iostream>
#include <cstring>
#include <stack>

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<n; i++)
            if(i != 1 && i != 89)
                makechain(i);

        int count = 0;
        for(int i=1; i<n; i++)
            if(arrive[chainno[i]] == 89)
                count++;

        cout << count << endl;
    }

    return 0;
}




C++:

#include <iostream>
#include <cstring>
#include <stack>

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<int> 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<n; i++)
            makechain(i);

        int count = 0;
        for(int i=1; i<n; i++)
            if(arrive[i] == 89)
                count++;

        cout << count << endl;
    }

    return 0;
}






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

相关文章:

  • SCOI2010第一场
  • 关键词过滤算法【转】
  • easyui toopTip,鼠标划过悬浮,显示一个小提示框的方法
  • spring 事物的一些理解
  • FMDB支持的事务类型
  • 自动化安装Mysql5.6-脚本实现
  • Java 反射解析指定jar包出现ClassNotFoundException异常,处理方式
  • 通过Adobe Encode CC 2017,将一张静态图生成一个长时间的视频。
  • centos7-msyql-慢查询优化
  • centos7-mysql-分表
  • python初学之魔法方法1
  • 50G存储-免费代码托管工具公测上线
  • 学习LaTex
  • Linux 打印 颜色显示
  • java 集合框架(四)Set
  • 3.7、@ResponseBody 和 @RestController
  • Apache的80端口被占用以及访问时报错403
  • C++11: atomic 头文件
  • IndexedDB
  • java正则表式的使用
  • Js基础知识(一) - 变量
  • Sequelize 中文文档 v4 - Getting started - 入门
  • Travix是如何部署应用程序到Kubernetes上的
  • vue 个人积累(使用工具,组件)
  • Web设计流程优化:网页效果图设计新思路
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 排序(1):冒泡排序
  • 想写好前端,先练好内功
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 优秀架构师必须掌握的架构思维
  • Mac 上flink的安装与启动
  • #NOIP 2014# day.1 T2 联合权值
  • $.ajax,axios,fetch三种ajax请求的区别
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (6)STL算法之转换
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (五)关系数据库标准语言SQL
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • ****Linux下Mysql的安装和配置
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • *1 计算机基础和操作系统基础及几大协议
  • .NET CLR基本术语
  • .NET Core 和 .NET Framework 中的 MEF2
  • .NET中GET与SET的用法
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @Pointcut 使用
  • @SuppressWarnings注解
  • @我的前任是个极品 微博分析
  • [ 手记 ] 关于tomcat开机启动设置问题
  • [ 云计算 | AWS ] 对比分析:Amazon SNS 与 SQS 消息服务的异同与选择
  • []使用 Tortoise SVN 创建 Externals 外部引用目录