Maverick happiness (25 points)

Maverick happiness (25 points)

Doing a sum of squares of the digits of a decimal number is called an iteration. If a decimal number can get 1 through several iterations, it is called a happy number. 1 is a happy number. In addition, for example, 19 gets 82 after 1 iteration, 68 after 2 iterations, 100 after 3 iterations, and 1 at the end. Then 19 is the happiness number. Obviously, the numbers passed during the iteration of a happiness number to 1 are all happiness numbers, and their happiness is dependent on the initial number. For example, the happiness of 82, 68, and 100 is dependent on 19. And a maverick happiness number is not dependent on any other numbers in a limited interval; its independence is the number of happiness numbers attached to it. If this number is still a prime number, its independence is doubled. For example, 19 is a maverick happiness number in the interval [1, 100], and its independence is 2×4=8.

On the other hand, if a number greater than 1 enters an endless loop after several iterations, then the number is not happy. For example, 29 iterations get 85, 89, 145, 42, 20, 4, 16, 37, 58, 89,... It can be seen that 89 to 58 form an endless loop, so 29 is not happy.

This question requires you to write a program to list all the maverick happiness numbers and their independence in a given interval.

Input format:
Input the two end points of the closed interval given in the first line: 1<A<B≤10
​4
​​.

Output format:
List all maverick happiness numbers and their independence in a given closed interval [A, B] in increasing order. Each pair of numbers occupies a line, and the numbers are separated by 1 space.

If there is no happiness number in the interval, SAD is output in one line.

#include <bits/stdc++.h>
using namespace std;
int fa[10001];
int get(int x)//求各位平方和
{
    
    
    int sum=0;
    while(x)
    {
    
    
        sum+=(x%10)*(x%10);
        x=x/10;
    }
    return sum;
}
int sushu(int x)//素数
{
    
    
    for(int i=2;i<=sqrt(x);i++)
    {
    
    
         if(x%i==0)
         {
    
    
             return 0;
         }

    }
    return 1;
}
int findd(int x)//判断是否是特立独行,跟是否是1
{
    
    
    int ans=x;
    int i=0;
    while(fa[ans]!=ans)
    {
    
    
        ans=fa[ans];
        i++;
        if(i>=10000)
            return 0;
    }
    return ans;
}
int fun(int x,int n,int m)//是否有依赖
{
    
    
    for(int i=n;i<=m;i++)
    {
    
    
        if(fa[i]==x)
            return 0;
    }
    return 1;
}
int main()
{
    
    
   int n,m;
   cin>>n>>m;
   int cnt=0;
   for(int i=1;i<=10000;i++)
    fa[i]=get(i);
   for(int i=n;i<=m;i++)
   {
    
    
       if(fun(i,n,m)&&findd(i)==1)
       {
    
    
           cout<<i<<' ';
           int cnt1=1,x=i;
           while(get(x)!=1)
           {
    
    
               cnt1++;
               x=get(x);
           }
           if(sushu(i))
            cnt1*=2;
           cout<<cnt1<<endl;
           cnt++;
       }
   }
   if(cnt==0)
    cout<<"SAD"<<endl;
}

Guess you like

Origin blog.csdn.net/a675891/article/details/115270609