acm问题解决1.5

本文介绍了一种基于自然数序列生成蛇形三角矩阵的方法,并提供了一个C语言实现的例子。该算法能够根据输入的正整数N,输出一个由自然数构成的N行蛇形矩阵,每行数字间用空格隔开。

[description of the problem]


The snake matrix is a triangular matrix on the basis of natural numbers starting from 1.


[input] there are multiple sets of data in the topic, and each group consists of a positive integer N. (N not more than 100)


Output for each set of data, output a snake line of N row. No extra space between the two sets of output. The number of the same row in the matrix triangle is separated by a space. Do not have extra space at the end of the line.


[input case] 5


[output example] 1361015259144 81371211

solved:

#include<stdio.h>

#define M 100

void main()

{

 int N,i,j,a[M][M];

 while(scanf("%d",&N)!='/0')

 {

  a[0][0]=1;

  printf("%d",a[0][0]);

  for(i=0;i<N-1;i++)

  {

   a[i+1][0]=a[i][0]+i+1;

   for(j=0;j<N-i-1;j++)

   {

    a[i][j+1]=a[i][j]+j+i+2;

    printf(" %d",a[i][j+1]);

   }

   printf("\n");

   printf("%d",a[i+1][0]);

  }

 }

 printf("\n");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ifHappyEveryDay@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值