Peter Paul and Mary

#include <iostream>

#include<fstream>

using namespace std;

ifstream infile;



int main()

{

    int a[10],b[10],c[10];

    int noOfCase;

    infile.open("1.txt");

    void set(int [],int [],int []);

    void standard(int [],int [],int []);

    int meet(int,int,int,int);

    void schedule(int [],int [],int []);



    infile>>noOfCase;

    int i = noOfCase;

    while(noOfCase!=0)

    {

        set(a,b,c);

        standard(a,b,c);

        cout<<"Case "<<i-noOfCase+1<<": ";

        schedule(a,b,c);

        noOfCase--;

    }

    infile.close();

    return 0;

}

void set(int a[],int b[],int c[])

{

    for(int i=0;i<10;i++)

    {

        infile>>a[i];

    }

    for(int i=0;i<10;i++)

    {

        infile>>b[i];

    }

    for(int i=0;i<10;i++)

    {

        infile>>c[i];

    }

    /*for(int i=0;i<10;i++)

    {

        cout<<a[i]<<" ";

    }

    cout<<endl;

    for(int i=0;i<10;i++)

    {

        cout<<b[i]<<" ";

    }

    cout<<endl;

    for(int i=0;i<10;i++)

    {

        cout<<c[i]<<" ";

    }

    cout<<endl;*/

}

void standard(int a[],int b[],int c[])

{

    for(int i=0;i<10;i++)

    {

        if(a[i]<6)

        {

            a[i]+=12;

        }

        if(b[i]<6)

        {

            b[i]+=12;

        }

        if(c[i]<6)

        {

            c[i]+=12;

        }

    }

    /*for(int i=0;i<10;i++)

    {

        cout<<a[i]<<" ";

    }

    cout<<endl;

    for(int i=0;i<10;i++)

    {

        cout<<b[i]<<" ";

    }

    cout<<endl;

    for(int i=0;i<10;i++)

    {

        cout<<c[i]<<" ";

    }*/

    //cout<<endl;

}

int meet(int a,int b,int c,int d)    //在编写这个函数时最好配合画图,把每一种情况用途裂化出来,不要漏掉任何一种情况

{

    if(b<=c || d<=a)//a,b is seperate from c,d

    {

        return 0;

    }

    else if(c>=a && d<=b)//c,d are withwin a,b

    {

        return (d-c);

    }

    else if(c>=a && c<=b && d>=b)//overlap and c,d follow a,b

    {

        return (b-c);

    }

    else if(c<=a && d>=a && d<=b)//overlap and a,b follow c,d

    {

        return (d-a);

    }

    else//a>=c b<=d, that is, a,b are within c,d

    {

        return (b-a);

    }

}

void schedule(int a[], int b[], int c[])

{

    int hour[3] = {1,2,3};

    int plan[3] = {-1};

    int i = 0;

    int j;

    for (int n=0; n<3; n++)

    {

        j = 0;

        for (; i<9; i=(i+2)%10, j++)//i=0,2,4,6,8

        {

            /*cout << "hour[n]" << hour[n] << endl;

            cout << meet(a[i], a[i+1], b[i], b[i+1]) << endl;

            cout << meet(b[i], b[i+1], c[i], c[i+1]) << endl;

            cout << meet(a[i], a[i+1], c[i], c[i+1]) << endl;*/

            if (meet(a[i], a[i+1], b[i], b[i+1])>=hour[n]&&

                meet(b[i], b[i+1], c[i], c[i+1])>=hour[n]&&

                meet(a[i], a[i+1], c[i], c[i+1])>=hour[n])

                {

                    plan[n] = i/2;

                    //n++;          //这里不用让n自增,因为跳出内层for循环后会执行外层循环中的n++

                    i=(i+2)%10;

                    break;

                }

            if (j == 4)

            {

                if (plan[n] == -1)//plan[n]=-1,没有安排时间,则说明不可安排

                {

                    cout << "The meetings cannot be scheduled." << endl;

                    return;

                }

            }

        }

    }

    switch (plan[0]+1)

    {

        case 1:cout<<"Monday";break;

        case 2:cout<<"Tuesday";break;

        case 3:cout<<"Wednesday";break;

        case 4:cout<<"Thursday";break;

        case 5:cout<<"Friday";

    }

    cout<<" for 1 hour,";

    cout<<" then ";

    switch (plan[1]+1)

    {

        case 1:cout<<"Monday";break;

        case 2:cout<<"Tuesday";break;

        case 3:cout<<"Wednesday";break;

        case 4:cout<<"Thursday";break;

        case 5:cout<<"Friday";

    }

    cout<<" for 2 hours,";

    cout<<" then ";

    switch (plan[2]+1)

    {

        case 1:cout<<"Monday";break;

        case 2:cout<<"Tuesday";break;

        case 3:cout<<"Wednesday";break;

        case 4:cout<<"Thursday";break;

        case 5:cout<<"Friday";

    }

    cout<<" for 3 hours."<<endl;

}



你可能感兴趣的:(ul)