【问题标题】:Basic loops in C - How to find the sum of factorsC中的基本循环 - 如何找到因子的总和
【发布时间】:2016-04-11 14:15:08
【问题描述】:

我的 C 课程大约有 4 周的时间,并且正在开发一个基本上会输出以下内容的程序 -

 ./perfect
Enter number: 6
The factors of 6 are:
1
2
3
6
Sum of factors = 12
6 is a perfect number

./perfect
Enter number: 1001
The factors of 1001 are:
1
7
11
13
77
91
143
1001
Sum of factors = 1344
1001 is not a perfect number

到目前为止我的工作 -

// Testing if a number is perfect

#include <stdio.h>

int main (void) {

//Define Variables
    int input, sum;
    int n;

//Obtain input
    printf("Enter number: ");
    scanf("%d", &input);

//Print factors
    printf("The factors of %d are:\n", input);

    n = 1;
    while (n <= input) {
        if (input % n == 0) {
            printf("%d\n", n);

        }

        n = n + 1;

    }
    //Sum of factors
    //printf("Sum of factors = %d", sum);

    //Is it a perfect number?
    if (sum - input == input) {
        printf("%d is a perfect number", input);
    } else if (sum - input == !input) {
        printf("%d is not a perfect number", input);

    }

    return 0;
}

所以我已经完成了第一部分和最后一部分(我认为)。这只是总结了我正在努力解决的因素。

如何将所有因素加在一起?它应该是第一个 while 循环的一部分,还是单独放置?

任何帮助将不胜感激!

谢谢!

【问题讨论】:

  • 你的意思是if (sum - input == input) {if ((sum - input) == input) {
  • if (sum - input == input) { 应该是if (sum == input) { 并且else 子句不需要任何if

标签: c while-loop sum logic


【解决方案1】:

试试这个。

#include <stdio.h>

int main (void) {

//Define Variables
int input, sum;
int n;

//Obtain input
printf("Enter number: ");
scanf("%d", &input);

//Print factors
printf("The factors of %d are:\n", input);

for (n=1, sum=0; n <= input; n++) {
    if (input % n == 0) {
        printf("%d\n", n);
        sum += n;
    }
}
//Sum of factors
//printf("Sum of factors = %d", sum);

//Is it a perfect number?
if ((sum - input) == input) {
    printf("%d is a perfect number", input);
} else {
    printf("%d is not a perfect number", input);
}

return 0;
}

【讨论】:

    【解决方案2】:

    是的。我会在顶部初始化 sum=0 并添加 sum += n;到第一个 while 循环。那应该为你做。

    【讨论】:

      【解决方案3】:

      您可以在第一个循环中执行此操作。例如,

          factorsSum = 0;
          while (n <= input) {
          if (input % n == 0) {
              printf("%d\n", n);
              factorsSum += n;
          }
      

      希望这会有所帮助 =)

      【讨论】:

      • 感谢您的回答,我已经添加了这个并且得到了一个奇怪的输出......所以我已经将第一个 while 循环更新为 ` n = 1;总和 = 0; while (n 输入数字:6 6 的因子是:1 因子之和 = 12 因子之和 = 33 因子之和 = 66 因子之和 = 126 是一个完美的数字 不确定这里发生了什么...
      • 看起来像你的 printf("Sum of Factors = %d", sum);实际上是在一个循环中,你检查过吗?
      • 谢谢@Luanf,它帮助我克服了我遇到的障碍。
      【解决方案4】:
      var sum =0;
      
          for (var i=1, sum=0; i <= input/2; i++) {
                 if (input % i == 0) {
                     printf("%d\n", n);
                     sum += i;
                 }    
      }    
      //Sum of factors    
      //printf("Sum of factors = %d", sum);
      

      此代码非常适合您。 循环计数更少 更多信息see here

      【讨论】:

      • 如果你想减少循环次数,循环到 sqrt(input) 就足够了。
      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2020-03-19
      • 2014-07-09
      • 2020-07-16
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多