Java implementation pie (Pie, NWERC 2006, LA 3635)

Here Insert Picture Description
Title
has individual F + 1 to N sub circular pie, each person gets a piece must be sent, rather than a few pieces together, and the area to be the same. Everyone can get up to ask how much area of the school (not necessarily circular).

A first set of input data a number of acts T. The first row of each data two integers N and F (1 ≤ N, F ≤ 10 000); a second behavior N integers ri (1≤ri≤10 000), i.e., the radius of each faction.

For each test, the maximum output per area faction obtained, accurate to 10-3.

Thinking
I have to take my share of the size of the value, with half to get straight, copies of my recursion sum is divided in
parts it shows every big points are small,
small number of copies, indicating points each one is big
termination condition is my range is less than 0.001

import java.util.Scanner;

public class{
	public static int n=0,f=0;
	public static double [] R;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		  n = sc.nextInt();
		  f = 1+sc.nextInt();
		  R= new double [n];
		double sum=0.0;
		for (int i = 0; i <n; i++) {
			R[i]=sc.nextDouble();
			R[i]=Math.PI*R[i]*R[i];
			sum+=R[i];
		}
		double res =f(0,sum/f);
		System.out.println(res);
	}
	public static double f(double l,double r){
		if(r-l<=0.001){
			return r;
		}
		double mid = (l+r)/2;
		int sum=0;
		for (int i = 0; i < R.length; i++) {
			sum+=R[i]/mid;
		}
		//饼子小,分的太多
		if(sum>=f)
			return f(mid,r);
		//饼子大,不够分
		else
			return f(l,mid);
	}

}

Released 1530 original articles · won praise 20000 + · views 2.08 million +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104856776