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

C++作业求助:如何实现横竖尺寸一致的指定地板图案输出

Fixing Your Floor Pattern C++ Code

Hey there! Let's work through your code to get that floor pattern right—with consistent horizontal/vertical dimensions and correct output. I'll break down the issues in your current code, share a revised version, and give you some optimization tips too.

Issues in Your Current Code

  • Duplicate Condition Check: Your second else if (i%2 != 0) is identical to the first if condition. That means even-numbered rows will never use array2—you should use else or else if (i%2 == 0) instead.
  • Array Out-of-Bounds Risk: array1 and array2 only have 4 elements (indices 0-3), but your inner loop runs j <= n. If n > 3, this will access memory outside the arrays, causing undefined behavior.
  • Early Program Exit: You placed return 0; inside the outer for loop. This makes the program exit after printing just the first row, instead of all n rows.
  • Mismatched Dimensions: Your inner loop prints n+1 characters per row, but you're only printing n rows—this breaks the "equal horizontal/vertical size" requirement.

Revised Working Code

This version fixes all the above issues, adds character looping for when n > 4, and ensures consistent dimensions:

#include <iostream>
using namespace std;

int main() {
    int n;
    // Use char arrays since we're dealing with single characters
    constexpr char oddRowChars[4] = {'O', '>', 'X', '<'};
    constexpr char evenRowChars[4] = {'v', '/', '^', '\\'}; // Escape backslash
    
    cin >> n;
    
    for (int row = 1; row <= n; row++) {
        if (row % 2 != 0) {
            // Print n characters, looping through oddRowChars
            for (int col = 0; col < n; col++) {
                cout << oddRowChars[col % 4];
            }
        } else {
            // Print n characters, looping through evenRowChars
            for (int col = 0; col < n; col++) {
                cout << evenRowChars[col % 4];
            }
        }
        cout << '\n'; // Use '\n' instead of endl for faster output
    }
    
    return 0;
}

Is Using Arrays Necessary?

Absolutely—arrays are a great choice here! They organize your pattern characters into clear, maintainable groups. Without arrays, you'd have to use messy conditional logic (like switch statements) to pick each character, which would make the code harder to read and modify.

Extra Optimization Tips

  • Avoid #include<bits/stdc++.h>: It includes every standard library header, which bloats your code. Stick to only what you need—here, just #include <iostream>.
  • Use constexpr: Marking the character arrays as constexpr lets the compiler resolve them at compile time, making your program slightly more efficient.
  • Simplify Row Checks: You could start your row loop at 0 instead of 1, making the parity check more concise: if (row % 2 == 0) for even rows.
  • Single-Line Row Selection: Use a ternary operator to pick the current row's character set in one line:
    const char* currentChars = (row % 2 != 0) ? oddRowChars : evenRowChars;
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 13:08:13