当前位置:网站首页>Huawei machine test question hj56 complete number calculation
Huawei machine test question hj56 complete number calculation
2022-04-22 16:14:00 【Qingyun Xiaofan】
describe
Perfect number (Perfect number), Also called perfect number or perfect number , It's some special natural number .
All its real factors ( That is, a divisor other than itself ) And ( Factor function ), Exactly equal to itself .
for example :28, It has a divisor 1、2、4、7、14、28, Get rid of it 28 Outside , rest 5 Add up the numbers ,1+2+4+7+14=28.
Input n, Please export n within ( contain n) The number of perfect numbers .
Data range : 1 \le n \le 5 \times 10^{5} \1≤n≤5×105
Input description :
Enter a number n
Output description :
The output does not exceed n The number of perfect numbers of
Example 1
Input :
1000
Copy output :
3
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
int checkCompletely(int num) // Check whether the entered number is a perfect number
{
std::vector<int> numVect;
for(int i = 1; i <= num; ++i)
{
if(num % i == 0) // It means that you can divide
{
numVect.push_back(i);
numVect.push_back(num / i);
}
}
std::vector<int> tempVect;
for(int i = 0; i < numVect.size() / 2; ++i)
{
tempVect.push_back(numVect.at(i));
}
int sum = 0;
for(int ele : tempVect)
{
sum += ele;
}
if(sum == num * 2)
{
return 1;
}
else
{
return 0;
}
}
int CompletelyNumber(int number)
{
int count = 0;
//1. First find all divisors of each number
for(int i = 1; i <= number; ++i)
{
int flag = checkCompletely(i);
if(flag)
{
count++;
}
}
return count;
}
int main()
{
int n;
while(std::cin >> n)
{
int rtn = CompletelyNumber(n);
std::cout << rtn << std::endl;
}
return 0;
}
版权声明
本文为[Qingyun Xiaofan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221609199512.html
边栏推荐
- [b01lers2020]Life on Mars
- 引用拷贝,深拷贝,浅拷贝
- Servlet 的使用与对应的九个内置对象
- 343-Leetcode 反转字符串中的元音字母
- 1016 Phone Bills (25 分) 测试点1,2
- Three dimensional model of power conductor
- The number of distinct data in the query table count MySQL Oracle takes the specified number
- Performance evaluation of rust asynchronous framework
- JUC(三)ThreadLocal
- window10安装STEP7 Micro/Win V4.0 SP9,安装完后每次开机都提示Assertion Program:pniopcac.exe File
猜你喜欢

每周推薦短視頻:存量時代如何重構企業核心競爭力?

Three dimensional model of power conductor

Crystal Chem 大豆 ELISA 试剂盒 II说明书

Sign up to open QKE container engine hosting version and container ecological Conference!

Unittest-单元测试2

redis简单存储建立文件夹

以前用淘宝助理备份的文件内的商品,全部重新上架到淘宝店铺

使用EasyGBS后更换为EasyCVR,更改端口为10000,报错404是什么原因?

万元礼品奖池!玩转「Lighthouse」有奖征文来袭

Altium designer除了GND以外的Nets自动布线
随机推荐
2022-4-21 [webrtc application] source code analysis of yangrtc / metartc open source library (III)
mobaXterm连接虚拟机后不能拖拽上传文件
NFT 平台安全指南
Altium designer 生成PCB制作文件及打样流程(以嘉立创商城为例)
Redis cache hit rate
数据库自动备份报错,提示SQLServerAgent 当前未运行,因此无法将此操作通知它。
NFT platform security guide
Crystal Chem 大豆 ELISA 试剂盒 II说明书
DM8:win7+php7.3.4(phpstudy)+apache(phpstudy)+dm8.1-2-84环境搭建
国标GB28181视频平台EasyGBS关闭了录像计划,为何还有录像文件生成?
三科变频器弯管机参数设置
4.21 summary
电力导线三维模型
Use of serial port data plot serialplot
JUC(三)ThreadLocal
Shell script attempt
Wukong's private collection of "architecture" books recommended
Reference copy, deep copy, shallow copy
Do short videos from media can't earn q? Teach you 6 moves
Immundiagnostik IDK TurbiPEL测定分析