假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高?
数据范围:输入的小球初始高度满足 ,且保证是一个整数
假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高?
数据范围:输入的小球初始高度满足 ,且保证是一个整数
输入起始高度,int型
分别输出第5次落地时,共经过多少米以及第5次反弹多高。
注意:你可以认为你输出保留六位或以上小数的结果可以通过此题。
1
2.875 0.03125
/*使用递归解*/ #include<stdio.h> #include<stdlib.h> double journey(double high){ static int cnt_j = 0; cnt_j++; if(cnt_j >= 5){ cnt_j = 0; return high; } return (high + high / 2) + journey(high/2); } double tentHigh(double high){ static int cnt_h = 0; cnt_h++; if(cnt_h >= 5){ cnt_h = 0; return high/2; } return tentHigh(high / 2); } double getJourney(int high) { return journey((double)high); } double getTenthHigh(int high) { return tentHigh((double)high); } int main(){ int high = 0; while(~scanf("%d",&high)){ printf("%g\n",getJourney(high)); printf("%g\n",getTenthHigh(high)); } }
import java.util.Scanner; /** * @author aiker * @since 2021/11/6 */ public class Main { private static double[] calculateTrail(double height, int times) { double[] result = new double[2]; double trail; if (times == 1) { result[0] = height; result[1] = height / 2; return result; } trail = height; for (int i = 1; i < times; i++) { trail += height; height /= 2; } result[0] = trail; result[1] = height / 2; return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { double nextInt = scanner.nextDouble(); double[] result = calculateTrail(nextInt, 5); System.out.println(result[0]); System.out.println(result[1]); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int high = in.nextInt(); float fhigh = high; float sumhigh=0; for(int i=0;i<4;++i){ sumhigh+=fhigh/2+fhigh; fhigh/=2; } sumhigh+=fhigh; fhigh/=2; System.out.println(sumhigh+"\n"+fhigh); } }
#include<iostream> #include<stdio.h> using namespace std; int main() { double h; cin>>h; double ans=h; //第一次落地时的高度 for(int i=0;i<4;++i) { ans+=h; //反弹的高度是原来的一半,但是起-落的过程经历的距离是反弹高度的两倍,仍是h h/=2; //第i+2次落地之前弹起的高度 } h/=2; printf("%0.6f\n%0.6f\n",ans,h); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int data; while(cin>>data) { float h,sum,flag1,flag2,flag3,flag4,flag5; h=data; flag1=h/2; flag2=flag1/2; flag3=flag2/2; flag4=flag3/2; flag5=flag4/2; sum=flag1+flag2+flag3+flag4; sum*=2; sum+=data; cout<<sum<<endl; cout<<flag5<<endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n =sc.nextInt(); g(n); } } public static void g(int n){ if(n<0){ System.out.println(0); } double h5 = (double)n/32; double len = 0; for(int i=1;i<5;i++){ len+=(n/Math.pow(2, i))*2; } len+=n; System.out.println(len); System.out.println(h5); } }
#include <iostream> //输出第5次落地时经历来多少米,是不包括第五次可落地后回弹的高度的, //总共经历的米数 == 最初的height + 中间每一次回弹高度*2 using namespace std; int main() { int height; while(cin >> height) { double res = height; int tmp = 2; for(int i = 1;i<=4;i++) { res += ((double)height/tmp)*2; tmp = tmp*2; } cout<<res<<endl<<(double)height/32<<endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextDouble()) { int height = scanner.nextInt(); double total = height; double curheight = height; for (int i = 1; i < 5; i++) { total = total + curheight; curheight = curheight / 2; } curheight = curheight / 2; try { System.out.println(sisheliuru(total)); System.out.println(sisheliuru(curheight)); } catch (Exception e) { System.out.println("error"); } } scanner.close(); } /** * 保留6位有效数字,对后面的数字实行四舍六入五留双,如果double结果有.0,去掉小数点 * * @param total * @return */ public static String sisheliuru(double total) { StringBuilder sBuilder = new StringBuilder(); int htotal = (int) total; String shtotal = htotal + ""; String stotal = total + ""; if (total == (int) total) { stotal = shtotal; } int haspoint = stotal.indexOf("."); if (stotal.length() <= 6 || (stotal.length() == 7 && haspoint != -1)) { return stotal; } if (shtotal.length() > 6) { sBuilder.append(shtotal.subSequence(0, 5)); int point6 = shtotal.charAt(5) - '0'; int point7 = shtotal.charAt(6) - '0'; if (point7 > 5) { sBuilder.append(point6 + 1); } else if (point7 < 5) { sBuilder.append(point6); } else { if (point6 % 2 == 0) { sBuilder.append(point6); } else { sBuilder.append(point6 + 1); } } for (int i = 0; i < shtotal.length() - 6; i++) { sBuilder.append("0"); } } else if (shtotal.length() == 6) { sBuilder.append(shtotal.subSequence(0, 5)); int point6 = shtotal.charAt(5) - '0'; if (haspoint != -1) { int point7 = stotal.charAt(haspoint + 1) - '0'; if (point7 > 5) { sBuilder.append(point6 + 1); } else if (point7 < 5) { sBuilder.append(point6); } else { if (point6 % 2 == 0) { sBuilder.append(point6); } else { sBuilder.append(point6 + 1); } } } else { sBuilder.append(point6); } } else { if (haspoint != -1) { sBuilder.append(stotal.subSequence(0, 6)); int point6 = stotal.charAt(6) - '0'; int point7 = stotal.charAt(7) - '0'; if (point7 > 5) { sBuilder.append(point6 + 1); } else if (point7 < 5) { sBuilder.append(point6); } else { if (point6 % 2 == 0) { sBuilder.append(point6); } else { sBuilder.append(point6 + 1); } } } else { sBuilder.append(stotal); } } return sBuilder.toString(); } }
#include<iostream> using namespace std; double journey(double &begin){ double total=begin; for(int i=1;i<5;i++){ total+=begin; begin/=2; } begin/=2; return total; } int main(){ int begin; while(cin>>begin){ double height=(double)begin; double total=journey(height); cout<<total<<endl<<height<<endl; } }
//画图做题 #include<iostream> using namespace std; int main(){ int a; while (cin >> a){ cout << (a*1.0) + (a*1.0 * 2 / (1 << 1)) + (a*1.0 * 2 / (1 << 2)) + (a*1.0 * 2 / (1 << 3)) + (a*1.0 * 2 / (1 << 4)) << endl; cout << (a*1.0 / (1 << 5)) << endl; } system("pause"); return 0; }
#include <iostream> using namespace std; int main() { double h; while (cin >> h) cout << 23 * h / 8 << endl << h/32 << endl; }敢敢单单
#include <iostream>
using namespace std;
double bounce(int height)
{
double high=(double)height;
for(int i=0;i<5;i++)
high/=2;
return high;
}
double Sum(int height)
{
double high=(double)height;
double sum=high;
for(int i=0;i<4;i++)
{
sum+=high;
high/=2;
}
return sum;
}
int main()
{
int high;
while(cin>>high)
{
cout<<Sum(high)<<endl;
cout<<bounce(high)<<endl;
}
return 0;
}
#include<iostream> using namespace std; double getJourney(double high) { double sum = high; for (int i = 0; i < 4; i++) { high = 1.0 * high / 2; sum += high * 2; } return sum; } double getTenthHigh(double high) { for (int i = 0; i < 5; i++) { high = high / 2.0; } return (double)high; } int main() { int height; while (cin >> height) { cout << getJourney(height) << endl; cout << getTenthHigh(height) << endl; } return 0; }