[原]sdut2624 Contest Print Server (大水+大坑)山东省第四届ACM省赛

本文出自:http://blog.csdn.net/svitter

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


题意:为什么每次都是我做这么坑爹的题目TAT

一开始的名字我在想名字有没有空格,就像是之前

Sdut2411 Pixel density 山东省第三届ACM省赛(输入输出字符串处理)

这道题目,以至于第一篇WA我根本想不出到底是name的问题还是其他方面的问题。后来证明果然是没读懂题意,哎,坑爹啊。


给你五个数 n,s, x,  y , mod,然后输入n行数据,以Team_name +"request"+num+"pages."的形式给你。

然后就出现坑了。

这句话。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).

我的理解是,每当数量达到s,那么调用生成函数生成新的s,如果之前的没有打印完,那么重新全部打印出来。

但是事实上,是超过s的话,那么。。。。。。下面是我的AC代码,if前面的注释就是我先前的代码,问题就出在只要到达s(正好等于也算),那么就更新s。= =然后我就呵呵的WA了。(这两个的差距就在于是否多输出一个0.)

- -结合实践的话确实也能想明白- -就是你正好打完了你还更新s干什么。。。

下面是两个思路的AC代码:

//============================================================================
// Name        : 省赛字符串.cpp
// Author      : Vit
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

struct Team
{
	char name[21];
	int num;
};

Team te[102];

int n, s, x, y, mod;

void update(int &s)
{
	s = ((s * x) % mod + y % mod) % mod;
	if(s == 0)
        update(s);
}
void print(int i)
{
	printf("%d pages for %s
", te[i].num, te[i].name);
}
void print(int i, int num)
{
	printf("%d pages for %s
", num, te[i].name);
}

void ace()
{
	//work point
	int i, t;
	//num;
	//freopen("test", "r", stdin);
	scanf("%d", &t);
	while (t--)
	{
		//input data
		scanf("%d%d%d%d%d", &n, &s, &x, &y, &mod);
		for (i = 0; i < n; i++)
		{
			scanf("%s request %d pages", te[i].name, &te[i].num);
		}

		int sum = 0;

		//handle data
		for (i = 0; i < n; i++)
		{
			sum += te[i].num;
//			if(sum == s)
//			{
//                print(i)
//                update(s);
//                sum = 0;
//            }
//            else
            if(sum > s)
            {
                print(i, te[i].num + (s - sum));
                update(s);
                sum = 0;
                i--;
            }
            else
            {
                print(i);
            }
		} //end of i
		printf("
");
	} // end of t;
}

int main()
{
	ace();
	return 0;
}

第二种:

//============================================================================
// Name        : 省赛字符串.cpp
// Author      : Vit
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

struct Team
{
	char name[21];
	int num;
};

Team te[102];

int n, s, x, y, mod;

void update(int &s)
{
	s = ((s * x) % mod + y % mod) % mod;
	if(s == 0)
        update(s);
}
void print(int i)
{
	printf("%d pages for %s
", te[i].num, te[i].name);
}
void print(int i, int num)
{
	printf("%d pages for %s
", num, te[i].name);
}

void ace()
{
	//work point
	int i, t;
	//num;
	//freopen("test", "r", stdin);
	scanf("%d", &t);
	while (t--)
	{
		//input data
		scanf("%d%d%d%d%d", &n, &s, &x, &y, &mod);
		for (i = 0; i < n; i++)
		{
			scanf("%s request %d pages", te[i].name, &te[i].num);
		}

		int sum = s;

		//handle data
		for (i = 0; i < n; i++)
		{
			if(sum >= te[i].num)
			{
                print(i);
                sum -= te[i].num;
			}
			else if(sum < te[i].num)
			{
                print(i, sum);
                update(s);
                sum = s;
                i--;
			}
		} //end of i
		printf("
");
	} // end of t;
}

int main()
{
	ace();
	return 0;
}


作者:svitter 发表于2014-5-4 17:12:46 原文链接
阅读:200 评论:1 查看评论
原文地址:https://www.cnblogs.com/svitter/p/3724318.html