(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 ...
随机推荐
- VB.Net常用数学函数整理
System.Math 类中定义了用于数学计算的函数.Math 类包括三角函数.对数函数和其他常用数学函数.下列函数是在 System 名称空间的 Math 类中定义的函数. 注意:要使用这些函数 ...
- C语言处理CSV文件的方法(一)
什么是CSV文件 CSV是 Comma-separated values (逗号分隔值)的首字母缩写,它通常是以逗号且不仅限于逗号分隔各个值,我们都叫他CSV. 看下面的例子: China, Shan ...
- leetcode Binary Tree Paths python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- javascript 数组的常用操作函数
join() Array.join(/* optional */ separator) 将数组转换为字符串,可带一个参数 separator (分隔符,默认为“,”). 与之相反的一个方法是:Stri ...
- Java中的String,StringBuffer,StringBuilder详解与区别
1.String Java中string类是不可变的,其中在声明的源代码中用的final,所以只能声明一次.所以每次在明面上的改变其实是重新生成一个String对象,指针指向新的String对象.同时 ...
- Android padding和margin的区别
如: Padding 为内边框,指该控件内部内容,如文本/图片距离该控件的边距 Margin 为外边框,指该控件距离边父控件的边距 如: 当按钮分别设置以上两个属性时,得到的效果是不一样的. andr ...
- python 日期、时间戳转换
获取当前日期: from datetime import datetime IN:datetime.now() OUT:datetime(2016,10,19,6,51,21,72341) 转化为字符 ...
- 初识C(2)---从printf函数开始
继承[K&R]的传统,我们的第一个C语言程序也是“Hello, World.”. 书写C语言程序的大前提:C语言中的语法符号必须都是英文字符,即在中文输入法关闭状态下输入的字符. 例 1. H ...
- Oracle EBS-SQL (SYS-6):sys_在线用户职责查询.sql
/*线用户查询-1*/ SELECT FSAV.USER_NAME,FU.DESCRIPTION,FSAV.RESPONSIBILITY_NAME,FSAV.USER_FORM_NAME,FSAV.L ...
- 限制ITEM读取其它物料的物料描述
应用 Oracle Purchasing 层 Level Function 函数名 Funcgtion Name CUXPOXPOEPO 表单名 Form Name POXPOEPO 说明 Des ...