两数之和:
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
public class Main {
public static void main(String[] args) {
int array[] = {12, 2, 5, 9, 20, 29, 21, 14, 23, 10 };
System.out.println("Original array : ");
for (int i : array){
System.out.println(i + ""); //输出源数列
}
System.out.println();
int target = 15;
System.out.println("Target number : " + target);
for (int i = 0; i < array.length; i++){
for (int j = i; j < array.length; j++){
if (j == i){
continue;
}
else {
if (target == array[i] + array[j]){
System.out.println("Index : " + i + " " + j); //输出目标元素下标
}
}
}
}
}
}
自由落体:
一球从100米高度自由落下,每次落地后反跳回原高度的一半;n次落地经过路线总长度和下次反弹的高度。
public class Main {
static double highlength = 100;
public static void main(String[] args) {
Main m = new Main();
int n = 2;
System.out.println("第" + n + "次的高度为:" +m.nhiglength(n) +"m");
System.out.println("经过总长度为: " + m.totalhighlength(n) + "m");
}
double nhiglength(int n){
return (highlength/Math.pow(2,n));
/*Math.pow(x, y)
*x为底数,y为次幂*/
}
double totalhighlength(int n){
double sum = highlength;
for (int i = 1; i < n; i++){
sum += nhiglength(i);
}
return sum;
}
}