登月紙橋
任務描述
本關任務:編寫一個函數,計算需要把紙折疊多少次(假設紙張足夠大,可以無限次折疊),其厚度才能搭建一座登月紙橋,考慮到將來需要到更遠的星球,所以函數需要根據具體距離計算紙張折疊的次數並返回。
測試樣例
測試輸入:363300 0.088
預期輸出:需要折疊42次
測試輸入:405500 0.088
預期輸出:需要折疊43次
源代碼
#include <iostream>
#include<math.h>
using namespace std;
// foldTimes-計算建紙橋的折疊次數
// 參數:dis-星際距離(千米),thick-紙的厚度(毫米)
// 返回值:建橋需要折疊的次數
int foldTimes(double dis, double thick);
int main()
{
double dis, thick;
cin >> dis >> thick;
cout << "需要折疊" << foldTimes(dis,thick) << "次" << endl;
return 0;
}
int foldTimes(double dis, double thick)
{
// 請在這里補充代碼,實現函數foldTimes
/********** Begin *********/
int i;
dis*=1000;
thick*=0.001;
double result=thick;
for(i=0;dis>result;i++){
result *=2;
}
return i;
/********** End **********/
}