zoukankan      html  css  js  c++  java
  • UVA

    /*
      有效使用结构体,会很简单,否则很难想
      
      BTW,发现有个博主的写法,真是简单又易懂,他在struct中放了布置3个数据,并且还有个专门用来更新,当前时刻位于周期中哪个位置,的函数
      
      总之是很不错的写法,于是按照他的思路,修改了自己的冗长代码,blog:
      
      http://blog.csdn.net/shannonnansen/article/details/42708257
      
      这题的WA点:
      //刚开始写时,忘记了在重载输入的student变量前加引用,导致用重载输出输出的时候,发现输出的全都是0...看来基础的语法真是要时不时回顾一下,分明上学期才学过,居然忘了重载输入要加引用这么重要的知识点 T^T 
    */



    #include <iostream>
    #include <cstdio>
    using namespace std;
    int n;
    struct student
    {
    	int  a, b, c, period;
    	
    	bool reinit(int d)
    	{
    		return (c == a && d >= n - d) || (c == period); //想睡觉但班级睡觉的人太少,进入下一个等待周期;或已经睡完b分钟起来,进入新一轮周期 
    	}
    } s[15];
    const int MAX_TIME = 10000;
    
    istream& operator >> (istream&in, student& t) 
    {
    	in >> t.a >> t.b >> t.c; 
    	t.period = t.a + t.b;
    	return in;
    }
    
    ostream& operator << (ostream&out, const student& t)
    {
    	out << t.a << " " << t.b << " " << t.c << endl;
    	return out;
    }
    int getAwake ();
    
    int main()
    {
    	cin.tie(0);
    	cin.sync_with_stdio(false);
    	
    	int k = 0, i, j;
    	while (cin >> n && n)
    	{
    		k++;
    		for (i = 0; i < n; i++) cin >> s[i];
    //		for (i = 0; i < n; i++) cout << s[i];
    		
    		for (i = 1; i < MAX_TIME; i++)
    		{
    			int count;
    			if ((count = getAwake()) == n) break;
    			
    			for (j = 0; j < n; j++)
    			if (s[j].reinit(count)) s[j].c = 1;
    			else s[j].c++;
    		}
    		cout << "Case " << k << ": " << (i < MAX_TIME ? i : -1) << endl;
    	}
    	return 0;
    }
    
    int getAwake ()
    {
    	int ans = 0;
    	for (int i = 0; i < n; i++)
    	if (s[i].c <= s[i].a) ans++;
    	return ans;
    }


  • 相关阅读:
    Vue(小案例_vue+axios仿手机app)_go实现退回上一个路由
    nyoj 635 Oh, my goddess
    nyoj 587 blockhouses
    nyoj 483 Nightmare
    nyoj 592 spiral grid
    nyoj 927 The partial sum problem
    nyoj 523 亡命逃窜
    nyoj 929 密码宝盒
    nyoj 999 师傅又被妖怪抓走了
    nyoj 293 Sticks
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789472.html
Copyright © 2011-2022 走看看