现场编码题目3
有一个蓝色圆形游泳池,中间有两个黄色圆形岛,也就是说,下方图中有3个圆形图像,圆形半径分别为:最大的外圆半径为outR,岛1半径为inR1,岛2半径为inR2。求游泳池水域的面积。算法规则规定:必须采用面向对象技术,解决这个问题。
参考代码 C#版:
using System; namespace Case3 { class Circle { private int radius; public Circle(int r) { radius = r; } public double GetArea() { return 3.14 * radius * radius; } } class Program { static void Main(string[] args) { Console.WriteLine("Outer Circle Radius:"); int outR=int.Parse(Console.ReadLine()); Console.WriteLine("Inner Circle One Radius:"); int inR1 = int.Parse(Console.ReadLine()); Console.WriteLine("Inner Circle two Radius:"); int inR2 = int.Parse(Console.ReadLine()); Circle outCircle = new Circle(outR); Circle inCircle1 = new Circle(inR1); Circle inCircle2 = new Circle(inR2); double actualArea = outCircle.GetArea() - inCircle1.GetArea() - inCircle2.GetArea(); Console.WriteLine("Swimming Pool Actual Area:{0}", actualArea); } } }
参考代码 C++:
#include <iostream> #include<algorithm> using namespace std; class Circle { public: Circle(int r); double GetArea(); private: int radius; }; Circle::Circle(int r) { radius = r; } double Circle::GetArea() { return 3.14 * radius * radius; } int main() { float outR, inR1, inR2; cout << "Outer Circle Radius:"<<endl; cin >> outR; cout << "Inner Circle One Radius:"<<endl; cin >> inR1; cout << "Inner Circle two Radius:"<<endl; cin >> inR2; Circle outCircle = Circle(outR); Circle inCircle1 = Circle(inR1); Circle inCircle2 = Circle(inR2); double actualArea = outCircle.GetArea() - inCircle1.GetArea() - inCircle2.GetArea(); cout << "Swimming Pool Actual Area :" << actualArea << endl; }
参考代码 Python版:
class Cirlce: def __init__(self,r): self.r=r def GetArea(self): return self.r*self.r*3.14 outR=int(input("Outer Circle Radius:")) inR1=int(input("Inner Circle one Radius:")) inR2=int(input("Inner Circle two Radius:")) outCircle=Cirlce(outR) inCircle1=Cirlce(inR1) inCircle2=Cirlce(inR2) actualArea=outCircle.GetArea()-inCircle1.GetArea()-inCircle2.GetArea() print("Swimming Pool Actual Area:",actualArea)