三角形火柴數的 C/C++ 程式?


在本文,我們將瞭解如何計算製作以下金字塔形狀所需火柴的數量。金字塔的底座已知。如果底座是 1,那麼製作金字塔需要 3 根火柴;底座為 2,需要 9 根火柴;底座為 3,需要 18 根火柴。

要解決此問題,我們必須使用此公式 -

示例

 動態演示

#include <iostream>
using namespace std;
int main(){
   int x;
   cout << "Enter the size of the base: ";
   cin >> x;
   int count = 3*x*(x+1)/2;
   cout << "Required Matchsticks: " << count;
}

輸出

Enter the size of the base: 5
Required Matchsticks: 45

更新於:2019 年 7 月 30 日

98 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.