Java 小练习(004):自由落体运行求反弹高度(20170305)
点我查看第四期 Java 小练习(20170226)这期的题目如下:
一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反
弹多高?
第四期源代码参考:
**** Hidden Message ***** 谢谢 看看 看看答案,应该是用for循环解决 class Answer{
public double Slen(double high,int n){
//求第n次落地共经过多少米
double s=high;
for(int i=1;i<n;i++){
s+=high;
high=high/2;
}
return s;
}
public double H(double high,int n) {
//求第n次反弹的高度
double h=high/2;
for(int i=1;i<n;i++){
h=h/2;
}
return h;
}
}
public class JavaPractice0 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Answer a=new Answer();
System.out.println(a.Slen(100, 10)+""+a.H(100, 10));
}
}
还不错,看看 本帖最后由 renxiaole 于 2018-3-24 21:48 编辑
九九八十一 发表于 2017-11-15 16:18
public double slen(double high, int n) {
if(n <= 1) return high + high/2;
return high + high/2 + slen(high/2, n-1);
}
public double H(double high, int n) {
if(n <= 1) return high/2;
return H(high/2, n-1);
} 看卡 回复 高中等比数列求和. 111 1 import java.util.Arrays;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
int[] arry = new int;
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arry.length; i++){
System.out.println("请输入数字:");
arry = scanner.nextInt();
}
for (int i =0 ; i < arry.length-1; i++){
for (int j = i+1; j < arry.length; j++) {
if (arry > arry) {
int d = arry;
arry = arry;
arry = d;
}
}
}
System.out.println(Arrays.toString(arry));
}
} 6666 学习 public class Test1{
public static void main(String []args) {
double n = 100,sum = 0;
int i = 1;
while(i<=10) {
sum = sum +n;
n = n/2;
i++;
}
System.out.println("共经过:"+sum+"米");
System.out.println("第10次反弹的高度:"+n);
}
} 吧 阿斯达达萨斯达脚 299.609375 1
页:
[1]
2