(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 ...
随机推荐
- android监听键盘
android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡.一种常用的方法是当Activity设置为 ...
- SqlServer2008 数据库可疑
今天遇到数据库可疑,以前都是直接删了还原,这次没有最新的备份文件,一起看看脚本怎么解决 --最好一句句执行,方便看到错误 USE MASTER GO --开启数据库选项"允许更新" ...
- js——DOM操作(二)
表格属性: tHead:表格头 tBodies:表格正文 tFoot:表格尾 rows:行 cells:列 表单操作: <form id="form1"> <in ...
- mybatis动态sql语句问题
1.关于mybatis的insertintoselect命令未结束问题 添加: useGeneratedKeys="false" 官网的解释是 允许 JD ...
- VS中C++对象的内存布局
本文主要简述一下在Visual Studio中C++对象的内存布局,这里没有什么测试代码,只是以图文的形式来描述一下内存分布,关于测试的代码以及C++对象模型的其他内容大家可以参考一下陈皓先生的几篇博 ...
- what oop ?
最近在做一个app的后台代码.......到底是什么是Oop ,没有感觉到啊,,,,,
- [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array
题目来源: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 题意分析: 在一个不重复的翻转的数组里面找到最小那个 ...
- erlang学习笔记(2)
函数%###geometry.erl###-module(geometry). 定义-export([area/1, function1/2, function2/0, ...]).area({rec ...
- dell PowerEdge R720 自动重启分析
dell PowerEdge R720 自动重启分析 摘要: 一,问题描述: 在同一批服务器当中,碰到这样一台服务器,如果不跑任何服务时没有问题,但一跑任务就是自动重启.既然同样的系统别的服务器都没出 ...
- 在cad中画一条长500mm,垂直90度的线段
视频教程奉上 方法1.点击线段按钮,鼠标指定一点,输入500,再输入<90. 方法2,点击线段按钮,鼠标指定一点,输入500,按tab,再输入90.