输入小球下落的高度和落地的次数(先输入小球初始高度再输入反弹次数)
输出小球第 n 次 落地时经过的距离和第 n 次反弹的高度(保留小数点后1位)
100 1
100.0 50.0
100 3
250.0 12.5
#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { // 下落的高度和落地的次数 double h; int n; cin >> h; cin >> n; // 落地时经过的距离和第 n 次反弹的高度 double distance = h; double height = h; while(--n){ distance += height; height /= 2; } height /= 2; cout<< std::fixed << setprecision(1)<< distance << " "<< height << endl; return 0; }
// // Created by Sween'e'y on 2025/2/18. // #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { double h; int n; cin >> h; cin >> n; double sum = 0, num = 0; sum += h; h *= 0.5; for (int i = 0; i < n - 1; ++i) { sum += 2 * h; h = h * 0.5; } printf("%.1f %.1f", sum, h); return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { // 下落的高度和落地的次数 double h; int n; cin >> h; cin >> n; // write your code here...... double s = 0 , height = h , cnt = h;//cnt记录原始高度 for(int i = 1; i<= n; ++i){ s += 2*h; h /= 2; height /= 2; } printf("%.1lf %.1lf",s-cnt,height);//第一次下落是单程 return 0; }
#include <stdio.h> #include <math.h> int main() { int n=0; int m=0; int h=0; scanf("%d %d",&h,&m);//h是下落的高度,m是落地的次数 float h1=0;//h1代表第n次落地经过的距离 float h2=0;//h2代表第你次反弹的高度 h2=h/pow(2,m); int i=0; float b=0; for(i=0;i<m;i++) { b=h*(1.0/pow(2,i)); if(b==h) { h1+=h; } else { h1+=2*b; } } printf("%0.1f %0.1f",h1,h2); } //给帅哥代码点点赞