Contest Print Server
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
输入
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.
输出
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
来源
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2624
/**************************************
***************************************
* 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;
}