码迷,mamicode.com
首页 > 其他好文 > 详细

扫描法作业

时间:2015-11-08 19:18:21      阅读:425      评论:0      收藏:0      [点我收藏+]

标签:

#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\n",village[i].X,village[i].Y,village[i].tan,village[i].Need,village [i].number);

    }
    printf("\n");
    //按照旋转角度从小到大输出小区的位置,位置相近的小区乘坐同一辆车;
    printf("第%d辆车:\t",k);
    for(i=0;i<MaxNum;i++)
    {
        if(car>village[i].Need)
        {
            car=car-village[i].Need;
            printf("%d\t",village[i].number);
        }
        else
        {
            if(car!=5)
            {
                village[i].Need=village[i].Need-car;
                printf("%d\t",village[i].number);
                car=5;
            }
                while(village[i].Need>5)
                {
                    printf("\n");
                    printf("第%d辆车:\t",k);
                    printf("%d\t",village[i].number);
                    k++;
                    village[i].Need=village[i].Need-5;
                }
                    printf("\n");
                    printf("第%d辆车:\t",k);
                    printf("%d\t",village[i].number);
                    k++;
                    car=car-village[i].Need;
            
        }
    }
    printf("\n");
    return 0;

}

 

扫描法作业

标签:

原文地址:http://www.cnblogs.com/code-wangjun/p/4947719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!