UVa 263 - Number Chains
题目:给你一个数字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的更多相关文章
- UVA 1558 - Number Game(博弈dp)
UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次能够选一个数字,然后它的倍数,还有其它已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp ...
- uva 11885 - Number of Battlefields(矩阵高速幂)
题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,而且该图形的最小包围矩阵的周长也是p,不包含矩形. 解题思路:矩阵高速幂 ...
- UVA 10706 Number Sequence (找规律 + 打表 + 查找)
Problem B Number Sequence Input: standard input Output: standard output Time Limit: 1 second A singl ...
- 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. ...
- UVA 529 Addition Chains(迭代搜索)
Addition Chains An addition chain for n is an integer sequence with the following four propertie ...
- UVA 11885 - Number of Battlefields(斐波那契)
11885 - Number of Battlefields 题意:给周长.求能围成的战场数目.不包含矩形. 思路:详细的递推没递推出来,可是看了网上一个规律,假设包含矩形的答案应该是斐波那契数列(可 ...
- UVA 10006_Carmichael number
题意: N 为合数,对于任意一个在(1,N)之间的数满足 anmodn=a,则称N为Carmichael number,对于给定的N,判断是否为Carmichael number. 分析: 素数区间筛 ...
- UVA 12377 Number Coding --DFS
题意:给一串数字,第一个数是Num的话,要使后面的数字组成Num个数,而且为不降的,将这Num个数分配到9个素因子上作为指数,问能组成多少个不同的数 解法:dfs一遍,看后面的数字能组成Num个不降数 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
随机推荐
- UNIX环境高级编程--3
文件IO 函数lseek: 每个打开文件都有一个与其相关联的“当前文件偏移量”,用来度量从文件开始处计算的字节数.除非指定O_APPEND选项,否则该偏移量被置为0.如果文件描述符指向的是一个管道.F ...
- mysql数据的操作
一.插入数据记录 1.插入完整数据记录 INSERT INTO table_name VALUES(value1,value2......valuen); 2.插入数据记录的一部分 INSERT IN ...
- [转]android 让一个控件按钮居于底部的几种方法
本文转自:http://www.cnblogs.com/zdz8207/archive/2012/12/13/2816906.html android 让一个控件按钮居于底部的几种方法 1.采用lin ...
- Android 将图片网址url转化为bitmap
public Bitmap returnBitMap(final String url){ new Thread(new Runnable() { @Override public void run( ...
- ZipMarket数字内容/素材交易网站源码项目
ZipMarket程序仿自Envato旗下网站,对于想创建数字内容/素材交易平台的站长来说,ZipMarket是一个十分独特和极具创新的解决方案,用户在你的网站注册并购买或出售数字内容/素材作品时,你 ...
- C 语言常用方法技巧
C语言常用方法技巧 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impor ...
- GitHub代码托管平台搭建
GitHub代码托管平台搭建 注册账户以及创建仓库 要想使用github第一步当然是注册github账号了, github官网地址:https://github.com/. 之后就可以创建仓库了(免费 ...
- django 加日志
LOGGING = { 'version': 1, 'disable_existing_loggers': False, # 指定输出的格式,被handler使用. 'formatters': { ' ...
- 初学者怎么才能快速学会Python?
提起对Python的印象,除了全能之外恐怕就是简单易学了.很多人都在推荐新手学Python入门,毕竟语法简单.语句简洁,所谓“人生苦短我用Python”绝不是一句空话.不过也不能忽视一点:Python ...
- Sersync+Rsync实现数据文件实时同步
rsync+inotify-tools与rsync+sersync架构的区别1,rsync+inotify-tools只能记录下被监听的目录发生的变化(增删改)并没有把具体变化的文件或目录记录下来在同 ...