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. Java企业微信开发_07_素材管理之上传本地临时素材文件

    一.本节要点 1.临时素材有效期 media_id是可复用的,同一个media_id可用于消息的多次发送(3天内有效) 2.上传文件时的http请求里都有啥 具体原理可参看: 为什么上传文件的表单需要 ...

  2. 如何解决修改AzureVM默认RDP端口后,连不上的问题

    Enter-PSSession -ComputerName 139.219.135.45 -Port 5986 -Authentication Negotiate -Credential 'mssto ...

  3. 【Ubuntu Desktop】安装主流桌面

    ubuntu的桌面环境实在多,在这里选了几款主流的桌面环境,大家可以按需安装使用. 1.GNOME 2 Classic 经典老界面 gnome2   after reboot,choose GNOME ...

  4. MAVEN 编译打包时报“.....找不到符号” 的处理方法总结

    这个错都是在MAVEN插件在编译的时候报的,所以问题一定是出在编译的环节上. 这个时候就要好好检查MAVEN的编译配置, 1.看看配置里的编译版本和本机环境上配置的java版本是否一致,有时候报错的类 ...

  5. 错误Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:

    今天写PHP代码,遇到了这个非常不友好的报错(Deprecated: mysql_connect(): The mysql extension is deprecated and will be re ...

  6. Java获取Object属性值

    做了一个拦截参数的需求,需要获取普通参数和对象参数 参数是Object类型,Object[] paramValues = pjp.getArgs(); 1.获取普通参数 ;i<paramValu ...

  7. 7.7 WPF后台代码绑定如果是属性,必须指定一下数据上下文才能实现,而函数(click)就不用

    如: private bool _IsExportWithImage; /// <summary> /// 是否选择导出曲线图 /// </summary> public bo ...

  8. 在Python的Flask框架下Address already in use [地址已在使用中]

    出现这种错误提示, 说明你已经有一个流程绑定到默认端口(5000).如果您之前已经运行过相同的模块,则很可能该进程仍然绑定到端口. 首先使用端口窗口查找进程 : sudo  lsof  - i : 5 ...

  9. Jmeter之Bean shell学习(一)

    一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...

  10. [js高手之路]node js系列课程-图解express+supervisor+ejs用法

    上文通过node js自带的http模块搭建了一个简易的服务器,实际在开发中,一般用的是express框架,本文我们就来讲讲项目开发中必备不可少的几样东西: 服务器( express ) 路由( ex ...