三角形火柴数的 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.