cdoj802-Just a Line
http://acm.uestc.edu.cn/#/problem/show/802
Just a Line
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
There are N points on a plane, among them N−1 points will form a line, your task is to find the point that is not on the line.
Input
The first line contains a single number N, the number of points. (4≤N≤50000)
Then come N lines each with two numbers (xi,yi), giving the position of the points. The points are given in integers. No two points' positions are the same. (−109≤xi,yi≤109)
Output
Output the position of the point that is not on the line.
Sample input and output
Sample Input | Sample Output |
---|---|
5 0 0 1 1 3 4 2 2 4 4 |
3 4 |
题目很简单,我一直wa的原因在于两点:%g与%.0f没有用好,遇到double型等的整数,别用%g,用%d或%.0f(我一直不晓得%g哪里错了,难道说有的整数强转后还能转出几位小数出来不成);另一点是eps,精度之前调为1e-10竟然精度还不够,以后就直接上-20.
代码1:
1 #include <fstream> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 #include <cmath> 7 #include <cstdlib> 8 9 using namespace std; 10 11 #define PI acos(-1.0) 12 #define EPS 1e-20 13 #define lll __int64 14 #define ll long long 15 #define INF 0x7fffffff 16 17 double a[10][2]; 18 int n; 19 20 inline double K(int i,int j);//斜率 21 inline bool F(double i,double j); 22 23 int main(){ 24 //freopen("D:\\input.in","r",stdin); 25 //freopen("D:\\output.out","w",stdout); 26 scanf("%d",&n); 27 n-=4; 28 for(int i=0;i<4;i++) scanf("%lf %lf",&a[i][0],&a[i][1]); 29 double k01=K(0,1); 30 double k02=K(0,2); 31 double k03=K(0,3); 32 double k12=K(2,1); 33 double k13=K(3,1); 34 double k23=K(2,3); 35 if(F(k01,k02)&&F(k01,k03)&&F(k03,k02)) printf("%.0f %.0f\n",a[0][0],a[0][1]); 36 else if(F(k01,k12)&&F(k01,k13)&&F(k13,k12)) printf("%.0f %.0f\n",a[1][0],a[1][1]); 37 else if(F(k12,k02)&&F(k12,k23)&&F(k23,k02)) printf("%.0f %.0f\n",a[2][0],a[2][1]); 38 else if(F(k03,k13)&&F(k23,k03)&&F(k23,k13)) printf("%.0f %.0f\n",a[3][0],a[3][1]); 39 else{ 40 double k04,k14,k24; 41 for(int i=0;i<n;i++){ 42 scanf("%lf %lf",&a[4][0],&a[4][1]); 43 k04=K(0,4); 44 k14=K(1,4); 45 k24=K(2,4); 46 if(F(k04,k14)&&F(k04,k24)&&F(k14,k24)){ 47 printf("%.0f %.0f\n",a[4][0],a[4][1]); 48 break; 49 } 50 } 51 } 52 return 0; 53 } 54 inline double K(int i,int j){ 55 if(a[i][0]==a[j][0]) return (double)INF; 56 else return (a[i][1]-a[j][1])/(a[i][0]-a[j][0]); 57 } 58 inline bool F(double i,double j){ 59 return fabs(i-j)>EPS; 60 }
代码2:
1 #include <fstream> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 #include <cmath> 7 #include <cstdlib> 8 9 using namespace std; 10 11 #define PI acos(-1.0) 12 #define EPS 1e-20 13 #define lll __int64 14 #define ll long long 15 #define INF 0x7fffffff 16 #define INT 2147483646 17 18 double a[50005][2]; 19 int n; 20 21 inline double K(int i,int j); 22 23 int main(){ 24 //freopen("D:\\input.in","r",stdin); 25 //freopen("D:\\output.out","w",stdout); 26 double kk[5]; 27 int cnt; 28 scanf("%d",&n); 29 for(int i=0;i<n;i++) scanf("%lf%lf",&a[i][0],&a[i][1]); 30 for(int i=0;i<n;i++){ 31 cnt=0; 32 for(int j=0;j<4;j++){ 33 if(i==j) continue; 34 kk[cnt++]=K(i,j); 35 } 36 if(fabs(kk[0]-kk[1])>EPS&&fabs(kk[0]-kk[2])>EPS&&fabs(kk[1]-kk[2])>EPS){ 37 printf("%.0f %.0f\n",a[i][0],a[i][1]); 38 break; 39 } 40 } 41 return 0; 42 } 43 inline double K(int i,int j){ 44 if(a[i][0]==a[j][0]) return (double)INT; 45 else return (a[i][1]-a[j][1])/(a[i][0]-a[j][0]); 46 }