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?

本题求得是数字 的数位平方和为89 的个数。

很简单,暴力大法好。

剪枝是必要的,要不会吃不消。

数字末为89的链下处简称89链

一条数链中 的数字无非是新数字和 已出现过的数字,89链标记已出现过的数字。

在求链过程中,出现已标记的数字,那么该链必定可到达89。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int q(int x)
{
    return x*x;
}
int f(int x)
{
    int s=0;
    while(x>0)
    {
        s+=q(x%10);
        x/=10;
    }
    return s;
}
int vis[10000000];
int main()
{
    int cnt=0;
    for(int i=2;i<10000000;i++)
    {
        int temp=0;
        int c=i;
        while(c!=1)
        {
            if(c==89||vis[c]){
                temp=1;
                cnt++;
                break;
            }
            c=f(c);         }
        if(temp==1) //如是89链,则标记数字
        {
            int c=i;
            while(c!=1&&!vis[c])
            {
                if(c==89){
                    temp=1;
                    break;
                }
                c=f(c);
                vis[c]=1;
 
            }
        }
    }     cout<<cnt<<endl;
}

  

Project Euler 92:Square digit chains C++的更多相关文章

  1. (Problem 92)Square digit chains

    A number chain is created by continuously adding the square of the digits in a number to form a new ...

  2. Project Euler 92:Square digit chains 平方数字链

    题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...

  3. Project Euler 57: Square root convergents

    五十七.平方根收敛(Square root convergents) 二的平方根可以表示为以下这个无穷连分数: \[ \sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac ...

  4. Project Euler #80: Square root digital expansion

    from decimal import getcontext, Decimal def main(): n = int(raw_input()) p = int(raw_input()) getcon ...

  5. Project Euler 20 Factorial digit sum( 大数乘法 )

    题意:求出100!的各位数字和. /************************************************************************* > Fil ...

  6. Project Euler 16 Power digit sum( 大数乘法 )

    题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...

  7. Project Euler 51: Prime digit replacements

    通过替换*3这样一个两位数的第一位,我们可以发现形成的九个数字有六个是质数,即13, 23,43,53,73,83.类似的,如果我们用同样的数字替换56**3这样一个五位数的第三位和第四位,会生成56 ...

  8. Project Euler 56: Powerful digit sum

    一个古戈尔也就是\(10^{100}\)是一个天文数字,一后面跟着一百个零.\(100^{100}\)更是难以想像的大,一后面跟着两百个零.但是尽管这个数字很大,它们各位数字的和却只等于一.考虑两个自 ...

  9. Project Euler 63: Powerful digit counts

    五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...

随机推荐

  1. How to generate file name according to datetime in bat command

    On the command line, note: must copy and paste below lines of code and then execute them one by one: ...

  2. TF-IDF模型详解

    1. 理论基础 由于数据挖掘所有数据都要以数字形式存在,而文本是以字符串形式存在.所以进行文本挖掘时需要先对字符串进行数字化,从而能够进行计算.TF-IDF就是这样一种技术,能够将字符串转换为数字,从 ...

  3. Coursera无法正常提交作业

    如下图,upload服务貌似被墙了,想要提交作业的话需要临时科学上网.

  4. python+selenium自动化软件测试(第12章):Python读写XML文档

    XML 即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 行定义的源语言.xml 有如下特征: 首先,它是有标签对组成:<aa></aa> ...

  5. 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据

    前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于“父子组件通信”的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰符做父子组件数据双向 ...

  6. macaca 测试web(3)

    上回书说到 macaca 测试web(2)  使用ddt做参数驱动化, 有些人会说,你好low,我说怎么low呢,他说你看看你的脚本就放在一个文件里,对于我们小白来说,这看起来很乱啊,能不能给我拆分, ...

  7. adb 安装apk 报错:Failure [INSTALL_FAILED_CPU_ABI_INCOMPATIBLE]

    这是因为系统里缺少了 Google Play 市场等各种谷歌服务应用,其实是因为版权问题,从 2.0 版本开始 Genymotion  提供的虚拟设备都已经移除了 Google Apps  以及 AR ...

  8. MySql数据库导入导出

    1.导出整个数据库     mysqldump -u 用户名 -p 数据库名 > 存放位置     比如:     mysqldump -u root -p project > c:/a. ...

  9. selenium 对https网站(加密证书)进行自动化测试

    由于公司需要,被测网站有证书加密,由于在selenium启动firefox的时候,它会重新建一个profile作为启动的profile,所以无论你怎么把站点设为可信任站点,在selenium启动的fi ...

  10. 通过新浪ip地址库获取用户省份

    <script src="http://apps.bdimg.com/libs/jquery/1.11.3/jquery.min.js"></script> ...