Project Euler 92:Square digit chains C++
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 → 1 → 1
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++的更多相关文章
- (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 ...
- Project Euler 92:Square digit chains 平方数字链
题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...
- Project Euler 57: Square root convergents
五十七.平方根收敛(Square root convergents) 二的平方根可以表示为以下这个无穷连分数: \[ \sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac ...
- Project Euler #80: Square root digital expansion
from decimal import getcontext, Decimal def main(): n = int(raw_input()) p = int(raw_input()) getcon ...
- Project Euler 20 Factorial digit sum( 大数乘法 )
题意:求出100!的各位数字和. /************************************************************************* > Fil ...
- Project Euler 16 Power digit sum( 大数乘法 )
题意: 215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26. 21000的各位数字之和是多少? 思路:大数乘法,计算 210 × 100 可加速计算,每 ...
- Project Euler 51: Prime digit replacements
通过替换*3这样一个两位数的第一位,我们可以发现形成的九个数字有六个是质数,即13, 23,43,53,73,83.类似的,如果我们用同样的数字替换56**3这样一个五位数的第三位和第四位,会生成56 ...
- Project Euler 56: Powerful digit sum
一个古戈尔也就是\(10^{100}\)是一个天文数字,一后面跟着一百个零.\(100^{100}\)更是难以想像的大,一后面跟着两百个零.但是尽管这个数字很大,它们各位数字的和却只等于一.考虑两个自 ...
- Project Euler 63: Powerful digit counts
五位数\(16807=7^5\)也是一个五次幂,同样的,九位数\(134217728=8^9\)也是一个九次幂.求有多少个\(n\)位正整数同时也是\(n\)次幂? 分析:设题目要求的幂的底为\(n\ ...
随机推荐
- koa-router源码分析
koa-router源码地址是 koa-router 当前解读版本为7.2.1 关系图 代码结构图 执行流程图 关系对应图 Router方法和属性浅析 methods.forEach 注册注册路由的方 ...
- UnitOfWork知多少
1. 引言 Maintains a list of objects affected by a business transaction and coordinates the writing out ...
- MySql的安装与卸载
首先到官网下载MySQL,点击安装 出现的页面如下,依次安装就好了,安装过程中需要修改的只有编码格式,需要填写的是数据库的密码,MySQL的默认用户名为root,默认端口为3306,端口号最好不要修改 ...
- Hive调优实践
1 文件格式的选择 ORC格式确实要比textFile要更适合于hive,查询速度会提高20-40%左右 例子1: youtube1的文件格式是TextFIle,youtube3的文件格式是orc h ...
- Oracle 11g的安装详细过程
本次采用的电脑为win10 专业版,以下为本人电脑的配置情况: 本人是第一次在win10系统上面进行oracle 11g的安装. 以下为安装步骤: 一.下载软件包 以下为各种oracle安装包的下载网 ...
- [日推荐] 『Streeter』极乐商店邀你一起来尬舞啊!-store.dreawer.com
你听的舞蹈有哪些?Jazz?Poppin?Hiphop?现代舞?民族舞?... 今天推荐一款小程序『Streeter』,几乎包含所有舞蹈视频,偷偷学好了,邀请你来尬舞啊 Streeter 这个Logo ...
- 使用webpack-dev-middleware 和 webpack-hot-middleware 配置一个dev-server
关于Webpack的资料教程网上已经数不胜数,但是对手动配置一个Express server的确不多,于是我对此进行着重的了解一番. webpack-dev-middleware和webpack-ho ...
- 45、concurrent.futures模块与协程
concurrent.futures —Launching parallel tasks concurrent.futures模块同时提供了进程池和线程池,它是将来的使用趋势,同样我们之前学习 ...
- 【技术分享】手把手教你使用PowerShell内置的端口扫描器
[技术分享]手把手教你使用PowerShell内置的端口扫描器 引言 想做端口扫描,NMAP是理想的选择,但是有时候NMAP并不可用.有的时候仅仅是想看一下某个端口是否开放.在这些情况下,PowerS ...
- Java温故而知新-冒泡法排序
冒泡法排序是各种初学者在学习数组与循环结构时都会练习的一种简单排序算法. 冒泡法的精髓在于比较相邻的两个元素,较大的元素会不断的排到队伍后面去,就像水里的泡泡一样不断向上跑. 想像一下倒在一个透明玻璃 ...