UVa 10591 - Happy Number

RK3568-Linux-麒麟系统---更新制作麒麟rootfs RK3568 Linux系统 麒麟国产麒麟镜像 阅读详情

题意:将给出数各个位上的数字的平方和加起来得到新的数字,依次往下推新数字,直到得到得到数字1(Happy number)或是与之前重复的数字(unhappy number)。

利用哈希表查找判重或是直接打表,因为输入最大是10的9次方的数字,因此得出的数字最大为9*9*9 = 729。

代码一(hash 0.020s):

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;
const int MaxSize  = 850;
int head[MaxSize], next[MaxSize];
int solve(int n)
{
    int t, sum = 0;
    while(n)
    {
        t = n % 10;
        sum += t * t;
        n /= 10;
    }
    return sum;
}
int insert(int s, int h)
{
    int u = head[h];
    while(u)
    {
        if(h == 1)
            return 1;
        if(head[u] == head[s])
            return 2;
        u = next[u];
    }
    next[s] = head[h];
    head[h] = s;
    return 0;
}
int main()
{
#ifdef test
    freopen("sample.txt", "r", stdin);
#endif
    int p, n, flag, cct = 0;
    scanf("%d", &p);
    while(p--)
    {
        int rear = 2;
        scanf("%d", &n);
        printf("Case #%d: %d", ++cct, n);
        memset(head, 0, sizeof(head));
        n = solve(n);
        while(1)
        {
            flag = insert(rear, n);
            if(!flag)
            {
                n = solve(n);
                ++rear;
            }
            else if(flag == 1)
            {
                printf(" is a Happy number.\n");
                break;
            }
            else if(flag == 2)
            {
                printf(" is an Unhappy number.\n");
                break;
            }
        }
    }
}


代码二(直接打表 0.008s):

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;
const int MaxSize  = 850;
int head[MaxSize];
int solve(int n)
{
    int t, sum = 0;
    while(n)
    {
        t = n % 10;
        sum += t * t;
        n /= 10;
    }
    return sum;
}
int insert(int n)
{
    int m = n;
    while(1)
    {
        n = solve(n);
        if(n == 1)
            return 1;
        else if(head[n] || n == m)
            return 0;
        head[n] = 1;
    }
    return 0;
}
int main()
{
#ifdef test
    freopen("sample.txt", "r", stdin);
#endif
    int p, n, cct = 0;
    scanf("%d", &p);
    while(p--)
    {
        scanf("%d", &n);
        printf("Case #%d: %d", ++cct, n);
        memset(head, 0, sizeof(head));
        if(insert(n))
            printf(" is a Happy number.\n");
        else
            printf(" is an Unhappy number.\n");
    }
}

IGCT 测试原理概述 IGCT(Integrated Gate-Commutated Thyristor,集成门极换流晶闸管)是一种高功率半导体器件,基于门极可关断晶闸管(GTO)的原理,但通过集成门极驱动单元和优化结构(如硬驱动技术),实现了更低的导通损耗、更快的开关速度和更高的可靠性。它适用于中高压大功率应用,如高压直流输电(HVDC)、中压变频器和模块化多电平变换器(MMC)。IGCT 的结构为四层 p-n-p-n 器件,门极电流控制导通和关断,阳极电流在关断时快速换流至门极,通常在 1 μs 内完成。 阅读详情

相关推荐

UnicodeEncodeError: ‘ascii‘ codec can‘t encode characters in position 0-4: ordinal not in range(128)

python3报错处理:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-4: ordinal not in range(128) 一、报错场景 // 报错代码 Traceback (most recent call last): File "test_autoo3_2.py", line 214, in <module> main() File "test_autoo3_2.py",

浪里小飞侠的博客 4201

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 in

D_Double's Journey 1219

从零部署奥比中光Gemini335L:Python SDK编译与手眼标定环境实战

本文提供了一份从零部署奥比中光Gemini 335L深度相机的完整实战指南。详细介绍了驱动安装、Python SDK编译、CMake配置及虚拟环境搭建等关键步骤,并重点解析了如何通过编译生成Python模块,最终实现深度图与点云数据获取,为后续的机械臂手眼标定工作奠定坚实基础。

weixin_29031161的博客 399

UVa OJ 10591-Happy Number

<br />模拟:<br />{ Author:wzx961008 Problem:UVa 10591-Happy Number Verdict:Accepted Language:PASCAL Run Time:0.016s Submission Date:2011-03-05 06:32:08 } var n,i,num:longint; f:array[1..1000]of boolean; function happy(num:longint):boolean

Chiwin 580

深入解析:el-table合并单元格

  在企业级应用开发中,我们经常需要处理复杂的数据展示需求。在构建复杂的数据展示后台时,我们常常需要将表格中具有相同属性的行进行视觉上的合并,以提升信息的聚合度和可读性。在 Element Plus 的 el-table 组件中,span-method 属性是实现单元格合并的核心机制,允许开发者自定义单元格的合并逻辑,开发者可以通过实现自定义合并策略来满足各种负责需求。  合并行的本质是通过计算相邻行数据的相似性,确定满足合并的单元格范围,然后通过 DOM 操作将多个单元格合并为一个。这种操作在前端数据可视

独泪了无痕 229

Unity游戏开发客户端C#基础

初级Unity客户端开发工程师 面经 之 C#篇

Sea3752的博客 2万+

UVA - 10591 - Happy Number (STL)

UVA - 10591 Happy Number Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description   Problem C Happy Number

Never say never 921

springcloud教程 -- 1.快速搭建入门级demo,看这一篇就够了

废话不多讲,跟紧我,开启你的SpringCloud初体验 首先回顾微服务的基本组成: [图片 here] 生产者:提供服务 消费者:消费服务 服务注册/发现中心:服务注册,发现,监控 所以,首先明白springcloud微服务的架构基础:生产者(client),消费者(client),服务注册/发现中心(server)

叶巨岩的博客 8万+

UVA - 10591 Happy Number

题目:#include #include #include #include #define ll long long int using namespace std; int vis[100000000]; ll bit(int u) { ll sum = 0; while (u) { int v = u %10; sum += v * v; u = u / 1

七夜 539

CryoEM - 冷冻电镜 基于深度学习的 从头重构(Ab-initio Reconstruction) 开源项目 教程

冷冻电镜(CryoEM) 是一种成像方式,为蛋白质和其他生命基本组成部分的动态,提供独特的观察方式。从数百万噪声和随机取向的二维投影中,以计算效率高的方式,联合估计生物大分子的姿态、三维结构和构象异质性。

AGI 1557

2018安徽省青少年信息学奥林匹克(AHOI)初中组

12 种合法的排列分别为:1, 3, 2, 4, 2, 3, 1, 4, 3, 1, 4, 2, 3, 2, 4, 1, 1, 3, 4, 2, 2, 3,4,1,1,4,2,3, 2,4,1,3, 4,1,3,2, 4,2,3,1, 1,4,3,2, 2,4,3,1。分为 2 组,一组的队员实力值是{4, 5, 2, 3},一组是{-4, -3, -5},其中最小的组人数为 3,可以发现没有比 3 更优的分法了。[0, 1, 1, 2]同样不是合法的分组方案,因为出现了两个实力值为 1 的选手。

weixin_42358969的博客 1143

uva10591 Happy Number

暴简单的一道题,不过我还是wa了一次,提交的太急了。。 也用不上哈希,除了

cgjiayou 430

万字长文:YOLOv5+RK3588 NPU多线程加速全攻略——从理论到部署的终极优化

RK3588 NPU多线程加速优化教程摘要 本教程深入讲解了如何利用线程池技术优化RK3588 NPU上的YOLO目标检测模型推理性能。RK3588内置3个NPU核心,理论算力达6TOPS,为多线程优化提供了硬件基础。教程详细解析了线程池的核心组件(任务队列、工作线程、同步机制)和实现架构,包括类设计、初始化流程、工作线程逻辑(任务获取、NPU推理、结果存储三个阶段)以及任务提交/获取机制。通过预分配模型实例、线程绑定、流量控制等优化手段,实现了从单线程28FPS到多线程120FPS的显著性能提升,为边缘A

LXY_LOVE36的博客 1101

UVA 10591 Happy Number

Problem C Happy Number Time Limit 1 Second   Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the sq

Remilia's 1138

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

uva 10591- Happy Number

#include #include #include #include #include using namespace std; int vis[1000]; int solve(int n) { int sum = 0; while(n>0) { sum += (n%10)*(n%10); n /= 10; }

u010652938的专栏 580

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

UVA10591 Happy Number【数学】

Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the squares of the digits of S1 be represented by S2 and so on. If Si = 1 for so...

海岛Blog 847

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

UVA 10591 - Happy Number

题目大意: jietisi #include int main() { int t, count = 1; scanf("%d", &t); while (t--) { int num, arr[800] = {0}; scanf("%d", &num); int n = num, sum; while (1) { sum = 0; while (n

Bingo 2045

UVA - 10591 Happy Number

Happy Number UVA - 10591 Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the squares of the digits of S1 be represented by S2...

weixin_34296641的博客 91

uva 10591 Happy Number

uva 10591 Happy Number

@fei 521

uva-10591 - Happy Number

这道题目的题意很简单,数据范围是1到10的9次方,看着有点大,实际上最大情况下数字是:999999999,把他们每一位的平方和为81*9=729;#include #include #include #include using namespace std; int map[1000]; int sure(int x) { int p=x; int visit[1000];

青竹梦 1114

UVA - 10591 - Happy Number

看了不下8遍题目,终于看懂了,这一堆的of把人都搞昏了。注意digit与integer的区别,这里的digit指的是整数的每一位数。若在构成循环之前,有一个Si(i>=1)的值为1,则这个数是快乐的。 7->49->97( 97=4*4+9*9)->130 (1*1+3*3+0)->10->1 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4中

749

uva 10591 - Happy Number

Problem C Happy Number Time Limit 1 Second   Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the s

uva code 1123

UVA - 10591 Happy Number(hash)

Problem C Happy Number Time Limit 1 Second   Let the sum of the square of the digits of a positive integer S0 be represented by S1. In a similar way, let the sum of the sq

小G的ACM之路 934
上一篇: 几种选择全排列实现的效率
下一篇: UVa 10391 - Compound Words
GooMaple
博客等级 码龄14年 79粉丝 197原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值