方法一:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main() {
    int i = 0, j = 0;
    int a[4][5];

    //给数组成员赋值
    for (int i = 0; i < 20; i++) {
        a[i / 5][i % 5] = i + 1;
    }
    //输出数值
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 5; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

方法二:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main() {
    int i = 0, j = 0;
    int a[4][5];
   //给数组成员赋值
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 5; j++) {
            a[i][j] = 5 * i + j + 1;
        }   
    }
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 5; j++) {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

posted on 2022-08-28 20:59  wshidaboss  阅读(36)  评论(0编辑  收藏  举报