程序输出偶数和奇数程序的错误答案

时间:2018-01-29 04:24:18

标签: c++ algorithm logic

我需要创建一个程序,它获得6个随机整数输入(3个偶数,3个奇数;以任意顺序),然​​后显示输入的最小偶数和奇数。我的代码几乎完全相同,除非你为第一个变量输入一个数字,如-6,对于最后一个变量一直输入-1,最小的偶数计为-4,最小的奇数为-5。究竟出现了什么问题才能实现这一目标?

short int a, b, c, d, e, f, smallEven=0, smallOdd=0;


 cout<<"Enter a number: ";          //test a-c: even or odd; is there a small even or small odd; if there is, is new input smaller than old?
cin>>a;
if (a%2==0){
   if (smallEven==0)
      smallEven=d;
   else if (a<smallEven)
        smallEven=a;
}
else if (a%2== 1 || a%2== -1){
     if (smallOdd==0)
        smallOdd=a;
     else if (a<smallOdd)
          smallOdd=a;} 

cout<<"Enter a number: ";
cin>>b;
if (b%2==0){
   if (smallEven==0)
      smallEven=b;
   else if (b<smallEven)
        smallEven=b;
}
else if (b%2==1 || b%2== -1){
     if (smallOdd==0)
        smallOdd=b;
     else if (b<smallOdd)
          smallOdd=b;}

cout<<"Enter a number: ";
cin>>c;
if (c%2==0){
   if (smallEven==0)
      smallEven=c;
   else if (c<smallEven)
        smallEven=c;
}
else if (c%2== 1 || c%2== -1){
     if (smallOdd==0)
        smallOdd=c;
     else if (c<smallOdd)
          smallOdd=c;}


cout<<"Enter a number: ";           //at this point only need to ask if input is even or odd, and if its the smallest
cin>>d;
if (d%2==0){
   if (d<smallEven)
        smallEven=d;
}
else if (d%2== 1 || d%2== -1){
     if (d<smallOdd)
          smallOdd=d;}

 cout<<"Enter a number: ";
cin>>e;
if (e%2==0){
   if (e<smallEven)
        smallEven=e;}
else if (e%2== 1 || e%2== -1){
     if (e<smallOdd)
          smallOdd=e;}

cout<<"Enter a number: ";
cin>>f;
if (f%2==0){
   if (f<smallEven)
        smallEven=f;}
else if (f%2== 1 || f%2== -1){
     if (f<smallOdd)
          smallOdd=f;}

2 个答案:

答案 0 :(得分:0)

void smallest(int* even, int* odd, int n){
        if(n%2 == 0 && n < *even) *even = n;
        else if(abs(n%2) == 1 && n < *odd) *odd = n;
}

然后你可以做这样的事情

int n, even, odd;

while(cin >> n){
 smallest(&even, &odd, n); 
}

如果您需要以这种方式进行分配,逻辑似乎是正确的,但要非常仔细地检查变量。我在第一个街区看到一些拼写错误。

答案 1 :(得分:0)

许多重复代码的问题是你经常在复制时犯下cut'n'paste错误。如果你是cin >> **a**但是if (smallEven==0) smallEven=**d**,那么这在你的第一部分就很明显了。

执行此操作的理想方法是将其分解为函数,但是,由于您声明由于课程限制而不允许这样做,因此您仍然可以通过实现不< em>需要很多变量。

int num, smallEven, smallOdd, countEven = 0, countOdd = 0;

while ((countEven < 3) && (countOdd < 3)) {
    cin >> num;

    // Select even or odd.

    if ((num % 2) == 0) {
        // Check allowed.

        if (countEven == 3) {
            cout << "No more even numbers allowed\n";
            continue;
        }

        // Store if first or lesser, add to count.

        if (countEven == 0) {
            smallEven = num;
        } else {
            if (num < smallEven) {
                smallEven = num;
            }
        }
        countEven++;
    } else {
        // Check allowed.

        if (countOdd == 3) {
            cout << "No more odd numbers allowed\n";
            continue;
        }

        // Store if first or lesser, add to count.

        if (countOdd == 0) {
            smallOdd = num;
        } else {
            if (num < smallOdd) {
                smallOdd = num;
            }
        }
        countOdd++;
    }

使用功能,这变得更加容易:

void apply(const char *type, const int num, int &count, int &small) {
    if (count == 3) {
        cout << "No more " << type << " numbers allowed\n";
        return;
    }

    // Store if first or lesser.

    if (count == 0) {
        small = num;
    } else {
        if (num < small) {
            small = num;
        }
    }
    count++;
}

:

int num, smallEven, smallOdd, countEven = 0, countOdd = 0;

while ((countEven < 3) && (countOdd < 3)) {
    cin >> num;

    // Select even or odd.

    if ((num % 2) == 0)
        apply("even", num, countEven, smallEven);
    else
        apply("odd", num, countOdd, smallOdd);
}