题目:给你一个数字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. 原生js实现简单的焦点图效果

    用到一些封装好的运动函数,主要是定时器 效果为图片和图片的描述定时自动更换 <!DOCTYPE html> <html> <head> <meta chars ...

  2. 使用Dreamweaver在一张图片上添加多个热点链接

    所有代码: <html> <head> <meta charset="utf-8"> <title>无标题文档</title& ...

  3. window.showModalDialog的问题

    通过window.showModalDialog的方式弹出B页面,总报“拒绝访问”的错误,将站点添加到受信任站点可以解决这个问题

  4. Android基础TOP3:Activity的线性,相对,帧和表格布局的概括

    线性布局 LinearLayout: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  5. js获取某年某月一共多少天

    const getDaysInMonth = (year, month) => { let date = new Date(year, month, 1); return new Date(da ...

  6. CSS——border

    表格细线: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. SQL基本操作——约束

    我们将主要探讨以下几种约束: 1.NOT NULL 2.UNIQUE 3.PRIMARY KEY 4.FOREIGN KEY 5.CHECK 6.DEFAULT SQL NOTNULL约束:NOT N ...

  8. js 学习笔记---基本概念

    早已接触javascript多年之后,竟然还有这些概念混淆不清,毫不知情,说出来真实无地自容 ! 1.使用严格模式,"use strict",虽然不适用,但是要知道,以免别人使用时 ...

  9. JSP学习笔记 - 内置对象 Request

    1.主要掌握以下5个内置对象及其所属类,必须学会在java docs里根据类名查找相应的方法 request     javax.servlet.http.HttpServletRequest res ...

  10. strcmp 与 _tcscmp

    strcmp 用来比较ANSI字符串,而_tcscmp用来比较UNICODE(宽字符)的字符串.ANSI字符串中,1个英文字母为1个字节,1个中文字符为2个字节,遇到0字符表示字符串结束.而在UNIC ...