题目描述

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?

输入描述:

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 ;
}

最新文章

  1. linux 持续构建(自动部署) 重启动tomcat或进程的脚本
  2. jQuery基础_2
  3. python socket server源码学习
  4. PCL可视化显示 直接加载显示pcb文件
  5. HDOJ 4696 Answers 乱搞
  6. [Java I/O] TextFile 工具类
  7. Java初转型-jdk安装和配置
  8. Poj 2499 Binary Tree(贪心)
  9. Run Loop简介 分类: ios技术 ios相关 2015-03-11 22:21 73人阅读 评论(0) 收藏
  10. CentOS 7 部署、连接 数据库mariadb
  11. java面试题—精选30道Java笔试题解答(一)
  12. JS在线生成二维码
  13. jqgrid 单元格引入时间datepicker控件
  14. Django之ORM模型
  15. Day06 - Ruby三种存取限制:Public,Protected,Private
  16. springboot~让我习惯了TDD的开发模式
  17. WSL(Windows Subsystem for Linux)的安装与使用及 mongodb安装
  18. 改写pipeline
  19. CENTOS7 SYSTEMD SERVICE 将自己的程序放入自动启动的系统服务
  20. eclipse中tomcat无法加载spring boot

热门文章

  1. 文件权限设置与http,php的关系
  2. Spring Cloud(7):Zuul自定义过滤器和接口限流
  3. JAVA 流程控制之选择语句
  4. 使用HTML5监測站点性能
  5. vux 全局使用 loading / toast / alert
  6. vSphere,ESXi,vCenter之间的关系
  7. 兔子--CheckBox与Radiobutton的差别
  8. Django打造大型企业官网(一)
  9. 华为云分布式数据库中间件DDM和开源MyCAT对比
  10. 在C++代码中调用L脚本语言