ホームページ  >  記事  >  バックエンド開発  >  逆フロイド三角形を出力するプログラムを C 言語で作成します。

逆フロイド三角形を出力するプログラムを C 言語で作成します。

WBOY
WBOY転載
2023-08-31 18:41:041361ブラウズ

プログラムの説明

フロイトの三角形は、コンピューター サイエンス教育で使用される自然数の直角三角形配列です。ロバート・フロイドにちなんで名付けられました。これは、三角形の行を左上隅の 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
<strong>Floyd&#39;s Triangle                </strong><strong>Reverse of Floyd&#39;s Triangle</strong>

アルゴリズム

フロイトの三角形を印刷します:

Accept the number of rows to print the Floyd&rsquo;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&rsquo;s Triangle
Print the values in the reverse order as specified in the reverse of Floyd&rsquo;s Triangle

Example

の中国語訳は次のとおりです:

Example

/*Program to print the Reverse of Floyd&#39;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&#39;s Triangle: ");
   scanf("%d", &rows);
   printf("</p><p>");
   for (r=1;r<=(rows*(rows+1))/2;r++){
      printf("%d ",r);
      if(r==(c*(c+1))/2){
         printf("</p><p>");
         c++;
      }
   }
   printf("</p><p></p><p>");
   /*Printing the Reverse of Floyd&#39;s Triangle*/
   printf("Enter number of rows to print the reverse of Floyd&#39;s Triangle: ");
   scanf("%d",&revrows);
   printf("</p><p></p><p>");
   printf("Reverse of Floyd&#39;s Triangle</p><p>");
   printf("</p><p></p><p>");
   d = (revrows*(revrows+1))/2;
   for(r1=revrows;r1>=1;r1--){
      for(c1=r1;c1>=1;c1--,d--){
         printf("%4d", d);
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}

Output

逆フロイド三角形を出力するプログラムを C 言語で作成します。

以上が逆フロイド三角形を出力するプログラムを C 言語で作成します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。