leetcode:202. 快乐数(数学,水题,简单)

题目:

在这里插入图片描述

分析:

出现与之前重复就一定不会再出现1了,是一个循环的环了,用set记录。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
 int n;
 cin>>n;
 set<int> s;
 while(1)
 {
  if(n==1) return 1;
  if(s.count(n)==1) return 0;
  s.insert(n);
  int all=0;
  while(n!=0)
  {
   int t=n%10;
   n=n/10;
   all+=t*t;
  }
  n=all;
 }
 } 

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/107336009