using System; namespace xx { class xianxing { [STAThread]//定义入口点; static void Main(string[] args) { //定义一个二维数组,最大为100组 int [,]arr=new int[100,2]; int i,j,n; //需要输入的数据是多少组 Console.Write("please input a number: "); n=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("please input the numbers"); //用循环求出所有数组的X,Y的和,平均值 for(i=0;i<n;i++) { for(j=0;j<2;j++) arr[i,j]=Convert.ToInt32(Console.ReadLine()); } float avg_x=0,avg_y=0,sum_x=0,sum_y=0; for(i=0;i<n;i++) { sum_x=sum_x+arr[i,0]; avg_x=sum_x/n; } for(i=0;i<n;i++) { sum_y=sum_y+arr[i,1]; avg_y=sum_y/n; } float l_xx=0,l_yy=0,l_xy=0; float a,b; double r; for(i=0;i<n;i++) //求出x,y的方差 { l_xx=(arr[i,0]-avg_x)*(arr[i,0]-avg_x); l_yy=(arr[i,1]-avg_y)*(arr[i,1]-avg_y); l_xy=(arr[i,0]-avg_x)*(arr[i,1]-avg_y); } //得出线性方程所要求的a,b,r b=l_xy/l_xx; a=avg_y-b*avg_x; r=l_xy/Math.Sqrt(l_xx*l_yy); Console.WriteLine("a={0};",a); Console.WriteLine("b={0};",b); Console.WriteLine("r={0};",r); } } }
最多是个预备程序员~~说话不脸红~哎呀呀 ~不晓得这样注释是不是正确~俺是新人俺怕谁~ 要脸一辈子学不到东西~