Be Good at Gaussing
题目描述
Give you many positive integer N (N<=23), for each N, just output N*(N+1)/2 integers in a single line, separated by space. (Don't ask me why.) For each N, the output line contains integers from 1 to N, and each just once. Again, do not ask me why, thank you. I'm so busy. But I can tell you a secret, the output has relationship with number triangle. As:(N=3)
1
2 6
3 4 5
See the sample for more information.
输入
a positive integer N (N<=23).
输出
For each N, output N*(N+1)/2 integers in a single line, separated by a blank space.
样例输入
3
4
2
6
4
2
6
样例输出
1 2 6 3 4 5
1 2 9 3 10 8 4 5 6 7
1 2 3
1 2 15 3 16 14 4 17 21 13 5 18 19 20 12 6 7 8 9 10 11
1 2 9 3 10 8 4 5 6 7
1 2 3
1 2 15 3 16 14 4 17 21 13 5 18 19 20 12 6 7 8 9 10 11
我的代码:
#include <stdio.h>
#include <stdlib.h>
void sovle(int n)
{
int a[24][24],i=1,j=1,e=1,num=0;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
a[i][j]=0;
i=0;j=1;
if(n==1)
{
a[1][1]=1;
}
#include <stdlib.h>
void sovle(int n)
{
int a[24][24],i=1,j=1,e=1,num=0;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
a[i][j]=0;
i=0;j=1;
if(n==1)
{
a[1][1]=1;
}
if(n>=2){
while(a[i+1][j]==0)
{i++;
while( a[i][e]==0 && i<=n)
{
num++;
a[i][e]=num;
i++;
}
i--;
while( a[i][j+1]==0 && j+1<=n)
{
num++;
a[i][j+1]=num;
j++;
}
while(a[i-1][j-1] ==0)
{
j--;i--;num++;
a[i][j]=num;
}
e++;
}
}
for(i=1;i<=n-1;i++)
for(j=1;j<=i;j++)
printf("%d ",a[i][j]);
for(j=1;j<=i-1;j++)printf("%d ",a[i][j]);
printf("%d",a[i][j]);
printf("\n");
}
int main(int argc, char *argv[]) {
int n;
while(scanf("%d",&n)!=EOF)
{
sovle(n);
}
return 0;
}