Happy Number-leetcode

Source: Internet
Author: User

Examination Questions

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 and replace the number by the Sum of the squares of its digits, and repeat the process until the number equals 1 (where it would stay), or it loops Endl essly in a cycle which does not include 1. Those numbers for which this process ends in 1 is happy numbers.

Example: is a happy number

    • 12 + 92 = 82
    • 82 + 22 = 68
    • 62 + 82 = 100
    • 12 + 02 + 02 = 1

Following function to solve the problem:

BOOL Ishappy (int n) {

}

Problem Solving Code
#include <stdio.h>BOOLIshappy (intN) {intarr[ the]; inti =0; intsum =0; intTEMP1,TEMP2;  while(true){         Do{Temp1= N/Ten; Temp2= n%Ten; N=Temp1; Sum= Sum + temp2 *Temp2; }  while(Temp1 >0); if(Sum! =1) {Arr[i]=sum; N=sum; I++; }        Else{            return true; }         for(intj =1; J < I; J + +){            if(Sum = = arr[j-1]){                return false; }} sum=0; }}intMain () {intN; scanf ("%d",&N); if(Ishappy (n)) {printf ("true\n"); }    Else{printf ("false\n"); }    return 0;}

Leetcode Judgment Result:

Basic algorithm Ideas

Split a number into a single number, then the sum of squares, and compare, if satisfied and 1, is the number of happy, if enter the cycle, then it is not happy number.

Code Comment Analysis
#include <stdio.h>BOOLIshappy (intN) {intarr[ the];//used to store and not be 1 values, just conjecture that there are so many values    inti =0;//number of records and    intsum =0;//and Values    intTemp1, Temp2;//Temp1 is the value, Temp2 is the remainder     while(true){         Do{Temp1= N/Ten; Temp2= n%Ten; N=Temp1; Sum= sum + temp2 * TEMP2;//and the} while(Temp1 >0);//Single decomposition Complete        if(Sum! =1){//if it is not 1, assign the value to arrArr[i] =sum; N=sum; I++; }        Else{            return true; }         for(intj =1; J < I; J + +) {//loop matches the value, and if the value in Arr is the same, exit because a loop appears            if(sum = = Arr[j-1]){                return false; }} sum=0;//the second summation, must first clear 0    }}intMain () {intN; scanf ("%d", &N); if(Ishappy (n)) {printf ("true\n"); }    Else{printf ("false\n"); }    return 0;}

Optimization

After data query, for the number of happiness, there are the following rules:

Not the number of happy numbers is called the number of unhappy, and the sum of the squares of all the unhappy numbers is counted, and the last one will go into the 4→16→37→58→89→145→42→20→4 cycle.

Optimized code
#include <stdio.h>BOOLIshappy (intN) {intsum =0; intTemp1, Temp2;  while(true){         Do{Temp1= N/Ten; Temp2= n%Ten; N=Temp1; Sum= Sum + temp2 *Temp2; }  while(Temp1 >0); if(Sum = =1){            return true; }        Else{            if(Sum = =4|| sum = = -|| sum = = -|| sum = =Panax Notoginseng|| sum = = the|| sum = = -|| sum = = the|| sum = =145){                return false; }} N=sum; Sum=0; }}intMain () {intN; scanf ("%d", &N); if(Ishappy (n)) {printf ("true\n"); }    Else{printf ("false\n"); }    return 0;}

Leetcode Judgment Result:

Happy Number-leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.