【问题标题】:C Program That Counts Even Integers from a Set of 5 Integers [closed]从一组5个整数中计算偶数的C程序[关闭]
【发布时间】:2017-02-11 06:17:36
【问题描述】:

我正在尝试编写一个 C 程序,其中用户输入五个不同的整数,并从这五个整数的输入中确定偶数的数量。这是我当前的代码:

#include <stdio.h> 

int main()
{
    int n1, n2, n3, n4, n5, sum;

//user enters 5 integers

    printf("Enter five different positive integers: \n");

//program scans for user input

    scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5);

//if statement to determine what integers are even

    if(((n1,n2,n3,n4,n5)%2)==0)

//sum of even integers

        sum = n1 + n2 + n3 + n4 + n5;

//program prints sum of even integers

        printf("There are %d even integers in the input. \n", sum); 

//program prints if there are no even integers for the inputs

    else

        printf("There are no even integers in the input. \n");

    return(0);
}

有什么想法吗?

【问题讨论】:

  • 尝试使用数组和循环。在所有情况下。

标签: c if-statement count sum int


【解决方案1】:

您的目标没有按需要明确说明:

  • 您要对所有偶数求和而忽略输入的奇数吗?

  • 您是否希望所有整数都是偶数并在不包含任何整数时拒绝输入?

无论哪种方式,您的程序都会因多种原因而失败:

  • if(((n1,n2,n3,n4,n5)%2)==0) 没有任何用处:它只检查最后一个整数是否为偶数。您可以检查所有整数是否与此偶数

     if ((n1 | n2 | n3 | n4 | n5) % 2) == 0)
    
  • 您没有使用大括号对 if 正文中的指令进行分组。与 Python 不同,缩进在 C 中不起作用,您必须在多条指令周围使用大括号(})在ifelsewhilefor 等之后形成一个块。

这是忽略奇数的代码的修改版本:

#include <stdio.h> 

int main(void) {
    int n1, n2, n3, n4, n5, sum, count;

    // user enters 5 integers
    printf("Enter five different positive integers:\n");

    // program scans for user input

    if (scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5) != 5) {
        printf("Invalid input\n");
        return 1;
    }

    // for each integer, add it if it is even

    count = 0;
    sum = 0;

    if (n1 % 2 == 0) {
        sum += n1;
        count++;
    }
    if (n2 % 2 == 0) {
        sum += n2;
        count++;
    }
    if (n3 % 2 == 0) {
        sum += n3;
        count++;
    }
    if (n4 % 2 == 0) {
        sum += n4;
        count++;
    }
    if (n5 % 2 == 0) {
        sum += n5;
        count++;
    }

    if (count > 0) {
        printf("There are %d even integers in the input, their sum is %d.\n",
               count, sum); 
    } else {
        //program prints if there are no even integers for the inputs
        printf("There are no even integers in the input.\n");
    }
    return 0;
}

使用一些更高级的 C 知识,您可以将代码简化为:

#include <stdio.h> 

int main(void) {
    int n1, n2, n3, n4, n5, sum, count;

    printf("Enter five different positive integers:\n");
    if (scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5) != 5) {
        printf("Invalid input\n");
        return 1;
    }

    // use the low order bit to test oddness
    count = 5 - ((n1 & 1) + (n2 & 1) + (n3 & 1) + (n4 & 1) + (n5 & 1));
    sum = n1 * !(n1 & 1) + n2 * !(n2 & 1) + n3 * !(n3 & 1) +
          n4 * !(n4 & 1) + n4 * !(n4 & 1);

    if (count > 0) {
        printf("There are %d even integers in the input, their sum is %d.\n",
               count, sum); 
    } else {
        printf("There are no even integers in the input.\n");
    }
    return 0;
}

但它实际上更复杂,可读性更差,并且没有被证明更有效。

真正的改进是使用循环:

#include <stdio.h> 

int main(void) {
    int i, n, sum = 0, count = 0;

    printf("Enter five different positive integers:\n");
    for (i = 0; i < 5; i++) {
        if (scanf("%d, &n) != 1) {
            printf("Invalid input\n");
            return 1;
        }
        if (n % 2 == 0) {
            sum += n;
            count++;
        }
    }

    if (count > 0) {
        printf("There are %d even integers in the input, their sum is %d.\n",
               count, sum); 
    } else {
        printf("There are no even integers in the input.\n");
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    (n1,n2,n3,n4,n5) 是一个表达式序列,以逗号分隔,计算结果为最后一个表达式,因此:

    //if statement to determine what integers are even
    if(((n1,n2,n3,n4,n5)%2)==0)
    

    只决定n5的整除性

    你可以使用数组:

    int n[5];
    

    然后,在一个循环中:

    for (int i = 0; i < 5; i++) {
        if ((n[i] % 2) == 0) {
            sum += n[i];
        }
    }
    

    【讨论】:

      【解决方案3】:

      对偶数的简单测试是除以 2 并测试余数为 0

         if (  N % 2 == 0 ) {  // This is even, inc your even counter here}
      

      只需使用一个 for 循环并逐步将所有证明相加

      这适用于 float 或 int 就好了

      【讨论】:

        【解决方案4】:

        问题。你想做什么?

        Ans.您想编写一个从一组 5 个整数中计算偶数个整数的 C 程序。输出为 no。 5 个整数集合中的偶数个

        我认为你没有学习数组的概念。数组使这个问题很容易解决。但是,别担心,我根据你的问题理解你。 但, 您的任务是了解什么是数组,为什么以及何时使用它?

        -----xxx--------xxx----------xxx--------- --xxx------------------------------------------------ ------------------------------------

        要求是:

        1. 5个变量(即num)存储用户给定的值
        2. 一个变量,即计数编号。甚至没有。它使用 0 进行初始化,因为在给出输入之前最初甚至没有。

        -----xxx--------xxx----------xxx--------- --xxx------------------------------------------------ ----------------------

        您的代码中还有 1 个问题:-

        if(((n1,n2,n3,n4,n5)%2)==0) //it only check for n5 because comma operator seperates the values and gives only last value(i.e. n5) for computaion.
        

        解决方案:-

        #include <stdio.h> 
        
            int main()
            {
                int n1, n2, n3, n4, n5, count=0;     //var count works as a counter variable and it's value will update by 1 when any even no. encounters.
        
        
          printf("Enter five different positive integers: \n");
        
          scanf("%d %d %d %d %d", &n1, &n2, &n3, &n4, &n5);
        
        
           if(n1%2==0)
            {
              count=count+1;       //now, count is set by 1 if the first input(n1) found even
            }
        
        
            if(n2%2==0)
            {
              count=count+1;       //now, count is set by 2 if the second input(n2) found even
            }
        
        
            if(n3%2==0)
            {
              count=count+1;       //now, count is set by 3 if the third input(n3) found even
            }
        
            if(n4%2==0)
            {
              count=count+1;       //now, count is set by 4 if the fourth input(n4) found even
            }
        
        
            if(n5%2==0)
            {
              count=count+1;       //now, count is set by 5 if the fifth input(n5) found even
            }
        
        
        
        
                    printf("There are %d even integers in the input. \n", count); //count holds no. of even integers enconteres among 5 
        
        //if count prints 0 it indicates no even no occured.
        

        您必须使用数组和循环编写相同的代码。它使您的代码小而快执行:)

        【讨论】:

        • 如果你喜欢我解决问题的方式,你可以投票给我的答案或接受答案。 ;)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-13
        • 1970-01-01
        • 2016-01-31
        • 2014-06-01
        • 1970-01-01
        • 2014-06-23
        相关资源
        最近更新 更多