美图笔试题
发布于 2017-04-16 20:36 1668 次浏览 0 赞 来自 笔试面试  

/*假设一个球从空中释放,每次落地后反弹都能回到原来高度一半的位置,然后再次落下,依此循环。现在要求根据输入的初始高度H(m)和某时刻球经过的总路径长度L(m),要求输出从释放开始到这一时刻球落地的次数

输入

输入数据有多组,每组占一行,包括两个整数H(1<=H<=100000)和L(1<=L<=1000000)

输出

对于每个测试实例,要求输出球落地的次数;如果输入的数据无解,则输出no。每个测试实例的输出占一行。


样例输入

20 20

50 30

1 100

样例输出

1

0

no*/



import java.util.Scanner;

class bishi

public class Main {

public static void main(String args []) {

int H  [] = new int [3];

int L []  = new int [3];

Scanner in = new Scanner(System.in);

for (int i = 0; i < 3; i++) {

H[i] = in.nextInt();

L[i]= in.nextInt();

}

for (int i = 0; i < 3; i++) {

if (H[i]>L[i]) {

System.out.println("0");

}

else {

for (int j = 1;; j++) {

H[i] = H[i] +2*(1/j)*H[i];

if (H[i]>=L[i]) {

System.out.println(j);

}

else {

System.out.println("no");

}

break;

}

}

}

}

}


1 条回复

package exam.meitu.test;


import java.util.Scanner;


/**

 * [@author](/user/author) steven

 * 

 *         2017-04-16 下午7:44:33

 * 

 *         类说明:

 */

public class Main2 {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int H, L;


while (sc.hasNext()) {

int num;

H = sc.nextInt();

L = sc.nextInt();

if (1 <= H && H <= 100000 && 1 <= L && L <= 100000) {


if (H == L) {

num = 1;

} else if (H > L) {

num = 0;

} else if ((2 * H - L) < 0) {

System.out.println("no");

continue;

} else {

num = (int) (Math.log(H / (2 * H - L)) / Math.log(2) + 1);

}

System.out.println(num);

}

}

}

}


2017-04-16 20:44
添加回复
回到顶部