单调递增序列判断程序问题:do-while循环逻辑困惑及代码求助
Fixing Your Monotonic Ascending Sequence Check with Do-While Loop
Hey Andrew, let's work through this problem step by step. I can see you've got the right core idea using a do-while loop, but there are a few logic gaps and flow missteps in your code that are throwing things off. Let's break it down and fix it together.
First, Let's Spot the Issues in Your Original Code
- Risky Initialization: Setting
numPrev = 0is problematic—if your first input is a negative number (like-2), comparing it to 0 will incorrectly mark the sequence as non-increasing right away. - Backwards Comparison Logic: You’re setting
numPrev = numbefore checking ifnum > numPrev—this means you’re always comparing a number to itself, which will never be true. - Missing Validity Flag: You don’t have a variable to track whether the sequence breaks the monotonic ascending rule (e.g., a number that’s less than or equal to the previous one).
- Incomplete
ifStatement: Yourif (num > numPrev)has no body, so even if the condition were correct, nothing would happen.
Understanding the Correct Do-While Flow
The do-while loop runs the code block at least once before checking the exit condition. Here’s the right flow for your problem:
- First, read the initial number. If it’s
-1, exit immediately (since there’s no sequence to check). - Initialize a flag variable (like
isAscending = 1for true) to track if the sequence stays valid. - Set
numPrevto this first valid number. - Enter the do-while loop:
- Read the next number.
- If the number is
-1, exit the loop. - Check if the current number is less than or equal to the previous number—if yes, set
isAscendingto 0 (false) because the sequence is no longer increasing. - Update
numPrevto the current number for the next iteration.
- After exiting the loop, check the flag and print the result.
Corrected Code with Explanations
#include <stdio.h> int main() { // Note: main should return int, not void int num, numPrev; int isAscending = 1; // 1 = true (sequence is ascending so far) printf("Enter numbers (enter -1 to exit):\n"); // Read the first number to start the sequence scanf("%d", &num); // Handle case where user exits immediately if (num == -1) { printf("No numbers entered.\n"); return 0; } numPrev = num; // Set first number as the reference for comparison do { scanf("%d", &num); if (num == -1) { break; // Exit loop early if we get the termination signal } // Check if current number breaks the ascending rule if (num <= numPrev) { isAscending = 0; // Mark sequence as non-ascending // Optional: Uncomment below to stop reading inputs once invalid // break; } numPrev = num; // Update reference number for next comparison } while (num != -1); // Print final result based on the flag if (isAscending) { printf("The sequence is Monotonic Ascending\n"); } else { printf("The sequence is NOT Monotonic Ascending\n"); } return 0; }
Key Notes
- We first handle the edge case where the user enters
-1immediately, so we don’t waste time processing an empty sequence. - The
isAscendingflag stays1only if every number is strictly greater than the previous one. If any number fails this, we set the flag to0(you can even break early if you don’t need to read remaining inputs once the sequence is invalid). - We update
numPrevafter the comparison, ensuring we always compare the current number to the actual previous value from the last iteration.
内容的提问来源于stack exchange,提问作者AndrewM
相关产品推荐
相关产品推荐

