fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. double x[500];
  6. double y[500];
  7. double sx, sy, sxx, sxy;
  8. double a, b;
  9. int i, n;
  10.  
  11. for (i = 0; i < 500; i++) {
  12. scanf("%lf%lf", &x[i], &y[i]);
  13. if (x[i] == 0.0) break;
  14. }
  15. n = i;
  16. sx = sy = sxx = sxy = 0;
  17. for (i = 0; i < n; i++) {
  18. sx += x[i];
  19. sy += y[i];
  20. sxx += x[i] * x[i];
  21. sxy += x[i] * y[i];
  22. }
  23. a = (n * sxy - sx * sy) / (n * sxx - sx * sx);
  24. b = (sxx * sy - sxy * sx) / (n * sxx - sx * sx);
  25. printf("a=%f b=%f\n", a, b);
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 2684KB
stdin
1 5.7
3 10.4
4 11.1
6 19.5
7 21.8
10 26.2
0 0
stdout
a=2.431803 b=3.219016