当前位置: 代码迷 >> C语言 >> 再求教一道图形题
  详细解决方案

再求教一道图形题

热度:140   发布时间:2005-05-18 09:34:00.0
再求教一道图形题

2.编写程序,输出如图所示高度为n的图形。

1 3 6 10 15 21

2 5 9 14 20

4 8 13 19

7 12 18

11 17 16 n=6时的数字倒三角

[此贴子已经被作者于2005-5-18 9:35:49编辑过]

搜索更多相关的解决方案: 图形  数字  三角  高度  

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------

/*编写程序,输出如图所示高度为n的图形。

n=6时的数字倒三角

1 3 6 10 15 21 2 5 9 14 20 4 8 13 19 7 12 18 11 17 16

*/

#include <stdio.h>

int matrix() { int i; int j; int counter; int count; int arry[6][6]; counter=1; count=0; for(i=0;i<6;i++) for(j=0;j<6;j++) arry[i][j]=0; do { for(i=count;i>=0;i--) { for(j=0;j<=count;j++) { if(i+j>count) { break; } else if(i+j==count) { arry[i][j]=counter; ++counter; } } } ++count; if(count==6) goto Label; }while(1);

Label: for(i=0;i<6;i++) { for(j=0;j<6-i;j++) printf("%5d",arry[i][j]); printf("\n\n\n"); } return 0; }

int main() { //int line; //printf("please enter the line:\n"); //scanf("%d",&line); matrix(); return 0; }


----------------解决方案--------------------------------------------------------
我的做法是:
main()
{
int k=1,a[6][6],m,n,i,j;
for(m=0;m&lt;=5;m++)
   { for(n=m;n&gt;=0;n--)
     { i=n;j=m-n;
      a[i][j]=k++;
     }
   }
for(i=0;i&lt;=5;i++)
  {
   for(j=0;j&lt;6-i;j++)printf("%-3d",a[i][j]);
   printf("\n");
  }
}

还望高手们多多指教!!
----------------解决方案--------------------------------------------------------
倒  太简单了吧!
都看不懂!!
----------------解决方案--------------------------------------------------------
真是太精彩了 谢拉!
----------------解决方案--------------------------------------------------------

我就喜欢凑热闹:给出2个方法,前一个还容易看懂一些,后一个故意混淆了一下。 #include <stdio.h>

int foo(int i, int j) { int t = i + j; return t * (t + 1) / 2 + i + 1; }

int main() { int i, j, n, t; while(scanf("%d", &n) == 1 && n > 0) { /* general solution: for(i = n; i > 0; i--) { printf("%d", foo(0, n - i)); for(j = 1; j < i; j++) printf(" %d", foo(j, n - i)); printf("\n"); } /*/ // another solution: for(t = n, i = n * (n + 1) / 2; i > 0; i--) { printf("%d", foo((t + 1) * t / 2 - i, n - t)); printf(t * (t - 1) / 2 == i - 1 ? t--, "\n" : " "); } //*/ } return 0; }


----------------解决方案--------------------------------------------------------
  相关解决方案