202. Happy Number(easy)

本文介绍了一种用于判断数字是否为快乐数的算法。快乐数是一种特殊的正整数,其过程始于任意正整数,通过重复将该数字替换为其各位数字平方和的过程,最终要么停留在1,要么陷入一个不包含1的无限循环。文章提供了C++和Java两种语言的实现代码。

Easy

952248FavoriteShare

Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 

Input: 19

Output: true

Explanation:

12 + 92 = 82

82 + 22 = 68

62 + 82 = 100

12 + 02 + 02 = 1

 

C++:

/*
 * @Author: SourDumplings
 * @Date: 2019-08-09 11:24:05
 * @Link: https://github.com/SourDumplings/
 * @Email: changzheng300@foxmail.com
 * @Description: https://leetcode.com/problems/happy-number/
 */

class Solution
{
public:
    bool isHappy(int n)
    {
        int checkNum = 100;
        while (checkNum--)
        {
            if (check(n))
            {
                return true;
            }
        }
        return false;
    }

    bool check(int &n)
    {
        string s = to_string(n);
        int sum = 0;
        for (char d : s)
        {
            int dd = d - '0';
            sum += dd * dd;
        }
        if ((n = sum) == 1)
        {
            return true;
        }
        return false;
    }
};

Java:

/*
 * @Author: SourDumplings
 * @Date: 2019-08-09 11:32:24
 * @Link: https://github.com/SourDumplings/
 * @Email: changzheng300@foxmail.com
 * @Description: https://leetcode.com/problems/happy-number/
 */

class Solution
{
    public boolean isHappy(int n)
    {
        int repeatNum = 100;
        while (repeatNum-- != 0)
        {
            if ((n = doCheck(n)) == 1)
                return true;

        }
        return false;
    }

    private int doCheck(int n)
    {
        String num = new Integer(n).toString();
        int l = num.length();
        int res = 0;
        for (int i = 0; i < l; i++)
        {
            Character d = num.charAt(i);
            int digit = Integer.parseInt(d.toString());
            res += digit * digit;
        }
        return res;
    }
}

 

(Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table: Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indi- cated in the above table to develop the seven-letter word “NUMBERS.” Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtaposi- tions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be happy to know that the number corresponds to “PETCARE.” Write a program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.
06-09
标题SpringBoot与微信小程序融合的私房菜定制上门服务系统研究AI更换标题第1章引言介绍私房菜定制上门服务的研究背景、意义、现状以及论文的方法和创新点。1.1研究背景与意义阐述私房菜定制上门服务在市场中的需求及重要性。1.2国内外研究现状分析国内外在私房菜定制服务及线上平台方面的研究进展。1.3研究方法以及创新点概述本文采用SpringBoot与微信小程序结合的研究方法及创新点。第2章相关理论介绍SpringBoot框架、微信小程序开发及私房菜定制服务相关理论。2.1SpringBoot框架概述阐述SpringBoot框架的特点、优势及其在开发中的应用。2.2微信小程序开发基础介绍微信小程序的开发流程、组件及API等基础知识。2.3私房菜定制服务理论分析私房菜定制服务的概念、特点及用户需求。第3章系统设计详细介绍基于SpringBoot与微信小程序的私房菜定制上门服务系统的设计方案。3.1系统架构设计给出系统的整体架构、模块划分及数据流程。3.2数据库设计阐述数据库的设计思路、表结构及关系。3.3界面设计介绍微信小程序的用户界面设计,包括交互流程与视觉呈现。第4章系统实现阐述私房菜定制上门服务系统的实现过程及关键技术。4.1SpringBoot后端实现介绍后端服务的实现过程,包括接口设计、数据处理等。4.2微信小程序前端实现阐述前端页面的实现过程,包括页面布局、交互逻辑等。4.3系统集成与测试前后端集成过程及系统测试的方法和结果。第5章实验与分析对私房菜定制上门服务系统进行实验验证和性能分析。5.1实验环境与数据集介绍实验所采用的环境、数据集及评估指标。5.2实验方法与步骤给出实验的具体方法和步骤,包括用户测试、性能测试等。5.3实验结果与分析对实验结果进行详细分析,包括用户满意度、系统性能等。第6章结论与展望总结本文的研究成果,并展望未来的研究方向。6.1研究结论概
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值