将一段c语言程序转变为python程序,急!!!
代码如下:#include<stdio.h>voidprintMatrix(intn){intarr[20][20];inti,j,k;intindex=1;intcur...
代码如下:
#include <stdio.h>
void printMatrix(int n)
{
int arr[20][20];
int i,j,k;
int index = 1;
int curNum = 1;
//produce the left-up half of matrix
for(; index <= n; index++)
{
i =0; j = index-1;
for(k=1; k <= index; k++)
{
if(index%2 == 1)
arr[i][j] = curNum++;
else
arr[j][i] = curNum++;
i++; j--;
}
}
//produce the right-down half of matrix
for(; index < 2*n; index++)
{
i = index - n;
j = n-1;
for(k=1; k <= 2*n - index; k++)
{
if(index%2 == 1)
arr[i][j] = curNum++;
else
arr[j][i] = curNum++;
i++; j--;
}
}
//display the matrix
printf("The matrix is as follow:\n");
for(i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
int main(void)
{
printMatrix(4);
return 0;
}
求转化为python代码!!!急!!! 展开
#include <stdio.h>
void printMatrix(int n)
{
int arr[20][20];
int i,j,k;
int index = 1;
int curNum = 1;
//produce the left-up half of matrix
for(; index <= n; index++)
{
i =0; j = index-1;
for(k=1; k <= index; k++)
{
if(index%2 == 1)
arr[i][j] = curNum++;
else
arr[j][i] = curNum++;
i++; j--;
}
}
//produce the right-down half of matrix
for(; index < 2*n; index++)
{
i = index - n;
j = n-1;
for(k=1; k <= 2*n - index; k++)
{
if(index%2 == 1)
arr[i][j] = curNum++;
else
arr[j][i] = curNum++;
i++; j--;
}
}
//display the matrix
printf("The matrix is as follow:\n");
for(i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
int main(void)
{
printMatrix(4);
return 0;
}
求转化为python代码!!!急!!! 展开
展开全部
按照你的要求,把C语言程序转换成Python3语言的程序如下
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def printMatrix(n):
arr = [ [0 for i in range(20)] for i in range(20)]
curNum = 1
for index in range(1,n+1):
i =0
j = index-1
for k in range(1,index+1):
if index%2 == 1:
arr[i][j] = curNum
curNum=curNum+1
else:
arr[j][i] = curNum
curNum=curNum+1
i=i+1
j=j-1
for index in range(n+1,2*n):
i =index-n
j = n-1
for k in range(1,2*n-index+1):
if index%2 == 1:
arr[i][j] = curNum
curNum=curNum+1
else:
arr[j][i] = curNum
curNum=curNum+1
i=i+1
j=j-1
print ("The matrix is as follow:");
for i in range(0,n):
for j in range(0,n):
print (arr[i][j],"\t",end='')
print ("")
printMatrix(4)
运行结果
The matrix is as follow:
1 3 4 10
2 5 9 11
6 8 12 15
7 13 14 16
追问
谢谢
追答
不用谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询