我編寫了這個程序來計算給定比薩(直徑)的這個尺寸的切片數量。但結果似乎有點過了......任何援助將不勝感激:)C++ - 計算比薩中切片的數量
例如: 如果我輸入18寸的披薩,它導致4.00344片... 如果我輸入22英寸比薩,它結果在4.8931片...
見下面的代碼:
#include <iostream>
using namespace std;
int main()
{
// Title of CMD Window
system("title How many Slices are in your Pizza?");
// Declare variables
double diameter = 0.0, // Diameter of the pizza
slices = 0.0, // No. of slices in the pizza
area = 0.0, // Area of the whole pizza
oneSlice = 14.125; // Area of one pizza slice
const double PI = 3.14159;
// Display prompt
cout << "What is the diameter of the pizza (inches):" << "\n";
cin >> diameter;
// Calculate the area of the pizza
area = PI * diameter;
// Calculate number of slices for the size of pizza given
slices = area/oneSlice;
// Display results
cout << "\n\n" << "You have " << slices << " slice(s) in this pizza:" << "\n\n"
<< "************************************" << "\n"
<< "\tDiameter of pizza= " << diameter << "\n"
<< "\tArea of pizza= " << area << "\n"
<< "************************************" << "\n";
system("pause");
return 0;
}
// End of program
您會期望結果是什麼?爲什麼? – interjay
面積= PI *直徑;這真的看起來不對 – lezebulon
'area = PI * diameter; //或者嘗試area = PI * radius * radius;' - 你是否嘗試了評論的公式? –