Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对角填充数组_C++ - Fatal编程技术网

C++ 对角填充数组

C++ 对角填充数组,c++,C++,我有以下节目。输入3 5 3 rows 5 growth of numbers 输出应为: 1 2 4 7 10 3 5 8 11 13 6 9 12 14 15 但我的程序给出: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 这是我到目前为止所做的尝试 int main() { int n, m, c = 0;

我有以下节目。输入3 5

3 rows
5 growth of numbers
输出应为:

1    2    4    7    10
3    5    8    11   13
6    9    12   14   15
但我的程序给出:

1    2    3    4    5
    6    7    8    9   10
   11   12   13   14   15
这是我到目前为止所做的尝试

int main() {
    int n, m, c = 0;
    cin >> n >> m;
    int a[n][m];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            a[i][j] = ++c;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
            cout << setw(4) << a[i][j];
        cout << endl;
    }
}
intmain(){
int n,m,c=0;
cin>>n>>m;
int a[n][m];
对于(int i=0;i不能Hi尝试改用tab

#include <iostream>
using namespace std;

int main() {
    int n, m, c = 0;

    cin >> n >> m;

    int *a  = new int[n * m];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            a[i * n + j] = ++c;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
            cout << "\t" << a[i * n + j];
        cout << endl;
    }
    delete[] a;
    return 0;
#包括
使用名称空间std;
int main(){
int n,m,c=0;
cin>>n>>m;
int*a=新的int[n*m];
对于(int i=0;i不能Hi尝试改用tab

#include <iostream>
using namespace std;

int main() {
    int n, m, c = 0;

    cin >> n >> m;

    int *a  = new int[n * m];

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            a[i * n + j] = ++c;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)
            cout << "\t" << a[i * n + j];
        cout << endl;
    }
    delete[] a;
    return 0;
#包括
使用名称空间std;
int main(){
int n,m,c=0;
cin>>n>>m;
int*a=新的int[n*m];
对于(int i=0;icout记不起我在中学时是如何解决这个问题的,但是当n小于m时,以下代码起作用:

#include <iostream>

using namespace std;

void nextij(long n,long m,long& i,long& j) {
    if (i==n-1) { //bottom row
        if (j<n-1) { //in the left square
            j = i+j+1;
            i = 0;
        }
        else { //out of the left square
            i = j-(n-1)+1;
            j = m-1;
        }
    }
    else { //other rows
        if (j==0) { //left most column
            j = i+1;
            i = 0;
        }
        else { //other columns
            i++;
            j--;
        }
    }
}

int main() {
    long n = 3;
    long m = 5;
    long a[3][5];  

    long i = 0;
    long j = 0;
    long c = 1;

    while (c<=n*m) {
        a[i][j] = c;        
        nextij(n,m,i,j);
        c++;        
    }

    for (i=0; i<n; i++) {
        for (j=0; j<m; j++)
            cout <<a[i][j] <<" ";
        cout <<endl;
    }
}

/*
output:
1 2 4 7 10 
3 5 8 11 13 
6 9 12 14 15 
*/
#包括
使用名称空间std;
void nextij(长n、长m、长i、长j){
如果(i==n-1){//最下面一行

如果(j记不起我在中学时是如何解决这个问题的,但是n小于m,那么下面的代码可以工作:

#include <iostream>

using namespace std;

void nextij(long n,long m,long& i,long& j) {
    if (i==n-1) { //bottom row
        if (j<n-1) { //in the left square
            j = i+j+1;
            i = 0;
        }
        else { //out of the left square
            i = j-(n-1)+1;
            j = m-1;
        }
    }
    else { //other rows
        if (j==0) { //left most column
            j = i+1;
            i = 0;
        }
        else { //other columns
            i++;
            j--;
        }
    }
}

int main() {
    long n = 3;
    long m = 5;
    long a[3][5];  

    long i = 0;
    long j = 0;
    long c = 1;

    while (c<=n*m) {
        a[i][j] = c;        
        nextij(n,m,i,j);
        c++;        
    }

    for (i=0; i<n; i++) {
        for (j=0; j<m; j++)
            cout <<a[i][j] <<" ";
        cout <<endl;
    }
}

/*
output:
1 2 4 7 10 
3 5 8 11 13 
6 9 12 14 15 
*/
#包括
使用名称空间std;
void nextij(长n、长m、长i、长j){
如果(i==n-1){//最下面一行

if(JJ)欢迎来到Stack Overflow。请花点时间阅读并参考您可以在此处询问的内容和方式。我投票将此问题作为离题题结束,因为它不是关于编程,而是关于使用测试系统的站点。我非常确定,只有在浏览器中将结果打印给您时,它才会失败(例如,跳过标记中的第一个空格)请注意,您打印了不同的数字欢迎来到Stack Overflow。请花点时间阅读并参考您可以在此处询问的内容和方式中的资料。我投票将此问题作为离题题结束,因为它不是关于编程,而是关于使用测试系统的网站。我非常确定只有在浏览器中将结果打印给您时才会失败(例如,跳过标记中的第一个空格)。只需修复所有其他内容,它就会工作。注意,您打印了不同的数字