1. 程式人生 > >Extraordinarily Tired Students UVA - 12108

Extraordinarily Tired Students UVA - 12108

簡單模擬特別困的學生,此刻我也挺困的昨晚寫到1點多沒過早上起來看了一下程式碼發現一個地方寫錯了,果然太晚的話自己的腦子思考的很差啊,

我是這樣的做的弄一個結構體,裡面存睡覺時間,清醒時間,狀態(0是清醒,1是睡覺)時間(t1表示還能清醒幾分鐘,t2表示還能睡幾分鐘)只要對時間做減然後特判時間為0的時候就可以

#include <iostream>

using namespace std;

struct node 
{
    int a,b;
    int c;
    int d;
    int t1;
    int t2;
}st[111];



int main()
{
    int n;
    int fg3=1;
    while(cin>>n&&n!=0)
    {
        for(int i=0;i<n;i++)
        {
            cin>>st[i].a>>st[i].b>>st[i].c;
            int cont=st[i].c%(st[i].a+st[i].b);
            if(cont==0)
            {
                st[i].d=1;
                st[i].t1=0;
                st[i].t2=1;
            }
            else
            {
                if(cont>st[i].a)
                {
                    st[i].d=1;
                    st[i].t1=0;
                    st[i].t2=(st[i].a+st[i].b)-cont+1;
                }
                else
                {
                    st[i].d=0;
                    st[i].t2=0;
                    st[i].t1=st[i].a-cont+1;
                }
                    
            }
        }
        int i;
        int fg1=0;
        for(i=1;i<100000;i++)
        {
            int fg0=0;
            int a=0;
            int b=0;
            for(int j=0;j<n;j++)
            {
                if(st[j].d==0)
                    a++;
                else
                    b++;
            }
            if(a==n)
            {
                fg1=1;
                break;
            }
            for(int j=0;j<n;j++)
            {
                if(st[j].d==0)
                {
                    if(st[j].t1==0)
                    {
                        if(a>=b)
                        {
                            st[j].d=0;
                            st[j].t1=st[j].a;
                            //st[j].t1--;
                        }
                        else
                        {
                            st[j].d=1;
                            st[j].t2=st[j].b;
                            st[j].t2--;
                        }
                    }
                    else
                        st[j].t1--;
                }
                else
                {
                    if(st[j].t2==0)
                    {
                        st[j].d=0;
                        st[j].t1=st[j].a;
                        st[j].t1--;
                    }
                    else
                        st[j].t2--;
                }
            }
        }
        printf("Case %d: ",fg3++);
        if(!fg1)
            cout<<-1<<endl;
        else
            cout<<i-1<<endl;
    }
    return 0;
}