2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
2024-09-01 20:10:12
题目描述
We consider a positive integer perfect, if and only if it is equal to the sum of its positive divisors less than itself.
For example, 6 is perfect because 6 = 1 + 2 + 3.
Could you write a program to determine if a given number is perfect or not?
For example, 6 is perfect because 6 = 1 + 2 + 3.
Could you write a program to determine if a given number is perfect or not?
输入描述:
The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
Each test case contains a line with a positive integer N (2 ≤ N ≤ 10^5).
输出描述:
For each test case, print the case number and determine whether or not the number is perfect.
If the number is perfect, display the sum of its positive divisors less than itself. The ordering of the
terms of the sum must be in ascending order. If a number is not perfect, print "Not perfect.".
输入例子:
3
6
8
28
输出例子:
Case 1: 6 = 1 + 2 + 3
Case 2: Not perfect.
Case 3: 28 = 1 + 2 + 4 + 7 + 14
-->
示例1
输入
3
6
8
28
输出
Case 1: 6 = 1 + 2 + 3
Case 2: Not perfect.
Case 3: 28 = 1 + 2 + 4 + 7 + 14
解题思路:测试数据不大,暴力水过!
AC代码一:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int t,n,sum,k,s[maxn];
int main(){
while(~scanf("%d",&t)){
for(int i=;i<=t;++i){
sum=k=;
scanf("%d",&n);
for(int j=;j<=n/;++j)
if(n%j==){sum+=j;s[k++]=j;}
printf("Case %d: ",i);
if(sum!=n)printf("Not perfect.\n");
else{
printf("%d = %d",n,s[]);
for(int j=;j<k;++j)
printf(" + %d",s[j]);
printf("\n");
}
}
}
return ;
}
AC代码二:也可以先打表找出10^5内所有的完数,一共只有4个完数为6,28,496,8128,这样也可以简单过!
#include<cstdio>
int t,n;
int main(){
while(~scanf("%d",&t)){
for(int i=;i<=t;++i){
scanf("%d",&n);
printf("Case %d: ",i);
if(n==)printf("6 = 1 + 2 + 3\n");
else if(n==)printf("28 = 1 + 2 + 4 + 7 + 14\n");
else if(n==)printf("496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248\n");
else if(n==)printf("8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064\n");
else printf("Not perfect.\n");
}
}
return ;
}
最新文章
- linux 持续构建(自动部署) 重启动tomcat或进程的脚本
- jQuery基础_2
- python socket server源码学习
- PCL可视化显示 直接加载显示pcb文件
- HDOJ 4696 Answers 乱搞
- [Java I/O] TextFile 工具类
- Java初转型-jdk安装和配置
- Poj 2499 Binary Tree(贪心)
- Run Loop简介 分类: ios技术 ios相关 2015-03-11 22:21 73人阅读 评论(0) 收藏
- CentOS 7 部署、连接 数据库mariadb
- java面试题—精选30道Java笔试题解答(一)
- JS在线生成二维码
- jqgrid 单元格引入时间datepicker控件
- Django之ORM模型
- Day06 - Ruby三种存取限制:Public,Protected,Private
- springboot~让我习惯了TDD的开发模式
- WSL(Windows Subsystem for Linux)的安装与使用及 mongodb安装
- 改写pipeline
- CENTOS7 SYSTEMD SERVICE 将自己的程序放入自动启动的系统服务
- eclipse中tomcat无法加载spring boot