2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)

Posted acgoto

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)相关的知识,希望对你有一定的参考价值。

题目描述

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.".
示例1

输入

3
6
8
28

输出

Case 1: 6 = 1 + 2 + 3
Case 2: Not perfect.
Case 3: 28 = 1 + 2 + 4 + 7 + 14
解题思路:测试数据不大,暴力水过!
AC代码一:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+5;
 4 int t,n,sum,k,s[maxn];
 5 int main(){
 6     while(~scanf("%d",&t)){
 7         for(int i=1;i<=t;++i){
 8             sum=k=0;
 9             scanf("%d",&n);
10             for(int j=1;j<=n/2;++j)
11                 if(n%j==0){sum+=j;s[k++]=j;}
12             printf("Case %d: ",i);
13             if(sum!=n)printf("Not perfect.
");
14             else{
15                 printf("%d = %d",n,s[0]);
16                 for(int j=1;j<k;++j)
17                     printf(" + %d",s[j]);
18                 printf("
");
19             }
20         }
21     }
22     return 0;
23 }

AC代码二:也可以先打表找出10^5内所有完数,一共就4个完数:6,28,496,8128,同样水过!

 1 #include<cstdio>
 2 int t,n;
 3 int main(){
 4     while(~scanf("%d",&t)){
 5         for(int i=1;i<=t;++i){
 6             scanf("%d",&n);
 7             printf("Case %d: ",i);
 8             if(n==6)printf("6 = 1 + 2 + 3
");
 9             else if(n==28)printf("28 = 1 + 2 + 4 + 7 + 14
");
10             else if(n==496)printf("496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
");
11             else if(n==8128)printf("8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064
");
12             else printf("Not perfect.
");
13         }
14     }
15     return 0;
16 }

 








以上是关于2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)的主要内容,如果未能解决你的问题,请参考以下文章

2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)

2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)

2018 ACM 国际大学生程序设计竞赛上海大都会 F - Color it (扫描线)

2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 A Fruit Ninja

哪里有第36届ACM国际大学生程序设计竞赛亚洲区预赛北京赛区现场赛视频?

第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(上海),签到题5题