当前位置:网站首页>L2-029 independent happiness
L2-029 independent happiness
2022-07-19 09:04:00 【Study hard 867】
Make a square sum for each digit of a decimal number , It's called an iteration . If a decimal number can be obtained by several iterations 1, It's called happiness number .1 It's a happy number . Besides , for example 19 after 1 Iterations get 82,2 After iterations, we get 68,3 After iterations, we get 100, Finally get 1. be 19 Is the number of happiness . obviously , Iterate over a happiness number to 1 All the numbers in the process are happiness numbers , Their happiness depends on the initial number . for example 82、68、100 Happiness is attached to 19 Of . And one personal independence of conduct Of happiness , It is not attached to any other number in a limited range ; Its independence It is the number of happiness attached to it . If this number is still a prime number , Then its independence doubles . for example 19 In the interval [1, 100] Inside is a unique happiness number , Its independence is 2×4=8.
On the other hand , If one is greater than 1 After several iterations, the number of is in a dead cycle , That number is not happy . for example 29 Iterate to get 85、89、145、42、20、4、16、37、58、89、…… so 89 To 58 Formed a dead cycle , therefore 29 Not happy .
This question asks you to write a program , List all unique happiness numbers and their independence in a given range .
Input format :
Enter the two endpoints of the closed interval in the first line :1<A<B≤104.
Output format :
Give the closed interval in increasing sequence [A,B] All the unique happiness numbers and its independence . Each pair of numbers takes up one line , Between the numbers 1 Space separation .
If there is no happiness number in the range , Output in one line SAD.
sample input 1:
10 40
sample output 1:
19 8
23 6
28 3
31 4
32 3
Be careful : In the example ,10、13 It's all happiness , But they are attached to other numbers ( Such as 23、31 wait ), So don't output . Other numbers actually depend on other happiness numbers , But because those numbers are not in the given range [10, 40] Inside , So they are unique happiness numbers in a given range .
sample input 2:
110 120
sample output 2:
SAD
Code length limit
16 KB
The time limit
400 ms
Memory limit
64 MB
Ideas : use book Array to record the status of each data , If he can push it out 1, But he can also be retired from us book What's in it is -2, If he can't launch -1, We store 0, If you can launch 1 And it cannot be withdrawn within this range , Is the value of his independence , And then use map To store previous values , If it happens again, we will jump out of the cycle , Otherwise, follow his steps , During this period, if the previously identified cannot be exported 1 And back to , If you are a maverick at present, mark the previous numbers as -2, Finally, cycle here , Greater than 0 What you want is what you want .
Code :
#include <bits/stdc++.h>
using namespace std;
int book[10010];
bool sushu(int n){
if(n==1)return false;
if(n==2)return true;
int i;
for(i=2;i<=sqrt(n);i++){
if(n%i==0)return false;
}
return true;
}
int u;
void judge(int n){
if(book[n]==0)return ;
if(book[n]==-2)return ;
unordered_map<int,int>mymap;
while(1){
int s=0;
while(n){
int k=n%10;
s+=k*k;
n/=10;
}
if(book[s]==0)return ;
if(s==1){
unordered_map<int,int>::iterator it;
for(it=mymap.begin();it!=mymap.end();it++){
book[it->first]=-2;
}
book[u]=mymap.size()+1;// Count yourself
if(sushu(u))book[u]*=2;
return ;
}
if(mymap.count(s)){book[s]=0;return ;}
else {
mymap[s]=1;
n=s;
}
}
}
int main(){
int l,r;
fill(book,book+10010,-1);
scanf("%d%d",&l,&r);
int i;
bool flag=0;
for(i=l;i<=r;i++){
u=i;
judge(i);
}
for(i=l;i<=r;i++){
if(book[i]>0){
flag=1;
printf("%d %d\n",i,book[i]);
}
}
if(flag==0)printf("SAD");
system("pause");
}边栏推荐
- Programming in the novel [serial 13] the moon bends in the yuan universe
- [handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
- Distributed transaction reliable message final consistency solution
- MySQL读写分离
- Markdown(5):锚链接
- 【论文笔记】基于深度学习的视觉检测及抓取方法
- C# - this 的用法
- 2022广东省安全员A证第三批(主要负责人)练习题及模拟考试
- 也挺难的,不光我自己写的很累
- Markdown (5): anchor link
猜你喜欢
随机推荐
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
Leetcode sword finger offer II 041 Average value of sliding window: low space consumption solution
[troubleshooting] common problems and solutions when installing MySQL in Windows system
Qprocess of QT
【虹科】激光雷达安全系统:让世界更安全
How to write a string field in MySQL if its value is not empty?
Example description of alternative writing of instanceof
Cbcgpedit control used by BCG
树状数组
Project code training tutorial
封装API,request拦截,response拦截,身份验证时效性
【论文笔记】基于深度学习的视觉检测及抓取方法
Hausdorff loss function for semantic segmentation
2022t elevator repair examination question bank and answers
第七天训练
Simple case of wechat applet calling API
【C语言-自定义类型】还能这样整?
Nacos new configuration management
Latest fruit flstudio20.9 low version upgrade high version tutorial
置顶随笔分类






![[Hongke] Introduction to genicam protocol](/img/a5/91a1d24a47ea2e3265530778d6fc1f.png)


