最佳拟合直线

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			int n = cin.nextInt();
            int[] x=new int[n];
            int[] y=new int[n];
			for (int i = 0; i < n; i++) {
                x[i]=cin.nextInt();
                y[i]=cin.nextInt();
			}
			int fx=sum(x);
			int fy=sum(y);
			int fxy=sum(x,y);
			int fxx=sum(x,x);
			double a=(double)(n*fxy-fx*fy)/(n*fxx-fx*fx);
			double b=(double)(fy*fxx-fx*fxy)/(n*fxx-fx*fx);
			System.out.println(String.format("%.3f",a));
			System.out.println(String.format("%.3f",b));
		}

		cin.close();
	}
	public static int sum(int[]...  arr)
	{
		int len=arr.length ;
		int deeplen=arr[0].length;
		@SuppressWarnings("unused")
		int sum=0;
		for(int i=0;i

你可能感兴趣的:(java4)