#include<stdio.h> #include<stdlib.h> #include<math.h> #define MaxNum 15 //定义一共有十五个小区; //对小区进行结构化定义; struct vex { int X; int Y; double Need; //定义每个小区的需求; double tan; //储存小区的扫描角; int number; //定义小区的编号; }; int main(void) { int i,j,k=1; //float Needness[15]; //int x[15],y[15]; double car=5; double S; struct vex village[MaxNum] ,temp; //随机初始化小区位置点; double Needness[15]={3561.77,1917.88,3287.78,2191.86,1643.90,3287.79,1095.93,2739.83,1369.92,821.95,1369.92,958.94,958.94,1095.93,1095.93}; int x[15]={-68,-11,-66,68,90,327,-68,-14,-245,-50,-292,283,77,22,113}; int y[15]={-113,-120,-36,-16,13,16,43,61,92,106,165,178,210,268,279}; for(i=0;i<MaxNum;i++) { village[i].Need=Needness[i]/365; village[i].number=i+1; village[i].X=x[i]; village[i].Y=y[i]; S=(double)asin((village[i].Y/sqrt(village[i].Y*village[i].Y+village[i].X*village[i].X))); if(village[i].X>0&&village[i].Y>0) { village[i].tan=S; } if(village[i].X<0&&village[i].Y<0) { village[i].tan=S+3.1415; } if(village[i].X<0&&village[i].Y>0) { village[i].tan=S+3.1415; } if(village[i].X>0&&village[i].Y<0) { village[i].tan=S+3.1415*2; } } //按照小区的角度进行排序,排序方法:冒泡。 for(i=0;i<MaxNum;i++) { for(j=i+1;j<MaxNum;j++) { if(village[i].tan>village[j].tan) { temp.tan=village[i].tan; temp.X=village[i].X; temp.Y=village[i].Y; temp.number=village[i].number; temp.Need=village[i].Need; village[i].tan=village[j].tan; village[i].number=village[j].number; village[i].Need=village[j].Need; village[i].X=village[j].X; village[i].Y=village[j].Y; village[j].tan=temp.tan; village[j].number=temp.number; village[j].Need=temp.Need; village[j].X=temp.X; village[j].Y=temp.Y; } } } //按照扫描角度的大小顺序,从小到大对更小区进行排序 for(i=0;i<MaxNum;i++) { printf("(X,Y):(%d,%d) angle:%f Need:%f Num:%d ",village[i].X,village[i].Y,village[i].tan,village[i].Need,village [i].number); } printf(" "); //按照旋转角度从小到大输出小区的位置,位置相近的小区乘坐同一辆车; printf("第%d辆车: ",k); for(i=0;i<MaxNum;i++) { if(car>village[i].Need) { car=car-village[i].Need; printf("%d ",village[i].number); } else { if(car!=5) { village[i].Need=village[i].Need-car; printf("%d ",village[i].number); car=5; } while(village[i].Need>5) { printf(" "); printf("第%d辆车: ",k); printf("%d ",village[i].number); k++; village[i].Need=village[i].Need-5; } printf(" "); printf("第%d辆车: ",k); printf("%d ",village[i].number); k++; car=car-village[i].Need; } } printf(" "); return 0; }