(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 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?
题目大意:
通过将一个数各位的平方不断相加,直到遇到已经出现过的数字,可以形成一个数字链。
例如:
44
32
13
10
1
1 85
89
145
42
20
4
16
37
58
89
因此任何到达1或89的数字链都会陷入无限循环。令人惊奇的是,以任何数字开始,最终都会到达1或89。
以一千万以下的数字n开始,有多少个n会到达89?
算法一:常规方法,从2~10000000逐个判断,同时统计结果
#include<stdio.h> #define N 10000000 int fun(int n)
{
int t, sum;
sum = ;
while(n) {
t = n % ;
sum += t * t;
n /= ;
}
return sum;
} void solve(void)
{
int i, sum, t;
sum = ;
for(i = ; i < N; i++) {
t = fun(i);
while() {
if(t == ) {
sum++;
break;
} else if(t == ) {
break;
} else {
t = fun(t);
}
}
}
printf("%d\n",sum);
} int main(void)
{
solve();
return ;
}
算法二(优化):使用一个bool型数组,保存每次结果,由于最大的中间数为9999999产生的:9^2*7 = 567,所以bool型数组的大小开到600足够
#include <stdio.h>
#include <stdbool.h> #define N 10000000 bool a[] = {false}; int fun(int n)
{
int t, sum;
sum = ;
while(n) {
t = n % ;
sum += t * t;
n /= ;
}
return sum;
} void solve(void)
{
int i, sum, t, temp;
sum = ;
for(i = ; i < N; i++) {
t = fun(i);
temp = t;
if(a[temp]) {
sum++;
} else {
while() {
t = fun(t);
if(a[t] || t == ) {
a[temp] = true;
sum++;
break;
} else if(t == ) {
break;
} else {
}
}
}
}
printf("%d\n",sum);
} int main(void)
{
solve();
return ;
}
|
Answer:
|
8581146 |
(Problem 92)Square digit chains的更多相关文章
- 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 ...
- Project Euler 92:Square digit chains 平方数字链
题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...
- (Problem 74)Digit factorial chains
The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...
- (Problem 34)Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...
- (Problem 33)Digit canceling fractions
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...
- Project Euler:Problem 63 Powerful digit counts
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...
- Porject Euler Problem 6-Sum square difference
我的做法就是暴力,1+...+n 用前n项和公式就行 1^2+2^2+....+n^2就暴力了 做完后在讨论版发现两个有趣的东西. 一个是 (1+2+3+...+n)^2=(1^3)+(2^3)+(3 ...
- lintcode:快乐数
快乐数 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...
- UVA - 10162 Last Digit
Description Problem B.Last Digit Background Give you a integer number N (1<=n<=2*10100). Ple ...
随机推荐
- js 控制台的错误提示
错误:程序运行过程中发生的异常状态 导致程序停止运行——异常 错误处理:当程序发生错误时,保证程序不退出的机制 发生错误时,程序会自动创建一个Error对象: Error对象中仅封装了错误的信息 js ...
- PHP基础示例:商品信息管理系统v1.1
实现目标:使用php和mysql写一个商品信息管理系统,并带有购物车功能 一.创建数据库和表 1.创建数据库和表:demodb 2.创建表格:goods 字段:商品编号,商品名称,商品类型,商品图片, ...
- MYSQL区分大小写
MYSQL区分大小写 1.linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写: 2.用root帐号登录后,在/etc/my.cnf 中的[mysqld]后添加添加lower ...
- OpenID倡议:别再创建新的用户名和密码
原文作者:Jeff Atwood 随着Stack Overflow开发工作的逐步深入,我们不可回避地碰到了这个问题:我们需要让用户登录,即使网络上用户名/密码已经泛滥成灾,我们也只能随波逐流.我已经有 ...
- EditText 默认不获取焦点,弹出软键盘布局变形解决方案
关于弹出软键盘布局变形解决方案: 在androidMainfest.xml文件中在此Activity中写入 android:windowSoftInputMode="adjustPan&qu ...
- IE6下不能定义1px高度的容器和IE6 双边距
(1)触发的条件---定义一个div,将容器的高度设置成1px (2) 编码得到的结果---在IE6浏览器中,容器的高度不是1px 而是18px (3)出现问题的原因---是因为IE6浏 ...
- 50行实现简易HTTP服务器
话说由于一直很懒,所以博客好像也没怎么更新...今天有空就写一下吧. 最近在看node.js的时候开始对http协议感兴趣了,毕竟node一开始就是为了做web服务器而产生的.于是试着想了一下大概的思 ...
- [C#]Windows文件分类器小程序
我平时习惯把各种文件都下载在`下载`文件夹中,时间久了,文件多了,想要找个文件就不那么方便了.于是我就想自己写一个小程序来实现下载文件的自动整理.我想到的文件分类方式是按照文件扩展名分类:把各文件移动 ...
- 不同的strcmp
Android libc中的strcmp https://android.googlesource.com/platform/bootable/bootloader/legacy/+/donut-re ...
- 1.org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
org.hibernate.MappingException: No Dialect mapping for JDBC type: -9 原因:Hibernate框架的方言(Dialect )没有数据 ...