HDU - 1406 完数

## HDU - 1406 完数

Input

Output

Sample Input
2
2 5
5 7
Sample Output
0
1

t*(t-1)/2

``````#include<iostream>
using namespace std;

int main()
{
int a, b;
cin >> a;
while (cin >> a >> b)cout << ((a - 6)*(b - 6) <= 0) + ((a - 28)*(b - 28) <= 0) + ((a - 496)*(b - 496) <= 0) + ((a - 8128)*(b - 8128) <= 0) << endl;
return 0;
}``````

A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).

``````#include<iostream>
using namespace std;

int f(long long n)//求平方和
{
if (n == 0)return 0;
return f(n / 10) + (n % 10)*(n % 10);
}

int main()
{
for(int i=1;i<200;i++){
int n=i;
while(n!=1&&n!=4)n=f(n);
cout<<n<<" ";
}
return 0;
}``````

## 力扣 202. 快乐数

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

``````class Solution {
public:
bool isHappy(int n) {
if (n == 4)return false;
if (n == 1)return true;
int ans = 0;
while (n)
{
ans += n % 10 * (n % 10);
n /= 10;
}
return isHappy(ans);
}
};``````