题目:给你一个数字n0。将它的每一个位的数字按递增排序生成数a,按递减排序生成数b,

新的数字为n1 = a-b,下次依照相同方法计算n1,知道出现循环,问计算了多少次。

分析:数论、模拟。直接模拟计算就可以,利用hash表判重。

说明:注意初始化。

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. //hash
  11. typedef struct hash_node
  12. {
  13. int path;
  14. hash_node* next;
  15. }hnode;
  16. hnode* tabel[1002];
  17. hnode nodeh[1002];
  18. int hash_size;
  19.  
  20. void hash_initial()
  21. {
  22. memset(tabel, 0, sizeof(tabel));
  23. hash_size = 0;
  24. }
  25.  
  26. int hash_find(int s)
  27. {
  28. int v = s%1001;
  29. for (hnode* p = tabel[v] ; p ; p = p->next)
  30. if (p->path == s)
  31. return 1;
  32. nodeh[hash_size].next = tabel[v];
  33. nodeh[hash_size].path = s;
  34. tabel[v] = &nodeh[hash_size ++];
  35. return 0;
  36. }
  37. //hash end
  38.  
  39. int buf[11];
  40. int number(int *a, int n)
  41. {
  42. int value = 0;
  43. for (int i = 0 ; i < n ; ++ i) {
  44. value *= 10;
  45. value += a[i];
  46. }
  47. return value;
  48. }
  49.  
  50. int cmp1(int a, int b) { return a < b; }
  51. int cmp2(int a, int b) { return a > b; }
  52.  
  53. int main()
  54. {
  55. int n,count,sum,a,b;
  56. while (~scanf("%d",&n) && n) {
  57. printf("Original number was %d\n",n);
  58.  
  59. hash_initial();
  60. sum = 0;
  61. do {
  62. hash_find(n);
  63. sum ++;
  64. count = 0;
  65. while (n) {
  66. buf[count ++] = n%10;
  67. n /= 10;
  68. }
  69. sort(buf, buf+count, cmp2);
  70. a = number(buf, count);
  71. sort(buf, buf+count, cmp1);
  72. b = number(buf, count);
  73. printf("%d - %d = %d\n",a,b,n = a-b);
  74. }while (!hash_find(n));
  75. printf("Chain length %d\n\n",sum);
  76. }
  77. return 0;
  78. }

UVa 263 - Number Chains的更多相关文章

  1. UVA 1558 - Number Game(博弈dp)

    UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次能够选一个数字,然后它的倍数,还有其它已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp ...

  2. uva 11885 - Number of Battlefields(矩阵高速幂)

    题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,而且该图形的最小包围矩阵的周长也是p,不包含矩形. 解题思路:矩阵高速幂 ...

  3. UVA 10706 Number Sequence (找规律 + 打表 + 查找)

    Problem B Number Sequence Input: standard input Output: standard output Time Limit: 1 second A singl ...

  4. uva 10706 Number Sequence(数学规律)

    题目连接:10706 - Number Sequence 题目大意:有一个有0 ~ 9组成的序列,1   1 2    1 2 3   1 2 3 4   1 2 3 4 5 ....就是第一位为1. ...

  5. UVA 529 Addition Chains(迭代搜索)

      Addition Chains  An addition chain for n is an integer sequence  with the following four propertie ...

  6. UVA 11885 - Number of Battlefields(斐波那契)

    11885 - Number of Battlefields 题意:给周长.求能围成的战场数目.不包含矩形. 思路:详细的递推没递推出来,可是看了网上一个规律,假设包含矩形的答案应该是斐波那契数列(可 ...

  7. UVA 10006_Carmichael number

    题意: N 为合数,对于任意一个在(1,N)之间的数满足 anmodn=a,则称N为Carmichael number,对于给定的N,判断是否为Carmichael number. 分析: 素数区间筛 ...

  8. UVA 12377 Number Coding --DFS

    题意:给一串数字,第一个数是Num的话,要使后面的数字组成Num个数,而且为不降的,将这Num个数分配到9个素因子上作为指数,问能组成多少个不同的数 解法:dfs一遍,看后面的数字能组成Num个不降数 ...

  9. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

随机推荐

  1. UNIX环境高级编程--3

    文件IO 函数lseek: 每个打开文件都有一个与其相关联的“当前文件偏移量”,用来度量从文件开始处计算的字节数.除非指定O_APPEND选项,否则该偏移量被置为0.如果文件描述符指向的是一个管道.F ...

  2. mysql数据的操作

    一.插入数据记录 1.插入完整数据记录 INSERT INTO table_name VALUES(value1,value2......valuen); 2.插入数据记录的一部分 INSERT IN ...

  3. [转]android 让一个控件按钮居于底部的几种方法

    本文转自:http://www.cnblogs.com/zdz8207/archive/2012/12/13/2816906.html android 让一个控件按钮居于底部的几种方法 1.采用lin ...

  4. Android 将图片网址url转化为bitmap

    public Bitmap returnBitMap(final String url){ new Thread(new Runnable() { @Override public void run( ...

  5. ZipMarket数字内容/素材交易网站源码项目

    ZipMarket程序仿自Envato旗下网站,对于想创建数字内容/素材交易平台的站长来说,ZipMarket是一个十分独特和极具创新的解决方案,用户在你的网站注册并购买或出售数字内容/素材作品时,你 ...

  6. C 语言常用方法技巧

    C语言常用方法技巧 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impor ...

  7. GitHub代码托管平台搭建

    GitHub代码托管平台搭建 注册账户以及创建仓库 要想使用github第一步当然是注册github账号了, github官网地址:https://github.com/. 之后就可以创建仓库了(免费 ...

  8. django 加日志

    LOGGING = { 'version': 1, 'disable_existing_loggers': False, # 指定输出的格式,被handler使用. 'formatters': { ' ...

  9. 初学者怎么才能快速学会Python?

    提起对Python的印象,除了全能之外恐怕就是简单易学了.很多人都在推荐新手学Python入门,毕竟语法简单.语句简洁,所谓“人生苦短我用Python”绝不是一句空话.不过也不能忽视一点:Python ...

  10. Sersync+Rsync实现数据文件实时同步

    rsync+inotify-tools与rsync+sersync架构的区别1,rsync+inotify-tools只能记录下被监听的目录发生的变化(增删改)并没有把具体变化的文件或目录记录下来在同 ...