#include "iostream.h"
#include "math.h"
#include "stdio.h"
main()
{
int n,i;
cout<<"请输入x,y个数:";
cin>>n;
cout<<endl;
float x,y;
float Lxx = 0;
float Lxy = 0;
float l1 = 0;
float l2 = 0;
float l3 = 0;
float l4 = 0;
for(i = 0;i < n;i ++)
{
cout<<"请输入(x,y):"<<endl;
cin>>x;
cin>>y;
l1 += x*y;
l2 += x;
l3 += y;
l4 += x*x;
}
Lxx = l4 - l2*l2/n;
Lxy = l1 - l2*l3/n;
float a , b;
a = Lxy/Lxx;
b = l3/n - a*l2/n;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
getchar();
return true;
}
评论12