Moon Bridge

Moon Bridge

mission details

This task: write a function to calculate how many times the paper needs to be folded (assuming that the paper is large enough to be folded infinitely), and its thickness can build a paper bridge to the moon. Considering the need to go to a farther planet in the future, the function needs Calculate the number of times the paper is folded according to the specific distance and return.

Test sample

Test input:363300 0.088

Expected output:需要折叠42次

Test input:405500 0.088

Expected output:需要折叠43次

Source code

#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 **********/
}

Guess you like

Origin www.cnblogs.com/lightice/p/12692342.html