Leetcode No. 202 Question-happy number

Source: Internet
Author: User
To tell the truth, if you do not look at the answer of others, this problem I also have no train of thought, do not know the loop several times, also do not know the termination condition of the cycle, then know, " 2-6" This range of numbers are not happy number so there is a termination condition,n> 6 is the termination condition, and when n jumps into this range, it terminates the loop, and finally it can determine if it is happy number.
#include <stdio.h> #include <stdlib.h>bool ishappy (int n) {while    (n>6) {int sum=0;while (n) {sum+= (n% * (n%10); n/=10;} N=sum;} return n==1;} int main () {int m;while (scanf ("%d", &m)!=eof) {bool Flag=ishappy (m); if (flag) printf ("yes\n"); elseprintf ("no\n");} return 0;}

Leetcode No. 202 Question-happy number

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.