[2013山东省第四届ACM大学生程序设计竞赛]——Contest Print Server

Contest Print Server

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

    In ACM/ICPC on-site contests ,3 students share 1 computer,so you can print your source code any time. Here you need to write a contest print server to handle all the requests.

输入

In each case,the first line contains 5 integers n,s,x,y,mod (1<=n<=100, 1<=s,x,y,mod<=10007), and n lines of requests follow. The request is like "Team_Name request p pages" (p is integer, 0<p<=10007, the length of "Team_Name" is no longer than 20), means the team "Team_Name" need p pages to print, but for some un-know reason the printer will break down when the printed pages counter reached s(s is generated by the function s=(s*x+y)%mod ) and then the counter will become 0. In the same time the last request will be reprint from the very begin if it isn't complete yet(The data guaranteed that every request will be completed in some time).
    You can get more from the sample.

输出

    Every time a request is completed or the printer is break down,you should output one line like "p pages for Team_Name",p is the number of pages you give the team "Team_Name".

    Please note that you should print an empty line after each case.

示例输入

2
3 7 5 6 177
Team1 request 1 pages
Team2 request 5 pages
Team3 request 1 pages
3 4 5 6 177
Team1 request 1 pages
Team2 request 5 pages
Team3 request 1 pages

示例输出

1 pages for Team1
5 pages for Team2
1 pages for Team3
 
 
 
 
1 pages for Team1
3 pages for Team2
5 pages for Team2
1 pages for Team3

来源

 2013年山东省第四届ACM大学生程序设计竞赛

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2624


这道题是我看的第一道题,大体扫了一眼,发现跟第三届的 手机屏幕 那道题类似。
于是就交给 昊 做了(上次手机屏幕那个就是他做的)。
题意很好懂,就是ACM比赛中,可以用打印功能。
但是打印的纸张不可能是无限多的,纸张的数量会按照一个函数添加。
(函数就是  s=(s*x+y)%mod )
所以,打印机可能出现没纸的情况。
这时候就要重新打印了。
这道题目就是要模拟这个过程。

其实,化繁为简就是:
相当于向一个容器添加东西,填满就清空它,并且容器大小改变。

思路,跟本谈不上,只是有几点要注意:
①如果纸刚好用完,下一个队伍要首先输出0,再向下判断。
②如果s为0,则再次更新s
③每组测试数据后面都要跟一个空行,这是after 不是 between case

这道题,由于之前刚用过strstr,于是我觉得可以用strstr来处理输入。
但是strstr得到的是指针,要求长度就要减去首地址。
然后在赋值完毕后,相应的末尾要加 '\0' (因为这个我 WA N次)
其实,还有更简单的scanf处理,一行搞定。(PS:scanf太强大了。。膜拜ING)

/************************************** 
*************************************** 
*        Author:Tree                  * 
*From :http://blog.csdn.net/lttree    * 
* Title : Contest Print Server        * 
*Source: 第四届山东省ACM比赛         * 
* Hint  : 模拟                        * 
*************************************** 
**************************************/  
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int s,x,y,mod;
struct Team
{
    char name[21];
    int page;
}team[101];
// s的更新函数
void update_s( void )
{
    s=(s*x+y)%mod;
    if( s==0 )  update_s();
}
int main()
{
    int t,n,i,j,len_req,temp;
    char str[50],req[8]="request";
    int sum;
    cin>>t;
    while( t-- )
    {
        cin>>n>>s>>x>>y>>mod;
        // 为下面gets将回车先收掉
        getchar();
        for( i=0 ;i<n ;++i )
        {

            gets(str);
            // strstr得到 request中r在 str指针,减去str首地址得到长度
            len_req=strstr(str,req)-str;
            // 赋值
            strncpy(team[i].name,str,len_req-1);
            // ★千万不要忘记!
            team[i].name[len_req-1]='\0';
            
            // 这些是为了存数字,用temp防止是多位数
            temp=0;
            j=len_req+8;
            while( str[j]>='0'  && str[j]<='9' )
            {
                temp=temp*10+str[j]-'0';
                ++j;
            }
            team[i].page=temp;
            
            // 用下面这一行就可以代替上面for循环的内容,但是同时要将for循环之前的getchar注释掉
//            scanf("%s request %d pages", team[i].name, &team[i].page);
        }

        sum=0;
        for( i=0;i<n;++i )
        {
            if( sum+team[i].page > s )
            {
                cout<<s-sum<<" pages for "<<team[i].name<<endl;
                sum=0;
                update_s();
                --i;
            }
            else
            {
                cout<<team[i].page<<" pages for "<<team[i].name<<endl;
                sum+=team[i].page;
            }
        }
        cout<<endl;
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值