Luogu brush questions C++ language | P1423 Xiaoyu is swimming

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

Xiaoyu was swimming happily, but soon she found sadly that she was not strong enough, and she was very tired from swimming. It is known that Xiaoyu can swim 2 meters in the first step, but as she becomes more and more tired and her strength becomes weaker and weaker, she can only swim 98% of the distance of the previous step in each next step. Now Xiaoyu wants to know   how many steps she needs to swim to reach a distance of s meters. Please program to solve this problem.

【enter】

Input a real number  s (unit: meter), indicating the target distance to swim.

【Output】

Output an integer, indicating how many steps Xiaoyu needs to swim in total.

【Input sample】

4.3

【Example of output】

3

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;

int main()
{
    double n, len=0, step=2;
    int ans=0;
    cin >> n;
    while (len < n) {
        len += step;
        step *= 0.98;
        ans++;
    }
    cout << ans;
    return 0;
}

【operation result】

4.3
3

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132634713