C中丰富的数字

时间:2014-02-10 21:01:27

标签: c

我有一个编程任务,我真的很难过。问题是:

编写一个程序,读入一个数字列表,并为每个数字确定并打印出该数字是否丰富。

输入规格 1.第一个整数输入将是一个正整数n,表示接下来的测试用例数。 2.接下来的n个输入分别是单个正整数,每个输入用于确定数字是否丰富。

输出规格 为每个输入数字输出一行,其中以下两种格式之一: 测试用例#t:X很丰富。 测试用例#t:X不丰富。

现在这就是我所写的全部内容,我不知道如何找出数量庞大的部分。

#include <stdio.h>
#include <stdlib.h>
int main(){

int n, i, array [] = {n}; 

printf("Please enter n followed by n numbers:");
scanf(" %d", &n);

for (i=0; i<n; i++){
    scanf(" %d", &array[n]);
}

system ("pause");
return 0;
}

2 个答案:

答案 0 :(得分:1)

丰富的数字是一个非常简单的概念 - 您可以在维基百科中找到相关信息:http://en.wikipedia.org/wiki/Abundant_number

我认为如果你想要任何解决方案(不是最快),你可以明确地找到每个数字的所有除数及其总和。

答案 1 :(得分:0)

首先......

int n, i, array [] = {n}; 

不按照你的想法行事;它声明array只包含1个元素,并且该元素初始化为n的值(它本身尚未初始化,因此该值将是不确定的)。它将无法保留n个值。

根据您提供的规范,您实际上不需要存储整个输入数字列表;你应该能够阅读一个,测试丰度,然后阅读下一个,测试丰度等等。​​所以代码的结构将是这样的:

int n = 0;
...
printf("Please enter n followed by n numbers:");
scanf(" %d", &n);

for (int i = 0; i < n; i++ )
{
  int candidate;
  scanf( "%d", &candidate );                // read the next input

  printf( "%d is%s abundant\n", 
    candidate,
    !test( candidate ) ? " NOT" : "" );     // test the next input and
                                            // print NOT if the test fails
}

printf语句是一种紧凑的写作方式

if ( test( candidate ) == 0 )
  printf( "%d is NOT abundant\n", candidate );
else
  printf( "%d is abundan\n", candidate );

test函数是你需要自己解决的问题,但它的大纲将是

  1. 找到输入值的适当除数
  2. 将它们汇总在一起
  3. 将该结果与输入进行比较
  4. 返回1是结果大于输入,否则为0。