uva 10591 - Happy Number hash

   非常简单的题,易知平方和后不超过800,开个800的数组,预处理,然后直接定址就可以了,不过要注意a和an的区别

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int is[800];
int solve(int now)
{
    if(now<800&&is[now]!=-1)return is[now];
    int t=now,ans=0;
    while(t)
    {
        ans+=(t%10)*(t%10);
        t/=10;
    }
    if(now<800)is[now]=0;
    t=solve(ans);
    if(now<800)is[now]=t;
    return t;
}
int main()
{
    int i,C=0,T,t;
    memset(is,-1,sizeof(is));
    is[1]=1;
    for(i=2;i<800;i++)
      solve(i);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&t);
        printf("Case #%d: %d is %s number.\n",++C,t,solve(t)?"a Happy":"an Unhappy");
    }
}
时间: 2024-08-03 16:08:54

uva 10591 - Happy Number hash的相关文章

uva 10591 - Happy Number

点击打开链接 题目意思:  给定一个数n,然后进行操作,先求出这个数每一位的平方和,然后这个和替代n继续做这个操作,知道当前的n为1 或 n之前以经出现过 ,如果n等于则是happy number ,反之不是. 解题思路:  暴力搜素+状态判重.                      对于这一题一个状态就是这个数字n,由于最大的n是10^9,那么平方和最大不超过1000,我们开一个vis[1000]数组来做为标记数组即可,当遇到n 初始值 或 vis[n] = 1则退出,初始化vis[1]

UVa 10591:Happy Number

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1532 类型: 哈希表 原题: Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let

UVa 188:Perfect Hash

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=124 类型: 哈希 原题: Perfect Software, Inc. has obtained a government contract to examine text flowing through a high-speed network

UVa 10706:Number Sequence

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1647 原题: A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of n

UVa 704:Colour Hash, 双向bfs

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=645 类型:隐式图搜索,双向bfs 原题: This puzzle consists of two wheels. Both wheels can rotate both clock and counter-clockwise. They contai

uva 10624 Super Number 回溯

  暴力回溯,用位运算加个奇偶剪枝   注意取余的效率,因为取余实在太慢了,所以要最少的进行取余运算,所以就是每19位进行次取余,这样1s内就过了 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <stdio.h> int ans[50]; char nok(int now) { int i,

uva 10624 - Super Number

点击打开链接 题目意思:  给定n和m 现在要求找到一个m位数的树使得,对于m的前i位数都是i的倍数,n <= i <= m, 而且这个数的第一位数不能为0,如果找到就输出这个数,否则输出-1 解题思路:    1  目前只懂得用深搜回溯做,只是这一题的数据没有想象中的那么大,所以加上一些剪枝即可水过(数据强的话肯定TLE)                       2  我们知道这个解空间树有m层,每一层的可能取值有10种(第一个除外),那么最大的m = 29 ,那么时间复杂都就是 10^

AS2.0中实现数据结构-哈希表

数据|数据结构 在游戏制作中我们经常需要存储一些离散的对象数据,比如道具箱里的道具,经常需要执行插入和删除操作,而且道具之间没有联系是无序排列的.有些人会说直接用数组不就得了,但是有大量数据存储时的数组的删除插入操作的效率是很低的.因此我们需要哈希表这样的可以提供快速的插入和删除,查找操作的数据结构,不论哈希表中有多少数据,插入和删除操作只需要接近常量的时间:即O(1)的时间级.既然这么好那么我们的AS可以实现吗?当然可以!AS发展到AS2.0,已经成为在语法上更接近于Java + Pascal

itpub坛友问题--基于普通表或分区表创建索引,会占用临时表空间及何时回收临时表空间

个人简介: 8年oracle从业经验,具备丰富的oracle技能,目前在国内北京某专业oracle服务公司从事高级技术顾问.        服务过的客户:           中国电信           中国移动           中国联通           中国电通           国家电网           四川达州商业银行           湖南老百姓大药房           山西省公安厅           中国邮政           北京302医院