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逐个判断,同时统计结果

  1. #include<stdio.h>
  2.  
  3. #define N 10000000
  4.  
  5. int fun(int n)
  6. {
  7. int t, sum;
  8. sum = ;
  9. while(n) {
  10. t = n % ;
  11. sum += t * t;
  12. n /= ;
  13. }
  14. return sum;
  15. }
  16.  
  17. void solve(void)
  18. {
  19. int i, sum, t;
  20. sum = ;
  21. for(i = ; i < N; i++) {
  22. t = fun(i);
  23. while() {
  24. if(t == ) {
  25. sum++;
  26. break;
  27. } else if(t == ) {
  28. break;
  29. } else {
  30. t = fun(t);
  31. }
  32. }
  33. }
  34. printf("%d\n",sum);
  35. }
  36.  
  37. int main(void)
  38. {
  39. solve();
  40. return ;
  41. }

算法二(优化):使用一个bool型数组,保存每次结果,由于最大的中间数为9999999产生的:9^2*7 = 567,所以bool型数组的大小开到600足够

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. #define N 10000000
  5.  
  6. bool a[] = {false};
  7.  
  8. int fun(int n)
  9. {
  10. int t, sum;
  11. sum = ;
  12. while(n) {
  13. t = n % ;
  14. sum += t * t;
  15. n /= ;
  16. }
  17. return sum;
  18. }
  19.  
  20. void solve(void)
  21. {
  22. int i, sum, t, temp;
  23. sum = ;
  24. for(i = ; i < N; i++) {
  25. t = fun(i);
  26. temp = t;
  27. if(a[temp]) {
  28. sum++;
  29. } else {
  30. while() {
  31. t = fun(t);
  32. if(a[t] || t == ) {
  33. a[temp] = true;
  34. sum++;
  35. break;
  36. } else if(t == ) {
  37. break;
  38. } else {
  39. }
  40. }
  41. }
  42. }
  43. printf("%d\n",sum);
  44. }
  45.  
  46. int main(void)
  47. {
  48. solve();
  49. return ;
  50. }
Answer:
8581146

(Problem 92)Square digit chains的更多相关文章

  1. 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 ...

  2. Project Euler 92:Square digit chains 平方数字链

    题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...

  3. (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 ...

  4. (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 ...

  5. (Problem 33)Digit canceling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...

  6. 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 ...

  7. 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 ...

  8. lintcode:快乐数

    快乐数 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...

  9. UVA - 10162 Last Digit

    Description  Problem B.Last Digit  Background Give you a integer number N (1<=n<=2*10100). Ple ...

随机推荐

  1. android监听键盘

    android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡.一种常用的方法是当Activity设置为 ...

  2. SqlServer2008 数据库可疑

    今天遇到数据库可疑,以前都是直接删了还原,这次没有最新的备份文件,一起看看脚本怎么解决 --最好一句句执行,方便看到错误 USE MASTER GO --开启数据库选项"允许更新" ...

  3. js——DOM操作(二)

    表格属性: tHead:表格头 tBodies:表格正文 tFoot:表格尾 rows:行 cells:列 表单操作: <form id="form1"> <in ...

  4. mybatis动态sql语句问题

    1.关于mybatis的insertintoselect命令未结束问题         添加:  useGeneratedKeys="false"     官网的解释是 允许 JD ...

  5. VS中C++对象的内存布局

    本文主要简述一下在Visual Studio中C++对象的内存布局,这里没有什么测试代码,只是以图文的形式来描述一下内存分布,关于测试的代码以及C++对象模型的其他内容大家可以参考一下陈皓先生的几篇博 ...

  6. what oop ?

    最近在做一个app的后台代码.......到底是什么是Oop ,没有感觉到啊,,,,,

  7. [LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array

    题目来源: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 题意分析: 在一个不重复的翻转的数组里面找到最小那个 ...

  8. erlang学习笔记(2)

    函数%###geometry.erl###-module(geometry). 定义-export([area/1, function1/2, function2/0, ...]).area({rec ...

  9. dell PowerEdge R720 自动重启分析

    dell PowerEdge R720 自动重启分析 摘要: 一,问题描述: 在同一批服务器当中,碰到这样一台服务器,如果不跑任何服务时没有问题,但一跑任务就是自动重启.既然同样的系统别的服务器都没出 ...

  10. 在cad中画一条长500mm,垂直90度的线段

    视频教程奉上 方法1.点击线段按钮,鼠标指定一点,输入500,再输入<90. 方法2,点击线段按钮,鼠标指定一点,输入500,按tab,再输入90.