当前位置:网站首页>C语言入门实战(9):完数判断
C语言入门实战(9):完数判断
2022-07-20 02:24:00 【liberg】
这是《C语言入门实战》系列的第9篇。
上一篇:C语言入门实战(8):switch-case计算下一天的年月日(平年/闰年计算)
题目
输入任意一个正整数n,判断n是否为一个完数。
完数是指一个数恰好等于它的因子之和。例如6是一个完数,它等于它的因子 1、2、3的和。类似地28等于其因子1、2、4、7、14的和。
因子不包含自身,且各个因子不能重复。
请编程实现。
要求
输入输出格式示例1:
输入:28<回车>
输出:yes
输入输出格式示例2:
输入:76<回车>
输出:no
参考代码
#include <stdio.h>
#include <stdlib.h>
/** * 输入任意一个正整数n,判断n是否为一个完数。 * 完数是指一个数恰好等于它的因子之和, * 例如6是一个完数,它等于它的因子 1、2、3的和。 */
int main(int argc, char *argv[]) {
int n;
scanf("%d", &n);
char* reply = "no";
// 题目说明了,因子不包含自身
// 所以,1不是完数,只需要处理>1的情况
if(n > 1) {
// 1作为n(>=2)的因子
int factorSum = 1;
// 计算>=2的其余因子,并累加之
int i;
for(i=2;i*i<=n;i++) {
if(n%i == 0) {
// 找到一个因子i
factorSum += i;
// 另一个因子
int other = n/i;
if(other != i) {
factorSum += other;
}
}
}
// 判断因子的和是否等于n本身
if(factorSum == n) {
reply = "yes";
}
}
// 打印输出答案
printf("%s", reply);
return 0;
}
代码复盘
头文件stdio.h
中声明的scanf()
和printf()
是标准库中用于处理标准输入和输出的库函数。
边栏推荐
- JSP custom tags (an article learned that each line of code has comments)
- LeetCode.1217. 玩筹码____简单贪心
- 网络安全—综合渗透测试-CVE-2018-10933-libssh漏洞分析
- Tutorial on principles and applications of database system (023) -- Summary of various methods of creating data tables in MySQL
- 力扣刷题记录2-----35.搜索插入位置
- Network security - comprehensive penetration test -cve-2019-15107-webmin remote code execution
- 力扣刷题目录
- xtrabackup实现mysql:全量备份+增量备份
- In the last interview, I knelt on redis, finished the internal redis documents given by cousin Ali, and finally entered the big factory
- Handbrake installation problem: prompt to install frameworks NET
猜你喜欢
Understand the MySQL architecture design in one article, and don't worry about the interviewer asking too much
【03】通过你的CPU主频,我们来谈谈“性能”究竟是什么?
JSP custom tags (an article learned that each line of code has comments)
Network security - comprehensive penetration test -cve-2019-15107-webmin remote code execution
【LeetCode-中等】34. 在排序数组中查找元素的第一个和最后一个位置 - 数组双指针
infraversion和superaversion
Technical analysis premint security events, how to avoid attacks?
力扣刷题记录2-----35.搜索插入位置
Kubernetes Kube scheduler
消息队列——消息队列入门
随机推荐
ShardingSphere-proxy 搭配 MogDB/openGauss 实现分布式数据库
动态内存管理
网络安全—综合渗透测试-CVE-2018-10933-libssh维持访问
中职网络安全技能大赛P100-Web渗透测试
力扣刷题记录1-----704.二分查找
LeetCode.745. 前缀和后缀搜索____双字典树+双指针
LeetCode.1252. 奇数值单元格的数目___简单模拟 / 行列数组+排列组合
力扣刷题记录4-----69. x 的平方根
W806 development board experience
LeetCode_ 78_ subset
LeetCode.302 场周赛___02_6164. 数位和相等数对的最大和___哈希 + 增强预处理 + 自定义优先队列
中职网络安全—2022年国赛逆向PE Reverse解题思路
The development of the meta universe is also a process of gradually becoming people's production and lifestyle
网络安全—综合渗透测试-CVE-2019-7238-Nexus远程代码执行
Network security in Secondary Vocational Schools - the thinking of reverse PE reverse problem solving in 2022 National Games
Luogu p1678 troubled college entrance examination volunteers
Understand the MySQL architecture design in one article, and don't worry about the interviewer asking too much
数据库系统原理与应用教程(031)—— MySQL 的数据完整性(四):定义外键(FOREIGN KEY)
Tutorial on principles and applications of database system (023) -- Summary of various methods of creating data tables in MySQL
Infraversion and superversion