#include <stdio.h>
#define M 100
#define N 100
int main()
{
int a[M][N]
int m = 3,n = 4 //这里设置数组的维度
int i,j
int t
int up=0,down=m-1
int left=0,right=n-1
i=up,j=left
t=1
while(1)
{
while(j != right+1)
{
a[i][j]=t
t++
j++
}
j--
if((t-1) == (m*n))
{
break
}
up++
i=up
while(i != down+1)
{
a[i][j]=t
t++
i++
}
if((t-1) == (m*n))
{
break
}
i--
right--
j=right
while(j != left-1)
{
a[i][j]=t
t++
j--
}
if((t-1) == (m*n))
{
break
}
j++
down--
i=down
while(i != up-1)
{
a[i][j]=t
t++
i--
}
if((t-1) == (m*n))
{
break
}
i++
left++
j=left
}
printf("---------螺旋形输出--------\n")
for(i = 0 i < m i++)
{
for(j = 0 j < n j++)
{
printf("%d ",a[i][j])
}
printf("\n")
}
printf("---------------------------\n")
return 0
}
哥们你很抠门儿呀,才5分!!
题目要求有一个问题,它要求N最小为2,却要求N为奇数,N为奇数应该最小为3或1才对.
按照题目要求编写的C语言矩阵程序如下
#include<stdio.h>
int main(){
int i,j,k,N,count=1
scanf("%d",&N)
if(N<=2 || 15<N || N%2==0){
printf("请输入小于等于15且大于2的奇数")
}else{
int a[N][N]
for(i=N/2i>=0i--){
k=N-i-1
for(j=N-i-2j>=ij--){
a[j][k]=count++
}
for(j=N-i-2j>=i+1j--){
a[i][j]=count++
}
for(j=ij<N-ij++){
a[j][i]=count++
}
for(j=i+1j<N-ij++){
a[k][j]=count++
}
}
for(i=0i<Ni++){
for(j=0j<Nj++){
printf("%-5d",a[i][j])
}
printf("\n")
}
}
return 0
}
#include<stdio.h>
#define N 3
void fun(int a[][N],int m)
{
int i, j
for (i = 0i <Ni++)
for (j = 0j <= ij++)
a[j][i] = a[j][i] * m
}
int main()
{
int i, j
int a[N][N]
printf("input a number: \n")
for(i=0i<Ni++)
for (j = 0j <Nj++)
scanf_s("%d", &a[i][j])
printf("3 * 3 matrix: \n")
for (i = 0i <Ni++)
{
for (j = 0j <Nj++)
printf("%3d", a[i][j])
printf("\n")
}
printf("change 3 * 3 matrix: \n")
fun(a, 2)
for (i = 0i <Ni++)
{
for (j = 0j <Nj++)
printf("%3d", a[i][j])
printf("\n")
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)