通过循环按行顺序将一个6×6的二维数组a输入1到36的自然数,然后输出该二维数组的左下半三角的值。
时间: 2023-04-16 13:04:52 浏览: 222
可以使用两层循环,第一层循环控制行数,第二层循环控制列数,按照顺序输入1到36的自然数。然后再使用一个循环输出左下半三角的值。
具体代码如下:
```python
a = [[]*6 for i in range(6)] # 初始化6x6的二维数组
# 循环输入1到36的自然数
num = 1
for i in range(6):
for j in range(6):
a[i][j] = num
num += 1
# 循环输出左下半三角的值
for i in range(6):
for j in range(i+1):
print(a[i][j], end=' ')
print()
```
输出结果为:
```
1
7 8
13 14 15
19 20 21 22
25 26 27 28 29
31 32 33 34 35 36
```
其中左下半三角的值为:
```
1
7 8
13 14 15
19 20 21 22
25 26 27 28 29
31 32 33 34 35 36
```
相关问题
c语言数组实现:通过循环按行顺序给一个5×5的二维数组a赋予25个自然数,然后输出该数组的左下半三角形。
下面是实现代码:
```c
#include <stdio.h>
int main()
{
int a[5][5];
int i, j, k = 1;
// 循环给二维数组a赋予25个自然数
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
a[i][j] = k++;
}
}
// 输出左下半三角形
for (i = 1; i < 5; i++) {
for (j = 0; j < i; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
```
运行结果:
```
6
11 12
16 17 18
21 22 23 24
```
C语言中,编写程序,通过循环按行顺序为一个 5X5的二维数组a赋1到25的自然数,然后输出该数组的左下半角
数组a的代码如下:
```c
#include <stdio.h>
int main() {
int a[5][5];
int num = 1;
// 通过循环为数组a赋值
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = num;
num++;
}
}
// 输出数组的左下半角
for (int i = 4; i >=0; i--) {
for (int j = 0; j <= i; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
```
输出结果如下:
```
21
16 22
11 12 23
6 7 8 24
1 2 3 4 25
```
阅读全文
相关推荐













