fortran如何编程出蛇形矩阵呢?求解答
fortran吧
全部回复
仅看楼主
level 1
这样的蛇形矩阵
1 3 4
2 5 8
6 7 9
2018年05月28日 13点05分 1
level 9
Program Main
Implicit None
Integer :: N , M , Number , i , j , iloc , jloc
Integer , Allocatable :: Mat(:,:)
Write(*,"(A12\)")"Input Dim = "
Read(*,*)N
Allocate(Mat(N , N))
M = 2 * N + 1
Number = 1
Do i = 1 , M
If(mod(i , 2) == 1)Then
iloc = 1
jloc = i
Do j = 1 , i
If( iloc > 0 .and. iloc <= N .and. jloc > 0 .and. jloc <= N)Then
Mat(iloc , jloc) = Number
Number = Number + 1
End If
iloc = iloc + 1
jloc = jloc - 1
End Do
Else
iloc = i
jloc = 1
Do j = 1 , i
If( iloc > 0 .and. iloc <= N .and. jloc > 0 .and. jloc <= N)Then
Mat(iloc , jloc) = Number
Number = Number + 1
End If
iloc = iloc - 1
jloc = jloc + 1
End Do
End If
End Do
Do i = 1 , N
Write(*,"(18I6)")Mat(i,:)
End Do
Deallocate(Mat)
End Program
2018年05月29日 03点05分 2
谢谢谢谢~~那个前面有点不太懂,能说下思路吗[委屈]
2018年05月29日 08点05分
@拉瓦CBYTX197 走斜线,在矩阵区域内填数字,不在矩阵区域里跳过。
2018年05月30日 07点05分
1