当前位置:网站首页>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");
}
边栏推荐
- LabVIEW用了多线程,程序是不是会跑的更快些
- Daily model series: July 11, 2022
- Redis
- SharePoint接入简要笔记
- shell-笔记
- SSM implementation of one-to-one query detailed tutorial (1)
- 为什么别人游戏里的特效都非常柔和
- Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
- 【论文笔记】融合多传感器数据的抓取机械臂末端定位研究
- 【C语言-自定义类型】还能这样整?
猜你喜欢
一文,教你实现单点登录
How to build your own cloud database in docker
分库分表
【C语言-自定义类型】还能这样整?
VMware中扩展硬盘
【AXI】解读AXI协议的额外信号(QOS信号,REGION信号,与USER信号)
Scratch reverse order output electronic society graphical programming scratch grade examination level 4 true questions and answers analysis June 2022
【排错必看】Windows系统安装mysql时常见问题及解决方法
gradle入门笔记
MySql数据是如何存储在磁盘上存储的?
随机推荐
Cbcgpedit control used by BCG
静态路由!!静态路由!!静态路由!!
Left connection query of Android database
[Hongke] lidar safety system: making the world safer
Hausdorff loss function for semantic segmentation
案例分享 | 基于Linkis+DSS构建合合信息一站式数据开发平台
力扣(LeetCode)197. 上升的温度(2022.07.16)
es概念模型与基本故障
【论文笔记】融合多传感器数据的抓取机械臂末端定位研究
Example description of alternative writing of instanceof
Encapsulation API, request interception, response interception, authentication timeliness
LabVIEW用了多线程,程序是不是会跑的更快些
Programming in the novel [serial 13] the moon bends in the yuan universe
Shell notes
分库分表
Distributed transaction reliable message final consistency solution
Scope and lifecycle of beans
Solve interface cross domain problems and node operation MySQL
cut,sort,uniq,xargs
AuthTalk 第一期预告 | 全面拆解多租户解决方案