【问题标题】:Sum of primes: what is the problem in result?素数和:结果有什么问题?
【发布时间】:2021-05-09 02:13:40
【问题描述】:

代码运行良好,但为什么我在int 结果中的答案有误?

在输出中:

3
10
2 3 5 7: 17  //correct
30
2 3 5 7 11 13 17 19 23 29: 146  //incorrect
50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47: 474   //incorrect

代码如下:

#include <stdio.h>

int main() {
    int y, n, i, fact, j, result = 0;
    scanf("%d", &y);
    for (int x = 1; x <= y; x++) {
        scanf("%d", &n);
        for (i = 1; i <= n; i++) {
            fact = 0;
            for (j = 1; j <= n; j++) {
                if (i % j == 0)
                    fact++;
            }
            if (fact == 2) {
                result += i;
                printf("%d ", i);
            }
        }
        printf(": %d\n", result); //Not Getting correct answer please HELP!
    }
    return 0;
}

【问题讨论】:

    标签: c loops scope initialization primes


    【解决方案1】:

    您忘记在每次计算之前初始化result

    for(int x=1;x<=y;x++){
        scanf("%d", &n);
        result = 0; // add this for initialization
        for (i = 1; i <= n; i++) {
            /* ... */
        }
        /* ... */
    }
    

    【讨论】:

      【解决方案2】:

      变量result只初始化一次

      int y, n, i, fact, j, result = 0;
      

      所以它会累积循环中计算的值

      for(int x=1;x<=y;x++){
         //...
      }
      

      将变量result的声明移到循环体中

      for(int x=1;x<=y;x++){
         int result = 0;
         //...
      }
      

      为避免此类错误,您应该在使用它们的最小范围内声明变量。

      也是这个循环

              for (j = 1; j <= n; j++)
              {
                  if (i % j == 0)
                      fact++;
              }
      

      意义不大。通过以下方式更改循环中的条件

              for (j = 1; j <= i; j++)
              {
                  if (i % j == 0)
                      fact++;
              }
      

      用变量n 替换变量i

      此外,您应该使用无符号整数类型而不是有符号整数类型 int,因为素数是为自然数定义的。

      该程序可以看起来例如以下方式

      #include <stdio.h>
      
      int main(void) 
      {
          unsigned int n = 0;
          
          scanf( "%u", &n );
          
          while ( n-- )
          {
              unsigned int max_value = 0;
              
              scanf( "%u", &max_value );
              
              unsigned long long int sum = 0;
                  
              for ( unsigned int i = 1; i <= max_value; i++ )
              {
                  size_t count = 0;
                      
                  for ( unsigned int j = 1; j <= i; j++ )
                  {
                      if ( i % j == 0 ) ++count;  
                  }
                  
                  if ( count == 2 )
                  {
                      printf( "%u ", i );
                      sum += i;
                  }
              }
              
              printf( ": %llu\n", sum );
          }
          
          return 0;
      }
      

      如果进入

      3
      10
      20
      100
      

      那么输出将是

      2 3 5 7 : 17
      2 3 5 7 11 13 17 19 : 77
      2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 : 1060
      

      【讨论】:

        猜你喜欢
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 2013-09-22
        • 2013-01-26
        • 1970-01-01
        • 2017-05-13
        • 2021-09-30
        • 2022-08-09
        相关资源
        最近更新 更多