首页 > 专题系列 > Java解POJ > POJ 2812 Extrusion [解题报告] Java
2013
11-12

POJ 2812 Extrusion [解题报告] Java

Extrusion

问题描述 :

The Acme Extrusion Company specializes in the production of steel bars with custom-designed cross-sections. The manufacturing process involves cutting a hole in a thick metal plate, the shape of the hole being determined by the customer’s specifications.

Molten metal is then forced through the hole to form a long bar. The shape of the hole determines the shape of the cross-section of the resulting bar.

Given a description of a polygonal hole and the volume of molten metal available, determine how long a bar can be formed by this process.

输入:

Input consists of one or more data sets consisting of the following information:
  • An integer, N , indicating the number of vertices making up the polygon. End of input is signaled by any N less than 3.
  • Next are N lines, each containing a pair of floating-point numbers, (xi, yi) , each denoting one vertex of the polygon. Vertices will be presented in clockwise order (relative to the closest interior point) proceeding around the perimeter of the polygon. The xi and yi values are in units of meters.
  • The data set is terminated by a floating point value indicating the amount of molten metal available (in cubic meters).

输出:

For each data set, the program should produce a single line of output of the form:

BAR LENGTH: x

where “x ” is the maximum bar length, a floating point number expressed with two digits precision.

样例输入:

4
0.0 0.0
0.0 0.1
0.1 0.1
0.1 0.0  
1.0
7
0.5 1.25
0.9 1.6
0.9  1.1
0.85 1.0
0.9 0.85
0.9 0.5
0.5 0.75
100.0
0

样例输出:

BAR LENGTH: 100.00 
BAR LENGTH: 318.73

解题代码:

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;
class POINT{
	double x,y;
}
public class Main {
	static final int N = 100000;
	static POINT point[] = new POINT[N];
	static int n;
	static void start(){
		for(int i=0;i< N;++i) point[i] = new POINT();
	}
	static double Get_Num(StreamTokenizer cin) throws Exception{
		cin.nextToken();
		return cin.nval;
	}
	
public static void main(String[]args) throws Exception{
  int i;
  double V;
  start();
  StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
  while(true){
		n = (int) Get_Num(cin);
		if(n< 3) break;
		for(i=0;i< n;++i){
			point[i].x = Get_Num(cin);
			point[i].y = Get_Num(cin);
		}
		V = Get_Num(cin);
		System.out.printf("BAR LENGTH: %.2f\n",solve(V));
	}
    }
	
 static double corss(POINT a,POINT b,POINT c){
		return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
	}
	static double solve(double V){
		double ans=0.0;
		for(int i=2;i< n;++i){
			ans+=corss(point[0],point[i-1],point[i]);
		}
		ans = ans< 0.0?ans/-2.0:ans/2.0;
		return V/ans;
	}
}

  1. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count