小東和三個朋友一起在樓上拋小球,他們站在樓房的不同層,假設小東站的樓層距離地面N米,球從他手里自由落下,每次落地后反跳回上次下落高度的一半,並以此類推知道全部落到地面不跳,求4個小球一共經過了多少米?(數字都為整數) 給定四個整數A,B,C,D,請返回所求結果。


include

#include<vector>
using namespace std; 
   class Balls {
public:
  int calcDistance(int A, int B, int C, int D) {
		// write code here
		
		return calcDis(A) + calcDis(B) + calcDis(C) + calcDis(D);
	}

	int calcDis(int x)
	{
		float sum = x;
		float xx = x;
		while (xx>0.01)
		{
			xx = xx / 2;
			sum =sum+ 2 * xx;
		}
		return sum+0.5;
	}
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM