用 C 列印弗洛伊德反向三角形程式


程式說明

弗洛伊德三角形是一個由自然陣列成的直角三角形陣列,用於計算機科學教育中。該三角形得名於羅伯特·弗洛伊德。它的定義是,用連續的數字填充三角形的行,從左上角的 1 開始

1                               15 14 13 12 11
2 3                             10 9 8 7
4 5 6                         6 5 4
7 8 9 10                       3 2
11 12 13 14 15                 1
Floyd's Triangle                Reverse of Floyd's Triangle

演算法

要列印弗洛伊德三角形−

Accept the number of rows to print the Floyd’s Triangle
Print value 1 for the Row 1
Print two values 2 and 3 in the next row
Print three values 4, 5 and 6 in the next row
Repeat till the number of rows specified

要列印弗洛伊德三角形的反向−

Accept the number of rows to print the reverse of Floyd’s Triangle
Print the values in the reverse order as specified in the reverse of Floyd’s Triangle

示例

/*Program to print the Reverse of Floyd's Triangle*/
#include<stdio.h>
int main() {
   int r,c=1;
   int rows,revrows,r1,c1,d;
   clrscr();
   printf("Enter number of rows to print the Floyd's Triangle: ");
   scanf("%d", &rows);
   printf("
");    for (r=1;r<=(rows*(rows+1))/2;r++){       printf("%d ",r);       if(r==(c*(c+1))/2){          printf("
");          c++;       }    }    printf("

");    /*Printing the Reverse of Floyd's Triangle*/    printf("Enter number of rows to print the reverse of Floyd's Triangle: ");    scanf("%d",&revrows);    printf("

");    printf("Reverse of Floyd's Triangle
");    printf("

");    d = (revrows*(revrows+1))/2;    for(r1=revrows;r1>=1;r1--){       for(c1=r1;c1>=1;c1--,d--){          printf("%4d", d);       }       printf("
");    }    getch();    return 0; }

輸出

更新時間: 13-Jul-2020

442 次瀏覽量

開啟你的 職業生涯

完成課程後獲得認證

開始入門
廣告
© . All rights reserved.