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

C++读取N组行列对生成填充连续数值二维数组问题求助

原代码问题汇总

  • 函数makeArray未声明返回值类型,C++要求所有函数必须明确指定返回值类型,此处不需要返回内容应声明为void
  • C++标准不支持变长数组int arr[a][b]的写法,且当前需求不需要实际存储数组,可直接删除该无用声明
  • 计数变量temp没有自增逻辑,会导致所有位置输出固定值1
  • 输出符号与样例要求不符:样例使用大括号{}、小括号()分组,原代码用了方括号[]
  • 元素末尾和行末尾多输出了多余的逗号,不符合样例格式要求
  • 遍历vector调用函数时语法错误:ip是存储pair的数组,需要取下标x对应元素,应写为ip[x].first和ip[x].second

修正后完整代码

#include<bits/stdc++.h>
using namespace std;

void makeArray(int a, int b){
    int temp = 1;
    cout << "{";
    for(int i = 0; i < a; i++){
        cout << "(";
        for(int j = 0; j < b; j++){
            cout << temp;
            if(j != b-1) cout << ",";
            temp++;
        }
        cout << ")";
        if(i != a-1) cout << ",";
    }
    cout << "}";
}

int main() {
    int n;
    cin >> n;
    int a,b;
    vector<pair<int,int>> ip;
    for(int i=0; i<n; i++){
        cin >> a >> b;
        ip.push_back(make_pair(a,b));
    }
    for (int x=0; x<n; x++)
    {
        makeArray(ip[x].first, ip[x].second);
        cout << endl;
    }
    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 02:45:02