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\ ...
随机推荐
- 【Linux笔记(000) 】-- 系统启动过程
一. 启动流程 BIOS --> MBR(Boot Code) --> 引导程序(GRUB) --> 加载内核 --> 执行Init --> runlevel 二. ...
- KeepAlived+Nginx实现高可用负载
一.环境及安装版本: centos6.5.Nginx1.4.7.keepalived1.3.2 虚拟IP 真是IP Nginx端口 主从分配 10.0.90.215 10.0.90.217 80 MA ...
- vim 基本编辑操作
一.光标跳转到指定行 1.在命令模式下,跳转到指定行 :n (n为行号) 2.命令行加参数 vim +n fileName 3.命令行加参数 vim + fileName 光标移至文件底部 4. ...
- 【Tomcat】重新獲得war包
Extract war in tomcat/webapps #!/bin/bash #----------------------------------------------- # FileNam ...
- 分享一个数据库工具DTOOLS
整理电脑的时候发现一个好的工具——DTOOLS,他是我在09年左右写的一个数据库工具. 可以干什么呢? 我罗列一下: 1.全面的展示数据库字段情况 2. 迅速切换,展示数据库记录情况,不输语句,即点即 ...
- yii2 无法显示debug条的问题解决方法
显示debug条需要设置三个地方 一.web/index.php defined('YII_ENV') or define('YII_ENV', 'dev');//设置为开发者模式 二.config/ ...
- 谈谈培训机构的"骗局"给新人一些建议
前言 本文只谈"骗局",不谈其他,绝不引战,如有错误,希望指出我会及时改正,想要讨论的可以在留言区写下你的观点和经历. 为什么要写这篇文章呢,近些年培训这个话题也比较火,很多在看这 ...
- Springmvc+mybatis的定时器配置文件spring-quartz.xml
<!-- 定时器配置文件---.xml 一个cron表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左至右,这些元素的定义如下: 1.秒(0–59) 2.分钟(0–59) 3.小时(0– ...
- 创建springbootdemo后运行报MongoSocketOpenException错误解决方法
在类SpringbootdemoApplication上右键Run as选择Spring Boot App后Console输出报错日志如下: com.mongodb.MongoSocketOpenEx ...
- windows server 2012 + sql server 2008 r2安装
windows server 2012 r2 里面安装 sql server 2008 r2 问题总结 前提是 windows server 2012 r2 已经安装完成 ,(仅仅是安装完成 啥服 ...