前言
Happy Number
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210411191400777.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21pc3RyZWJq,size_16,color_FFFFFF,t_70) 输入描述: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210411191446511.png) 输出描述: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210411191509612.png) 链接:https://ac.nowcoder.com/acm/problem/220119 来源:牛客网
示例1
输入
19
输出
HAPPY
示例2
输入
5
输出
UNHAPPY
代码如下(示例):
这个题大概的意思就是经过题目表述一直计算,算到重复数字的时候是不是1,如果是以就输出HAPPY否则输出UNHAPPY,提前存一下前十个数能不呢算出1,
#include<iostream>
#include<string>
#include<map>
using namespace std;
bool a[10] = { 0,1,0,0,0,0,0,1,0,0};
bool flag;
queue<int> q;
void dfs(int x) {
if (flag) return;
if (x >= 0 && x <= 9) {
flag = true;
if (a[x]) cout << "HAPPY" << endl;
else cout << "UNHAPPY" << endl;
}
int m = x,sum=0;
while (m) {
sum += (m % 10)*(m%10);
m = m / 10;
}
dfs(sum);
}
int main() {
int n;
cin >> n;
dfs(n);
return 0;
}