2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
题目描述
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
-->
输入
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 ;
}
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)的更多相关文章
- 2018 ACM 国际大学生程序设计竞赛上海大都会部分题解
题目链接 2018 ACM 国际大学生程序设计竞赛上海大都会 下午午休起床被同学叫去打比赛233 然后已经过了2.5h了 先挑过得多的做了 .... A题 rand x*n 次点,每次judge一个点 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it
链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛
传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位dp)
题目链接:https://ac.nowcoder.com/acm/contest/163/J 题目大意:给定一个数N,求区间[1,N]中满足可以整除它各个数位之和的数的个数.(1 ≤ N ≤ 1012 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 A,D
A链接:https://www.nowcoder.com/acm/contest/163/A Fruit Ninja is a juicy action game enjoyed by million ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会 F - Color it (扫描线)
题意:一个N*M的矩形,每个点初始都是白色的,有Q次操作,每次操作将以(x,y)为圆心,r为半径的区域涂成黑点.求最后剩余白色点数. 分析:对每行,将Q次操作在该行的涂色视作一段区间,那么该行最后的白 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)
题目描述 In mathematics, matrix multiplication or matrix product is a binary operation that produces a m ...
随机推荐
- TKmybatis的框架介绍和原理分析及Mybatis新特性
tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效,下面来看看这个框架的基本使用,后面会对相关源码进行分析,感兴趣的同学可以看一下,挺不错的一个工具 实现对员工表的增删改查 ...
- msp430入门学习00
在TI官网上找到MSP430的程序例程.数据手册.使用指南等文件.以MSP430F169为例,步骤如下: 1)进入ti官网:http://www.ti.com.cn/ 或者http://www.ti. ...
- Test for Job 图上的动态规划(DAG)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11399 Accepted: 2697 Des ...
- SOJ 2800_三角形
真的是O不是0[看了discuss才发现.....一个大写的蠢 [题意]多个黑白三角形组成的倒三角,求白三角形组成的最大倒三角的面积 [分析]由于问的是倒三角个数,所以只需看与行数奇偶性相同的白色倒三 ...
- [bzoj3527][Zjoi2014]力_FFT
力 bzoj-3527 Zjoi-2014 题目大意:给定长度为$n$的$q$序列,定义$F_i=\sum\limits_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum\lim ...
- [bzoj3436]小K的农场_差分约束
小K的农场 bzoj-3436 题目大意:给定n个点,每个节点有一个未知权值.现在有m个限制条件,形如:点i比点j至少大c,点i比点j至多大c或点i和点j相等.问是否可以通过给所有点赋值满足所有限制条 ...
- 2017-10-03-afternoon
P100 zhx 竞赛时间:????年??月??日??:??-??:?? 题目名称 a b c 名称 a b c 输入 a.in b.in c.in 输出 a.out b.out c.out 每个测试 ...
- 动态替换logback FileAppender/RollingFileAppender 配置- Programmatically configure logback FileAppender/RollingBackAppender
一.本文实际解决的问题 如何在代码中修改logback的RollingFileAppender配置(本文代码实例为修改日志文件路径) 二.针对的场景: 本文解决的问题属于一个大需求的一部分,需求为:需 ...
- react 服务器端渲染 ssr 中 localstorage/history/window is not defined 解决方案
1.原因 ssr 会在后端执行组件的 componentWillMount 以及在它这个生命周期之前的生命周期 也就是说 ssr 阶段是不会执行 componentDidMount 方法的 当你在 c ...
- 改动Centosserver主机名称
1.暂时改动server主机名称: hostname myhost. myhost为你指定的主机名称. 2.永久性的改动主机名称 Centosserver安装好之后.默认的主机名为:localhost ...