链接:https://ac.nowcoder.com/acm/problem/17377
来源:牛客网
题目描述
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 ≤ 105).
输出描述:
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
题目中文描述:
我们认为一个正整数是完全的,当且仅当它等于它的正因子的和小于它自己。
例如,6是完美的,因为6 = 1 + 2 + 3。
你能编写一个程序来确定给定的数字是否完美吗?
输入描述:
输入的第一行是T(1≤T≤100),表示需要解决的测试用例数量。
每个测试用例包含一个正整数N(2≤N≤105)的行。
输出描述:
对于每个测试用例,打印用例号并确定这个数字是否完美。
如果数字是完美的,则显示其正因子的和小于其本身。的顺序
求和的项必须按升序排列。如果数字不完美,打印“not perfect.”。
示例1
输入
复制
3.
6
8
28
输出
复制
例1:6 = 1 + 2 + 3
案例2:不完美。
情况3:28 = 1 + 2 + 4 + 7 + 14
题目分析:完全数问题,利用for循环寻找寻找输入数字的正因子,并且将找到的正因子存入一个数组中方便输出。
1.#include<stdio.h>
2.#include<stdlib.h>
3.#include<string.h>
4.int main(){
5. int t,n,i,a[100020],count,sum;//数组a存储正因子,count用来记录正因字个数和数组a的下标,sum用来存储所 有正因字的和;
6. while(scanf("%d",&t)!=EOF){
7. int q=1;//用来控制Case的个数
8. while(t–){
9. count=0;//初始化数组a的下标为0处
10. sum=0;//初始化为count和sum赋值为0;
11. scanf("%d",&n);
12. for(i=1;i<n;i++){
13. if(n%i0){//判断i是否为n的正因子
14. a[count]=i;//如果是正因子,将该数存入数组a;
15. count++;//使数组a的下标右移
16. sum=sum+i;//将n的正因字加起来
17. }
18. }
19. if(sumn){//判断,如果这个数是完全数,则按输入格式输出
20. printf(“Case %d: “,q);
21. q++;
22. printf(”%d = “,sum);
23. for(i=0;i<count;i++){
24. printf(”%d”,a[i]);
25. if(i!=count-1){//用来控制最后一个数后面没有多余空格和加号
26. printf(" + “);
27. }
28. }
29. printf(”\n");//换行
30. }
31. else{
32. printf("Case %d: ",q);
33. q++;
34. printf(“Not perfect.\n”);//如果不是完全数,则按该格式输出;
35. }
36. }
37. }
38. return 0;
39.}