当前位置:网站首页>OJ每日一练——完数
OJ每日一练——完数
2022-04-22 08:46:00 【KJ.JK】
问题描述:
一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数,并按下面格式输出其因子:
输入
N
输出
? its factors are ? ? ?
样例
输入
1000
输出
6 its factors are 1 2 3
28 its factors are 1 2 4 7 14
496 its factors are 1 2 4 8 16 31 62 124 248
Java代码:
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner rd=new Scanner(System.in);
int a=rd.nextInt();
for(int i=2;i<=a;i++) {
int sum=0;
for(int j=1;j<=i/2;j++) {
if(i%j==0) {
sum=sum+j;
}
}
if(sum==i) {
System.out.print(i+" "+"its factors are"+" ");
for(int j=1;j<=i/2;j++) {
if(i%j==0) {
System.out.print(j+" ");
}
}
System.out.println();
}
}
}
}
C代码:
#include <stdio.h>
int main(){
int a;
scanf("%d",&a);
int sum;
for(int i=2;i<=a;i++){
sum=1;
for(int j=2;j<=i/2;j++){
if(i%j==0){
sum+=j;
}
}
if(sum==i){
printf("%d its factors are 1 ",i);
for(int j=2;j<=i/2;j++){
if(i%j==0){
printf("%d ",j);
}
}
printf("\n");
}
}
}
作者:KJ.JK
本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习
版权声明
本文为[KJ.JK]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_47384542/article/details/124333554
边栏推荐
- 学习RHCSA的第三天
- LeetCode 349. 两个数组的交集(简单、数组)day12
- 云原生微服务的下一站 微服务引擎 MSE 升级
- 一文2500字带你了解性能测试的那些基础知识
- Breakthroughs and new development opportunities of smart watches
- hyperscan --- 2 编译
- EventBridge 集成云服务实践
- Construire manuellement le tissu hyperledger V2. X réseau de production (IV) Création de canaux, cycle de vie des codes de chaîne
- Flink流处理引擎系统学习(三)
- 聊聊限流
猜你喜欢

Open3D点云处理

A 2500 word article will take you to understand the basic knowledge of performance testing

宝宝起名神器小程序源码_支持多种流量主模式

Flink流处理引擎系统学习(三)

About the fact that I was cheated of fifteen thousand when I wanted to borrow money

Raspberry pie Kali system HDMI modifies screen resolution

Leetcode0396. Rotation function (medium, iteration)

链表中倒数第k个节点(顺序查找、快慢指针)

从科普、医生培训及创新医械产品推广需求出发,「佰医绘」如何布局医学可视化SaaS服务?

如何解决项目文档管理中的复杂性?
随机推荐
Using stack to realize queue (double stack, input stack and output stack)
The penultimate node in the linked list (sequential search, speed pointer)
Flink流处理引擎系统学习(二)
MySQL-on duplicate key upate/case when语法使用
關於我想借款卻被騙了一萬五這件事
mysql用source命令导入sql出现报错 觉得应该是编码问题 不知道该怎么解决
Separate linked list (create two empty linked lists)
Halo 开源项目学习(一):项目启动
2022 R1 quick opening pressure vessel operation exercises and online simulation examination
开源,不只 coding
MyCms 自媒体 CMS 系统 v3.2.2,广告插件优化
Section I: the first step of portrait refinement - reasonable transfer
云原生微服务的下一站 微服务引擎 MSE 升级
(cvpr-2014) deep learning face representation by predicting 10000 categories
分隔链表(建两个空链表)
Manually build hyperledger fabric v2 X production network (IV) create channel and chain code life cycle
IDEA连接成功H2数据库后什么子文件都没有
手动搭建hyperledger fabric v2.x 生产网络(四)创建通道,链码的生命周期
Cmake uses the basic grammar of basic knowledge I
C 标准库 - <time.h>