当前位置:网站首页>华为机试题——HJ56 完全数计算
华为机试题——HJ56 完全数计算
2022-04-22 16:09:00 【青云 --小凡】
描述
完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数。
它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。
例如:28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28。
输入n,请输出n以内(含n)完全数的个数。
数据范围: 1 \le n \le 5 \times 10^{5} \1≤n≤5×105
输入描述:
输入一个数字n
输出描述:
输出不超过n的完全数的个数
示例1
输入:
1000
复制输出:
3
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
int checkCompletely(int num) //检查输入的数字是否是完全数
{
std::vector<int> numVect;
for(int i = 1; i <= num; ++i)
{
if(num % i == 0) //说明可以整除
{
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.先求每个数字的所有约数
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;
}
版权声明
本文为[青云 --小凡]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41765969/article/details/124345844
边栏推荐
- Talk about data subcontracting and related tips
- Redis cache hit rate
- Grafana series (IX): introduction to Loki, an open source cloud native logging solution
- Sed in shell script
- NFT 平台安全指南
- Three dimensional model of power conductor
- One page submits two forms to the same place
- Shiro自定义缓存,扩展Shiro缓存模块,只需要配置缓存即可实现Session共享
- Hash table (II)
- Redis simple storage folder
猜你喜欢

Redis simple storage folder

Weekly recommended short video: how to reconstruct the core competitiveness of enterprises in the stock era?

Crystal Chem β-乳球蛋白 ELISA 试剂盒 II说明书

实验三 FFT及其在卷积计算和谱分析中的应用

NFT platform security guide

【ARM汇编】如何对键入数据做判断?

redis简单存储建立文件夹

Use js to complete the text and move it on the screen according to the numbers in the input box

NFT 平台安全指南

国标GB28181视频平台EasyGBS关闭了录像计划,为何还有录像文件生成?
随机推荐
实验三 FFT及其在卷积计算和谱分析中的应用
ICMP and IPv6 global unicast address dynamic allocation
NDSM, CHM, DTM, DEM, DSM, cut continuously, but clear
短链接生成器,adf.ly、shorte.st、ouo.io、adfoc.us哪个更好,有哪些区别
Immundiagnostik CRP(C反应蛋白)ELISA分析
Servlet基础
基于激光雷达点云的电力导线三维重建
Where is the dimension association table of Flink SQL? 800W dimension table
Sending non-protected broadcast
flink sql的维度关联表一般放在哪里,800W的维表
All the goods in the files backed up by Taobao assistant were put on the shelves again
三科变频器弯管机参数设置
MiniUSB管脚接口引脚定义
Shell脚本尝试
Crystal Chem 大豆 ELISA 试剂盒 II说明书
哈希表篇(二)
redis简单存储建立文件夹
牛客SQL刷题记录
solidworks两条线重合了如何选其中一条
7寸触摸屏屏幕校准参数设置