You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单调递增序列判断程序问题: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 = 0 is 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 = num before checking if num > 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 if Statement: Your if (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:

  1. First, read the initial number. If it’s -1, exit immediately (since there’s no sequence to check).
  2. Initialize a flag variable (like isAscending = 1 for true) to track if the sequence stays valid.
  3. Set numPrev to this first valid number.
  4. 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 isAscending to 0 (false) because the sequence is no longer increasing.
    • Update numPrev to the current number for the next iteration.
  5. 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 -1 immediately, so we don’t waste time processing an empty sequence.
  • The isAscending flag stays 1 only if every number is strictly greater than the previous one. If any number fails this, we set the flag to 0 (you can even break early if you don’t need to read remaining inputs once the sequence is invalid).
  • We update numPrev after the comparison, ensuring we always compare the current number to the actual previous value from the last iteration.

内容的提问来源于stack exchange,提问作者AndrewM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:09:21