首页 > 搜索 > BFS搜索 > POJ 2641 Billiard [解题报告] Java
2013
11-11

POJ 2641 Billiard [解题报告] Java

Billiard

问题描述 :

In a billiard table with horizontal side a inches and vertical side b inches, a ball is launched from the middle of the table. After s > 0 seconds the ball returns to the point from which it was launched, after having made m bounces off the vertical sides and n bounces off the horizontal sides of the table. Find the launching angle A (measured from the horizontal), which will be between 0 and 90 degrees inclusive, and the initial velocity of the ball.

Assume that the collisions with a side are elastic (no energy loss), and thus the velocity component of the ball parallel to each side remains unchanged. Also, assume the ball has a radius of zero. Remember that, unlike pool tables, billiard tables have no pockets.

输入:

Input consists of a sequence of lines, each containing five nonnegative integers separated by whitespace. The five numbers are: a, b, s, m, and n, respectively. All numbers are positive integers not greater than 10000.

Input is terminated by a line containing five zeroes.

输出:

For each input line except the last, output a line containing two real numbers (accurate to two decimal places) separated by a single space. The first number is the measure of the angle A in degrees and the second is the velocity of the ball measured in inches per second, according to the description above.

样例输入:

100 100 1 1 1
200 100 5 3 4
201 132 48 1900 156
0 0 0 0 0

样例输出:

45.00 141.42
33.69 144.22
3.09 7967.81

解题代码:

//* @author:
import java.util.*;
public class Main {
 static public void main( String [] str ){
   Scanner sc = new Scanner(System.in);
   while( sc.hasNext())
   {
     int a=sc.nextInt();
     int b=sc.nextInt();
     int s=sc.nextInt();
     int m=sc.nextInt();
     int n=sc.nextInt();
     if( a == 0 )
	break;
     double x = b*n;
     double y = a*m;
     System.out.printf( "%.2f %.2f\n", Math.atan2( x, y )/Math.acos(-1)*180, Math.sqrt( x*x + y*y ) / s );
     }
	
    }
}

  1. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }

  2. 这道题目的核心一句话是:取还是不取。
    如果当前取,则index+1作为参数。如果当前不取,则任用index作为参数。