JXUFE紫书第四章习题 4-8

When a student is too tired, he can’t help sleeping in class, even if his favorite teacher is right here infront of him. Imagine you have a class of extraordinarily tired students, how long do you have to wait,before all the students are listening to you and won’t sleep any more? In order to complete this task,you need to understand how students behave.When a student is awaken, he struggles for a minutes listening to the teacher (after all, it’s too badto sleep all the time). After that, he counts the number of awaken and sleeping students (includinghimself). If there are strictly more sleeping students than awaken students, he sleeps for b minutes.Otherwise, he struggles for another a minutes, because he knew that when there is only very few sleepingstudents, there is a big chance for them to be punished! Note that a student counts the number ofsleeping students only when he wants to sleep again.Now that you understand each student could be described by two integers a and b, the length ofawaken and sleeping period. If there are always more sleeping students, these two periods continueagain and again. We combine an awaken period with a sleeping period after it, and call the combinedperiod an awaken-sleeping period. For example, a student with a = 1 and b = 4 has an awaken-sleepingperiod of awaken-sleeping-sleeping-sleeping-sleeping. In this problem, we need another parameter c(1 ≤ c ≤ a + b) to describe a student’s initial condition: the initial position in his awaken-sleepingperiod. The 1st and 2nd position of the period discussed above are awaken and sleeping, respectively.Now we use a triple (a, b, c) to describe a student. Suppose there are three students (2, 4, 1), (1, 5,2) and (1, 4, 3), all the students will be awaken at time 18. The details are shown in the table below.Table 1. An exampleWrite a program to calculate the first time when all the students are not sleeping.InputThe input consists of several test cases. The first line of each case contains a single integer n (1 ≤ n ≤10), the number of students. This is followed by n lines, each describing a student. Each of these linescontains three integers a, b, c (1 ≤ a, b ≤ 5), described above. The last test case is followed by a singlezero, which should not be processed.OutputFor each test case, print the case number and the first time all the students are awaken. If it’ll neverhappen, output ‘-1’.Sample Input32 4 11 5 21 4 331 2 11 2 21 2 30Sample OutputCase 1: 18Case 2: -1


中文题意:

每个学生(1<=n<=10)存在一个awake-sleep周期,当这个学生到awake的最后一刻时,他要判断当前睡觉和醒的学生的人数,如果睡觉的人数绝对大于醒着的人数,那么他要继续保持清醒a分钟,否则就进入睡觉状态。

给出n个学生的清醒周期和睡觉周期以及初始时刻处于周期的时刻,判断什么时刻全班同学都清醒着,如果不存在这样的时刻,那么就输出-1.

分析:只需要计算每个时刻全班的清醒人数和睡觉人数:更新每位同学的状态。然后判断是否awake人数==n即可。

尝试:

①用结构体

②对每个时刻的状态判断

#include <stdio.h>  
#include <string.h>  
#define maxn 15  
struct STUDENT {  
    int A;//清醒A分钟  
    int B;//睡B分钟  
    int C;//当前处于的周期时间C分钟  
    int D;//周期总时间  
    int is_sleep;//当前时间是不是睡着  
}st[maxn];  
int cases = 0, n;  
int main()  
{  
    //freopen("in.txt", "r", stdin);  
    //freopen("out.txt", "w", stdout);  
    while (~scanf("%d", &n) && n)  
    {  
        int ALL = 1;
        int awake = 0, sleep = 0;
        for (int i = 0;i < n;i++)  
        {  
            scanf("%d%d%d", &st[i].A, &st[i].B, &st[i].C);  
            st[i].D = st[i].A + st[i].B;  
            if (st[i].C <= st[i].A)  
            {  
                st[i].is_sleep = 0;  
                awake++;  
            }  
            else  
            {  
                st[i].is_sleep = 1;  
                sleep++;  
            }  
            ALL *= st[i].D;  
        }  
        int time;
        int flag=0;  
        for (time = 1;time <= ALL;time++)  
        {  
            if (awake == n)  
            {  
                flag = 1;  
                break;  
            }  
            int pre_sleep = sleep;
            int pre_awake = awake;  
            for (int i = 0;i < n;i++)  
            {  
                st[i].C++;
                if (st[i].C == (st[i].D + 1))
                {  
                    st[i].C = 1;  
                    st[i].is_sleep = 0;  
                    sleep--;  
                    awake++;  
                }  
                else if (st[i].C == (st[i].A + 1))
                {  
                    if (pre_sleep > pre_awake)  
                    {
                        st[i].is_sleep = 1;  
                        sleep++;  
                        awake--;  
                    }  
                    else st[i].C = 1;  
                }  
            }  
        }  
        if (flag) printf("Case %d: %d\n", ++cases, time);  
        else printf("Case %d: %d\n", ++cases, -1);  
    }  
    return 0;  
}  

猜你喜欢

转载自blog.csdn.net/jxufe_acmer/article/details/80341965
4-8