无循环无数组从6个整数中找最小奇偶整数的代码问题排查
Hey there! Let's work through fixing your code to correctly find the smallest even and odd numbers from 6 input integers (3 even, 3 odd). I spotted a couple key issues that are throwing off your output—let's break them down and fix them step by step.
Key Issues in Your Current Code
- Incorrect initialization: Setting
smallEven=0andsmallOdd=0causes problems if your input includes negative numbers (e.g., if the smallest even is -6, your code would keep 0 instead). It also leads to wrong logic when checking if we've set the initial value. - Assignment instead of comparison: You're using
=(assignment) in yourifconditions likeif (smallEven=0)instead of==(equality check). This overwrites your variable and evaluates tofalseevery time, so your code never updates the minimum values correctly. - Incomplete input handling: You only process the first two numbers (
aandb)—you need to handle all 6 inputs!
Corrected Code
First, we'll initialize our min values to a very large number (using INT_MAX from the <climits> header) so any valid input number will be smaller than the initial value. Then, we'll process each input with proper equality checks and comparisons.
#include <iostream> #include <climits> // For INT_MAX using namespace std; int main() { int a, b, c, d, e, f; int smallEven = INT_MAX; // Initialize to largest possible int int smallOdd = INT_MAX; // Process first number cout << "Enter a number:\n"; cin >> a; if (a % 2 == 0) { smallEven = a; } else { smallOdd = a; } // Process second number cout << "Enter a number:\n"; cin >> b; if (b % 2 == 0) { if (b < smallEven) { smallEven = b; } } else { if (b < smallOdd) { smallOdd = b; } } // Process third number cout << "Enter a number:\n"; cin >> c; if (c % 2 == 0) { if (c < smallEven) { smallEven = c; } } else { if (c < smallOdd) { smallOdd = c; } } // Process fourth number cout << "Enter a number:\n"; cin >> d; if (d % 2 == 0) { if (d < smallEven) { smallEven = d; } } else { if (d < smallOdd) { smallOdd = d; } } // Process fifth number cout << "Enter a number:\n"; cin >> e; if (e % 2 == 0) { if (e < smallEven) { smallEven = e; } } else { if (e < smallOdd) { smallOdd = e; } } // Process sixth number cout << "Enter a number:\n"; cin >> f; if (f % 2 == 0) { if (f < smallEven) { smallEven = f; } } else { if (f < smallOdd) { smallOdd = f; } } // Output results cout << "Smallest even number: " << smallEven << endl; cout << "Smallest odd number: " << smallOdd << endl; return 0; }
How This Works
- Initialization: Using
INT_MAXensures that the first even/odd number we input will always replace the initial value, regardless of whether it's positive or negative. - Proper comparisons: For each input, we check if it's even or odd, then compare it to the current minimum value. If it's smaller, we update the minimum.
- Full input handling: We process all 6 required inputs, as specified in your assignment.
Test Case Example
If you input the numbers: -6, 3, -2, -5, 4, 1
- The smallest even number will be
-6 - The smallest odd number will be
-5
This code handles both positive and negative values correctly, and follows your assignment rules (no loops, no arrays).
内容的提问来源于stack exchange,提问作者edmonda7

