-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappynumber.cpp
50 lines (47 loc) · 993 Bytes
/
happynumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
string ishappy(int num)
{
int sum;
sum = 0;
vector<int> numlist;
while (1){
sum += (num % 10) * (num % 10);
num = num / 10;
if (num == 0){
if (sum == 1){
return "HAPPY";
}
for (int k = 0; k < numlist.size(); k++){
if (sum == numlist[k]){
return "UNHAPPY";
}
}
numlist.push_back(sum);
num = sum;
sum = 0;
}
}
}
int main()
{
ifstream inStream;
int numTestCases;
inStream.open("input.txt");
if (inStream.fail())
{
cerr << "Input file opening failed.\n";
exit(1);
}
inStream >> numTestCases;
for (int i = 0; i < numTestCases; i++)
{
int num;
inStream >> num;
cout << ishappy(num) <<endl;
}
inStream.close();
}