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;
}
时间: 2024-08-05 12:19:45

UVa 263 - Number Chains的相关文章

UVA 1558 - Number Game(博弈dp)

UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次可以选一个数字,然后它的倍数,还有其他已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp记忆化去求解,要输出方案就枚举第一步即可,状态转移过程中,选中一个数字,相应的变化写成一个函数,然后就是普通的博弈问题了,必胜态之后必有必败态,必败态之后全是必胜态 代码: #include <stdio.h> #include <string.h> const int N = 105

uva 10706 Number Sequence(找规律)

uva 10706 Number Sequence A single positive integer iis given. Write a program to find the digit located in the position iin the sequence of number groups S1S2-Sk. Each group Skconsists of a sequence of positive integer numbers ranging from 1 to k, w

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

11885 - Number of Battlefields 题意:给周长,求能围成的战场数目,不包括矩形. 思路:具体的递推没递推出来,但是看了网上一个规律,如果包括矩形的答案应该是斐波那契数列(但是奇数情况为0),然后减去矩形数目就是答案,矩形数目为n / 2 - 1,用矩阵快速幂就能求了. 具体的递推过程哪位大神能指点下... 代码: #include <stdio.h> #include <string.h> const long long MOD = 987654321;

uva 11885 - Number of Battlefields(矩阵快速幂)

题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,并且该图形的最小包围矩阵的周长也是p,不包括矩形. 解题思路:矩阵快速幂,如果包含矩形的话,对应的则是斐波那契数列的偶数项,所以对应减去矩形的个数即可. #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const ll MOD = 9876543

UVA - 10706 Number Sequence

先找到是在哪个集合内,再找到是集合内的哪个元素,最后找到元素的第几位数 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; long long table[100010]; long long psum[100010]; int n=100000; void maketable() { int i,flag=1,x=0;

UVa 818 Cutting Chains 题解

难度:β 建议用时:40 min 这题应该有迭代加深搜索的解法的,但我参考网友做法,用暴力枚举法. 大致思路是:枚举圆环的每种开闭状态,统计符合要求的最小的打开的圆环的数量. 要判断打开圆环的某一种方法是否符合要求,容易想到的一个是先判断除去这些已打开的圆环外的闭合圆环有没有组成一个环. 如果还有环,那么无论把打开的圆环怎样重新连城一条链套回去,都不能消除环,而题目要求我们够一条链.所以如果任然存在环的方法是不行的. 0000   0 (此时还有一个环没打开,不能组成链) 0    0 0000

UVA 818 Cutting Chains (DFS)

What a find! Anna Locke has just bought several links of chain some of which may be connected. They are made from zorkium, a material that was frequently used to manufacture jewelry in the last century, but is not used for that purpose anymore. It ha

UVa 11371 - Number Theory for Newbies

題目:給你一個數字n,將裡面每位的數重新組合形成a,b,使得a-b最大且是9的倍數. 分析:數論.題目要求a,b和n的位數相同,不能有前導0. 定理1:交換一個數字中的某兩個位的數,形成的新數組和原數字之差是9的倍數: 證明1:設數字為abc..i..j...xwz,其中每个字母代表一个位,对应值可以相同, 那么随意交换两位i,j得到的新数字为abc..j..i..xwz,做差为9..90..0 *(i-j), 所以一定是9的倍数,得证. 通過上面定理可以繼續證明,任意交換任意位數字形成的新數字

UVA 818 Cutting Chains

题意就是给一张无向图,去掉某些结点,然后连成一条链,问最少去掉几个结点. n很小n<=15,所以直接枚举2^15个状态就行啦. 链的条件是1.无环,2.没有度大于2的点,3.把n个散链连起来需要n-1次拼接,去掉的结点数>=n-1. #include<bits/stdc++.h> using namespace std; const int maxn = 15; int G[maxn][maxn]; int n; int c[maxn]; bool dfs(int u,int s,