7-3 高空坠球(20 分)
皮球从某给定高度自由落下,触地后反弹到原高度的一半,再落下,再反弹,……,如此反复。问皮球在第n次落地时,在空中一共经过多少距离?第n次反弹的高度是多少? 输入格式:
输入在一行中给出两个非负整数,分别是皮球的初始高度和n,均在长整型范围内。 输出格式:
在一行中顺序输出皮球第n次落地时在空中经过的距离、以及第n次反弹的高度,其间以一个空格分隔,保留一位小数。题目保证计算结果不超过双精度范围。 输入样例:
33 5
输出样例:
94.9 1.0
思路
模拟这个过程就可以了 但是要注意 n == 0 的情况 剩用 –n
AC代码
#include <cstdio> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cmath> #include <climits> #include <ctime> #include <iostream> #include <algorithm> #include <deque> #include <vector> #include <queue> #include <string> #include <map> #include <stack> #include <set> #include <numeric> #include <sstream> #include <iomanip> #include <limits> #define CLR(a) memset(a, 0, sizeof(a)) #define pb push_back using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair<string, int> psi; typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327; const double E = exp(1); const double eps = 1e-30; const int INF = 0x3f3f3f3f; const int maxn = 1e5 + 5; const int MOD = 1e9 + 7; int main() { long double h; ll n; cin >> h >> n; if (n == 0) printf("0.0 0.0\n"); else { long double tot = h; while (--n) { h /= 2; tot += 2 * h; } h /= 2; printf("%.1Lf %.1Lf\n", tot, h); } }转载于:https://www.cnblogs.com/Dup4/p/9433204.html
相关资源:JAVA上百实例源码以及开源项目