1.写一段程序,读入一个点的坐标,一个圆的中心坐标和半径,确定这个点是否在圆内
#include <stdio.h>
#include <math.h>
struct Point {
int x;
int y;
};
int main() {
float r,d;
struct Point point1,point2;
scanf("%d %d",&point1.x,&point1.y);
scanf("%d %d",&point2.x,&point2.y);
scanf("%f",&r);
d=sqrt(pow(point1.x-point2.x,2)+pow(point1.y-point2.y,2));
if(d>r)
printf("no");
else if(d==r)
printf("on");
else
printf("in");
}
2.已知平面上有100个点,任意两点可以构成线段,编写一个函数,求在可以构成的所有线段中,长度最小的线段长度
#include <stdio.h>
#include <math.h>
double fun(int a[100][2]) {
double min=-1;
double d;
for(int i=0; i<100; i++)
for(int j=i; j<100; j++) {
d=sqrt(pow(a[i][1]-a[j][1],2)+pow(a[i][2]-a[j][2],2));
if(min==-1&&d>0)
min=d;
else if(d<min)
min=d;
}
return min;
}
3.已知平面上有100个点,任意两点可以构成线段,编写一个函数,求在可以构成的所有线段中,长度最长的线段长度
#include <stdio.h>
#include <math.h>
double fun(int a[100][2]) {
double max=-1;
double d;
for(int i=0; i<100; i++)
for(int j=i; j<100; j++) {
d=sqrt(pow(a[i][1]-a[j][1],2)+pow(a[i][2]-a[j][2],2));
if(max==-1&&d>0)
max=d;
else if(d>max)
max=d;
}
return max;
}
4.平面有100个点,任意三个点可以构成一个三角形,编一程序,输入100个点的坐标,输出在构成的所有三角形中,最大的三角形面积(三个点的叉乘的二分之一即三点的面积)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct Point {
double x,y;
} Point;
Point decPoint(Point p1,Point p2) {
Point ret;
ret.x=p1.x-p2.x;
ret.y=p1.y-p2.y;
return ret;
}
double mutiPoint(Point p1,Point p2) {
return (p1.x*p2.y-p2.x*p1.y);
}
double area(Point A,Point B,Point C) {
return fabs(mutiPoint(decPoint(B,A),decPoint(C,A))/2.0);
}
int main() {
Point pts[110];
double x,y;
double max=-1;
for(int i=0; i<100; i++)
scanf("%lf %lf",&pts[i].x,&pts[i].y);
max=area(pts[0],pts[1],pts[2]);
for(int i=0; i<100; i++)
for(int j=i+1; j<100; j++)
for(int k=j+1; k<100; k++)
if(max<area(pts[i],pts[j],pts[k]))
max=area(pts[i],pts[j],pts[k]);
printf("%lf",max);
}
5.平面上的点由直角坐标系给出,建立描述平面上点位置的数据类型,并编写一个函数,求任意三点构成的三角形外接圆
#include <stdio.h>
#include <math.h>
typedef struct Point {
int x;
int y;
} Point;
void getCircle(Point p1,Point p2,Point p3) {
float x0,y0,R;
float x21,y21,x32,y32;
x21=p2.x-p1.x;
y21=p2.y-p1.y;
x32=p3.x-p2.x;
y32=p3.y-p2.y;
if(x21*y32-y21*x32==0)
return;
float xy21=p2.x*p2.x-p1.x*p2.x+p2.y*p2.y-p1.y*p1.y;
float xy32=p3.x*p3.x-p2.x*p2.x+p3.y*p3.y-p2.y*p2.y;
y0=(x32*xy21-x21*xy32)/(2.0*(y21*x32-y32*x21));
x0=(y32*xy21-y21*xy32)/(2.0*(x21*y32-x32*y21));
R=sqrt(pow(p1.x-x0,2)+pow(p1.y-y0,2));
printf("%lf",R);
}
int main() {
struct Point p1,p2,p3;
scanf("%d %d",&p1.x,&p1.y);
scanf("%d %d",&p2.x,&p2.y);
scanf("%d %d",&p3.x,&p3.y);
getCircle(p1,p2,p3);
}
6.平面上给100个圆,((x1,y1),r1)...分别代表圆心的坐标和半径,试写一函数,返回一个链表头,链表中的结构为三个阈m,n,link,表示m和n是相切的
#include <stdio.h>
#include <math.h>
int x[100];
int y[100];
int r[100];
typedef struct node {
int m,n;
struct node* next;
} node;
double d(int i,int j) {
return sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2));
}
node* fun() {
node* head;
node* p;
for(int i=0; i<100; i++)
for(int j=i+1; j<100; j++)
if((d(i,j)==r[i]+r[j])||(d(i,j)==r[i]-r[j])||(d(i,j)==r[j]-r[i])) {
p=(node*)malloc(sizeof(node));
p->m=i;
p->n=j;
p->next=head;
head=p;
}
return head;
}
7.求方程的解
#include <stdio.h>
#include <math.h>
int main() {
float a,b,c,disc,x1,x2;
scanf("%f %f %f",&a,&b,&c);
if(a==0)
printf("NULL");
else {
disc=1.0*b*b-4.0*a*c;
if(fabs(disc)==0)
printf("%f",-b/(2*a));
else if(disc>0) {
x1=(-b*1.0+sqrt(disc))/(2*a);
x2=(-b*1.0-sqrt(disc))/(2*a);
printf("%f,%f",x1,x2);
} else
printf("NULL");
}
}
8.用二分法求方程:位于(-10,10)之间的一个根
#include <stdio.h>
#include <math.h>
const double eps=1e-6;
double fun(double x){
return 2*x*x*x-4*x*x+3*x-6;
}
int main(){
double left=-10,right=10,middle;
if(fun(left)*fun(right)<0){
while(fabs(left-right)>eps){
middle=fun((left+right)/2);
if(fabs(middle)<eps)
break;
else if(fun(middle)*fun(left)<0)
right=middle;
else if(fun(middle)*fun(right)<0)
left=middle;
}
}
printf("%f %f\n",left,right);
printf("%f",(left+right)/2);
}
9.写一个程序,用牛顿迭代法求方程:在x=1.5附近的根
#include <stdio.h>
#include <math.h>
const double eps=1e-6;
double fun(double x){
return 2*x*x*x-4*x*x+3*x-6;
}
double getk(double x){
return 6*x*x-8*x+3;
}
double asnt(){
float x1=1.5,x2=x1-fun(x1)/getk(x1);
while(fabs(x1-x2)>eps){
x1=x2;
x2=x2-fun(x2)/getk(x2);
}
return x2;
}
int main(){
double key = asnt();
printf("%f",key);
}
10.编写程序,输入A,B,C,D四个点的坐标,假设ABC三点可以后成一个三角形,判断D点是否落在三角形内
#include <stdio.h>
#include <math.h>
typedef struct Point {
double x,y;
} Point;
Point decPoint(Point p1,Point p2) {
Point ret;
ret.x = p1.x-p2.x;
ret.y = p1.y-p2.y;
return ret;
}
double mutipoint(Point p1,Point p2) {
return p1.x*p2.y-p2.x*p1.y;
}
int sign(double x) {
double eps=1e-8;
if(fabs(x)<eps) return 0;
if(x>0) return 1;
return -1;
}
double cross(Point A,Point B,Point C) {
return mutipoint(decPoint(B,A),decPoint(C,A));
}
int main() {
Point pts[4];
int sgn[3];
for(int i=0; i<4; i++)
scanf("%lf %lf",&pts[i].x,&pts[i].y);
sgn[0]=sign(cross(pts[0],pts[1],pts[3]));
sgn[1]=sign(cross(pts[0],pts[2],pts[3]));
sgn[2]=sign(cross(pts[0],pts[1],pts[3]));
if(sgn[0]==0||sgn[1]==0||sgn[2]==0)
printf("on the triangle");
else if(sgn[0]==sgn[1]&&sgn[1]==sgn[2])
printf("in the triangle");
else
printf("out of the triangle");
}
#include <stdio.h>
#include <math.h>
typedef struct Point {
double x,y;
} Point;
Point decPoint(Point p1,Point p2) {
Point ret;
ret.x = p1.x-p2.x;
ret.y = p1.y-p2.y;
return ret;
}
double mutipoint(Point p1,Point p2) {
return p1.x*p2.y-p2.x*p1.y;
}
int sign(double x) {
double eps=1e-8;
if(fabs(x)<eps) return 0;
if(x>0) return 1;
return -1;
}
double cross(Point A,Point B,Point C) {
return mutipoint(decPoint(B,A),decPoint(C,A));
}
int main() {
Point pts[4];
int sgn[3];
for(int i=0; i<4; i++)
scanf("%lf %lf",&pts[i].x,&pts[i].y);
sgn[0]=sign(cross(pts[0],pts[1],pts[3]));
sgn[1]=sign(cross(pts[0],pts[2],pts[3]));
sgn[2]=sign(cross(pts[0],pts[1],pts[3]));
if(sgn[0]==0||sgn[1]==0||sgn[2]==0)
printf("on the triangle");
else if(sgn[0]==sgn[1]&&sgn[1]==sgn[2])
printf("in the triangle");
else
printf("out of the triangle");
}
11.已知数组ax[100]和ay[100]中分别保存了平面上100个点的横坐标和纵坐标,假定其中任意三个点均可构成三角形。编写一段代码,求这100个点构成的所有三角形中面积最大的三角形及其面积,即输出面积最大的三角形的各顶点坐标及面积值,注意:满足条件的可能有多组
#include <stdio.h>
#include <math.h>
double ax[100],ay[100];
double getss(double a,double b,double c) {
double res = (a+b+c)/2.0;
return res;
}
double getdec(int i,int j) {
return sqrt(pow(ax[i]-ax[j],2)+pow(ay[i]-ay[j],2));
}
double getarea(int i,int j,int k) {
double s=getss(getdec(i,j),getdec(i,k),getdec(j,k));
return sqrt(s*(s-getdec(i,j))*(s-getdec(i,k))*(s-getdec(j,k)));
}
int main() {
int maxx;
int maxy;
int maxz;
for(int i=0; i<100; i++)
scanf("%lf %lf",&ax[i],&ay[i]);
int maxarea=getarea(0,1,2);
for(int i=0; i<100; i++)
for(int j=i+1; j<100; j++)
for(int k=j+1; k<100; k++)
if(maxarea<getarea(i,j,k)) {
maxarea=getarea(i,j,k);
maxx=i;
maxy=j;
maxz=k;
}
printf("%lf\n",maxarea);
printf("%lf %lf %lf",maxx,maxy,maxz);
}