写了一个,不怎么好,凑合着用吧。
程序代码:
#include <stdlib.h> #include <stdio.h> #include <math.h> #include <time.h> double calc_angle_cos(double x,double y); int main(void) { const double pi=3.14159; double a[100],b[100]; int i,k=0; double maxangle_cos=1.0,angle_cos,maxangle; srand((unsigned)time(0)); printf("100 points created randomly (|x|<5,|y|<5)\n"); for(i=0; i<100; i++) { a[i]=rand()%1000/100.0-5; b[i]=rand()%1000/100.0-5; if(b[i]==0) { i--; continue; } printf("(%.2lf,%.2lf)\t",a[i],b[i]); if(a[i]<0) { angle_cos=calc_angle_cos(a[i],b[i]); if(maxangle_cos>angle_cos) { maxangle_cos=angle_cos; k=i; } } } maxangle=acos(maxangle_cos)/pi*180; printf("\nMax angle (x<0) is %.1lf degree\n" "And created by the %dth point:(%.2lf,%.2lf)\n",maxangle,k+1,a[k],b[k]); return 0; } double calc_angle_cos(double x,double y) { double angle_cos; double a2,b2; a2=x*x+y*y; b2=(x-1)*(x-1)+y*y; angle_cos=(a2+b2-1)/(2*sqrt(a2)*sqrt(b2)); return angle_cos; }