编写算法:打印具有下面规律的图形。
1
5 2
8 6 3
10 9 7 4
输入:手动输入n
输出:格式输出n行
思路:
1.定义100x100的二维数组并给其赋值
赋值方式:
a30 | a31 | a32 | a33 |
a20 | a21 | a22 | |
a10 | a11 | ||
a00 |
从最后一行开始赋值,第一行赋值n个第二行n-2第i行n-i;
for(i=n-1;i>=0;i--)
{
for(j=0;j<=i;j++)
{
a[i][j]=k;k++;
}
}
{
for(j=0;j<=i;j++)
{
a[i][j]=k;k++;
}
}
输出:
第一行输出1个第i行输出i个
观察上面的表格得出输出规律:
1:a30
2:a20,a31
3:a10,a21,a31
n:a(n+1)0.......a(n+j)
#include<stdio.h>
void main()
{
int a[100][100],i,j,n;
int k=1,g=1;
printf("输入显示行数:");
scanf("%d",&n);
//给数组赋值
for(i=n-1;i>=0;i--)
{
for(j=0;j<=i;j++)
{
a[i][j]=k;k++;
}
}
//printf("%d\n",a[2][0]);
for(i=n-1;i>=0;i--)
{
for(j=0;j<g;j++)
{printf(" %4d",a[i+j][j]);}
printf("\n");g++;
}
}
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAc0AAAD2CAYAAAC0j9zHAAAgAElEQVR4Ae2d344rS5aXo/aufZqjPtOnPWpEq6VBYFoaGIG4sEAjkFBf1A0PUK9QD1WvUALNBZeWOGIYiRvfDGIkNOAZJDQIBG2dmTN9umf/QyuzVtXycmRGpJ0RmbY/S94Zf1as9Ysvs3JlpNPeN58/f/4ceEEAAhCAAAQgkCTwJmmBAQQgAAEIQAACDQGSJgcCBCAAAQhAIJMASTMTFGYQgAAEIAABkibHAAQgAAEIQCCTAEkzExRmEIAABCAAAZImxwAEIAABCEAgk8Btyu6P//N/CbvdLmV20P/jH/84/JN//HvhzRvy8gEcGiAAAQhA4CwJJDPaf/vvfxa++vq3w1dfLzrevx1+68ft+y9//Tn85fefmvef/Nc/PSrZniXFsA3rx3XY7omXtqewGX69seeFCgQgAAEIzIfATerHDf79f/ij8C9+/5+FIT+BcHMTwh/+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