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

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

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

说明:注意初始化。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath> using namespace std; //hash
typedef struct hash_node
{
int path;
hash_node* next;
}hnode;
hnode* tabel[1002];
hnode nodeh[1002];
int hash_size; void hash_initial()
{
memset(tabel, 0, sizeof(tabel));
hash_size = 0;
} int hash_find(int s)
{
int v = s%1001;
for (hnode* p = tabel[v] ; p ; p = p->next)
if (p->path == s)
return 1;
nodeh[hash_size].next = tabel[v];
nodeh[hash_size].path = s;
tabel[v] = &nodeh[hash_size ++];
return 0;
}
//hash end int buf[11];
int number(int *a, int n)
{
int value = 0;
for (int i = 0 ; i < n ; ++ i) {
value *= 10;
value += a[i];
}
return value;
} int cmp1(int a, int b) { return a < b; }
int cmp2(int a, int b) { return a > b; } int main()
{
int n,count,sum,a,b;
while (~scanf("%d",&n) && n) {
printf("Original number was %d\n",n); hash_initial();
sum = 0;
do {
hash_find(n);
sum ++;
count = 0;
while (n) {
buf[count ++] = n%10;
n /= 10;
}
sort(buf, buf+count, cmp2);
a = number(buf, count);
sort(buf, buf+count, cmp1);
b = number(buf, count);
printf("%d - %d = %d\n",a,b,n = a-b);
}while (!hash_find(n));
printf("Chain length %d\n\n",sum);
}
return 0;
}

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. 涨知识III - 百度2016校园招聘——移动软件研发工程师

    1.列关于线程调度的叙述中,错误的是(). 正确答案 :BE A调用线程的sleep()方法,可以使比当前线程优先级低的线程获得运行机会 B调用线程的yeild()方法,只会使与当前线程相同优先级的线 ...

  2. Zookeeper的临时节点和永久节点

    Zookeeper中节点分为两种:临时节点和永久节点. 临时节点有一个节点: 当创建临时节点的程序停掉之后,这个临时节点就会消失. 更直观的,如下   Persistent是临时节点. Persist ...

  3. MySQL关于视图的创建

    -- 视图就是一条select 语句 执行后返回结果集,是一种虚拟表,是一个逻辑表 -- 方便操作,减少复杂的SQL语句,增加可读性,更加安全一些 create view demo_view as s ...

  4. JVM 内存区域划分

    一.运行时数据区包括哪几部分? 根据<Java虚拟机规范>的规定,运行时数据区通常包括这几个部分:程序计数器(Program Counter Register).Java栈(VM Stac ...

  5. [ JSOI 2015 ] Salesman

    \(\\\) \(Description\) 给出一棵以\(1\)为根的\(N\)个节点的树,开始的时候你在\(1\)号节点. 除了\(1\)号节点以外,每个点都有访问次数限制\(t_i\),即到达该 ...

  6. 【转载】HTTP 缓存的四种风味与缓存策略

    原文地址:https://segmentfault.com/a/1190000006689795 HTTP Cache 通过网络获取内容既缓慢,成本又高:大的响应需要在客户端和服务器之间进行多次往返通 ...

  7. (转)分布式文件存储FastDFS(六)FastDFS多节点配置

    http://blog.csdn.net/xingjiarong/article/details/50759918 前面几篇关于FastDFS的博客中介绍了如何在一台机器上搭建一个简易的FastDFS ...

  8. iOS 9 中可用的受信任根证书列表

    iOS 受信任证书存储区包含随 iOS 预安装的可信根证书.  https://support.apple.com/zh-cn/HT205205 关于信任和证书 iOS 9 受信任证书存储区包含三类证 ...

  9. Shell基本运算符

    原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用. expr 是一款表达式计算工具,使用它能完成表达式的求值操作. 例如,两个数相加(注意使用 ...

  10. ionic错误

    1. 问题:Error: read ECONNRESET 启动使用ionic serve启动服务器之后只要一刷新界面就会导致服务器关闭,报的错误如下: events.js:136 throw er; ...