uva - 12108-Extraordinarily Tired Students

来源:互联网 发布:linux 远程登录oracle 编辑:程序博客网 时间:2024/06/05 17:03

由于数据范围很小,所以是一个纯模拟题,没什么技术含量,需要注意的时候如果十年到了 所有 周期相乘的那个数还没跳出循环,就说明没有正解。


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define MAXD 10 + 5struct Student{    int A;   /*醒A分钟*/    int B;   /*睡B分钟*/    int C;   /*当前处于的周期时间*/    int D;   /*周期长度*/    int is_sleep; /*是不是正在睡觉*/}student[MAXD];int n;void display(){    printf("======================\n");    for(int i = 0 ; i < n ; i++)    printf("%d %d %d %d\n",student[i].A,student[i].B,student[i].C,student[i].is_sleep);}int main(){    int Case = 1;    while(scanf("%d",&n) && n){        int sleep = 0,awake = 0;  /*睡觉的人数,清醒的人数*/        int ALL = 1;        for(int i = 0 ; i < n ; i ++){          scanf("%d%d%d",&student[i].A,&student[i].B,&student[i].C);           student[i].D = student[i].A + student[i].B;           if(student[i].C <= student[i].A){           awake ++;           student[i].is_sleep = 0;           }           else{           student[i].is_sleep = 1;           sleep ++;           }           ALL *= student[i].D;        }        int time ; /*当前时间*/        int ok  = 0;        for(time = 1 ; time <= ALL ; time ++){            if(awake == n) {            ok = 1;            break;            }            int t_sleep = sleep ,t_awake =awake;            for(int i = 0 ; i < n ; i++){                student[i].C ++;                if(student[i].C == (student[i].D + 1)){                    student[i].C = 1;                    student[i].is_sleep = 0;                    awake ++;                    sleep --;                }                else if((!student[i].is_sleep) && (student[i].C == student[i].A + 1)){                    if(t_sleep > t_awake){                        student[i].is_sleep = 1;                        awake --;                        sleep ++;                    }                    else                       student[i].C = 1;                }            }        }        if(ok)        printf("Case %d: %d\n",Case++,time);        else        printf("Case %d: -1\n",Case++);    }    return 0;}


0 0
原创粉丝点击