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

C++实现输入起止数字输出指定有序对的代码修复

C++ 有序对打印功能修复方案

现有代码问题分析

  • 函数参数仅接收单个整数,不支持自定义起始、结束数字输入
  • 两层循环的终止条件均为<n,会漏掉等于结束值的遍历
  • 内层循环起始值为i+1,无法生成(ii)格式的首项有序对
  • 输出格式不符合要求:额外添加了逗号分隔符,且每个有序对后直接换行,没有整行输出完成后再换行
  • 主函数硬编码数值,没有实现用户输入获取逻辑

修复后完整代码

#include <iostream>
using namespace std;

// start为起始数字,end为结束数字
void printPairs(int start, int end) {
    for (int i = start; i <= end; ++i) {
        for (int j = i; j <= end; ++j) {
            cout << "(" << i << j << ")";
        }
        // 整行打印完成后换行
        cout << endl;
    }
}

int main() {
    int start, end;
    cout << "请输入起始数字:";
    cin >> start;
    cout << "请输入结束数字:";
    cin >> end;
    printPairs(start, end);
    return 0;
}

功能验证

输入起始值1、结束值5时,输出结果完全匹配预期:

(11)(12)(13)(14)(15)
(22)(23)(24)(25)
(33)(34)(35)
(44)(45)
(55)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 03:24:04