https://www.luogu.com.cn/problem/P1423
题目描述
小玉开心的在游泳,可是她很快难过的发现,自己的力气不够,游泳好累哦。已知小玉第一步能游
2 米,可是随着越来越累,力气越来越小,她接下来的每一步都只能游出上一步距离的
98%。现在小玉想知道,如果要游到距离 s 米的地方,她需要游多少步呢。请你编程解决这个问题。
程序首先读入目标距离s,然后使用一个while循环,计算小玉需要的步数。在每一次循环中,我们增加步数steps,并根据上一步的距离计算出这一步的距离2 * pow(0.98, steps - 1)。我们使用pow函数计算幂,因为每一步距离都是上一步距离的0.98倍。如果小玉游的总距离distance还没有达到目标距离s,我们就继续循环。当distance大于或等于s时,我们跳出循环并输出步数steps。
ACcode:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double s;
cin >> s;
int steps = 0;
double distance = 0.0;
while(distance < s)
{
steps++;
distance += 2 * pow(0.98, steps - 1);
}
cout << steps << endl;
return 0;
}