#include <iostream>
#include <map>
#include <set>
#include <stdlib.h>
using namespace std;
int k, j = 0;
int SumofSq(int num)
{ //a function that gives sum of the square of digits
int newnum = 0;
while (num > 0)
{
int digit = num % 10;
newnum += digit * digit;
num /= 10;
}
num = newnum;
}
bool Happy(int num){ //use boolean to determine if happy or not
int temp = 0;
while (num != 1)
{
if (k == j) exit(1);
num = SumofSq(num);
if (temp++ > 10)
{
return 0; //Not Happy Number
}
}
j++;
return 1; //Happy Number
}
int main()
{ //the main function that calculates the first 100000 happy #s
int count = 0;
long int num = 1, p = 0;
while (count != 100001){
int out = Happy(num);
if (out==1)
{
cout<<num<<" ";
count++;
p++;
}
if (p==10){
cout<<endl;
p=0;
}
num++;
}
return 0;
}
hw1.rar_Happy!
版权申诉
63 浏览量
2022-09-24
21:02:03
上传
评论
收藏 103KB RAR 举报
刘良运
- 粉丝: 66
- 资源: 1万+
最新资源
- 51单片机学习(1)-软件keil下载
- 历届(第1-21届)希望杯数学竞赛初一试题及答案(最新整理).doc全国数学邀请赛(264页资料)
- 水滴.psd
- TokenPocket_V2.1.2_release.apk
- Apache-druid-kafka-rce.yaml
- 基于C#的ASP.NET数据库原理及应用技术课程指导平台的开发
- 基于ROS的智能车轨迹跟踪算法的仿真与设计源码运用PID跟踪算法.zip.zip
- Bug Bounty Tip - i春秋Self-XSS变废为宝的奇思妙想
- 1991-2015年全国初中化学竞赛复赛试题汇编(212页)(24年竞赛复赛真题).docx天原杯
- Apache Flink 未授权访问+远程代码执行.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0