题目1 : Magic Box
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
The circus clown Sunny has a magic box. When the circus is performing, Sunny puts some balls into the box one by one. The balls are in three colors: red(R), yellow(Y) and blue(B). Let Cr, Cy, Cb denote the numbers of red, yellow, blue balls in the box. Whenever the differences among Cr, Cy, Cb happen to be x, y, z, all balls in the box vanish. Given x, y, z and the sequence in which Sunny put the balls, you are to find what is the maximum number of balls in the boxever.
For example, let's assume x=1, y=2, z=3 and the sequence is RRYBRBRYBRY.
After Sunny puts the first 7 balls, RRYBRBR, into the box, Cr, Cy, Cb
are 4, 1, 2 respectively. The differences are exactly 1, 2, 3.
(|Cr-Cy|=3, |Cy-Cb|=1, |Cb-Cr|=2) Then all the 7 balls vanish. Finally
there are 4 balls in the box, after Sunny puts the remaining balls. So
the box contains 7 balls at most, after Sunny puts the first 7 balls and
before they vanish.
输入
Line 1: x y z
Line 2: the sequence consisting of only three characters 'R', 'Y' and 'B'.
For 30% data, the length of the sequence is no more than 200.
For 100% data, the length of the sequence is no more than 20,000, 0 <= x, y, z <= 20.
输出
The maximum number of balls in the boxever.
样例输入
1 2 3
RRYBRBRYBRY
样例输出
7
—————————哥哥哥—————————————
为什么投设计也要做题啊啊啊啊!
而且开考前一小时才收到补发的邮件,才看到是考编程,用C/C++/Java/C#做
我的C已经还给妈妈了啊啊啊啊
好吧,一边查书查百度一边做……
我的电脑还没搭C的编译环境,于是用在线编译器http://ideone.com测试……
—————————哥哥哥—————————————
这一题死活编译不顺利!
输入样例,就是给我出来0!0你妹啊!!用printf大法查来查去,发现abc的值一直只有c是1,其他两个都是0,好像没有读入数据啊?
后来截止时间前20秒(是的!就是这么炫酷心跳!)才发现一个大蠢bug!我c本来是计算某个颜色的小球数量的,却被我在前面的循环里用去做字符的输入!!变量冲突,难怪读不出来……改改改,只来得及改成这样——
注意加粗的if里的c……忘了改……就这么提交了……泪
#includeint main(void) {
int n,x,y,z,a,b,c,m;
n=m=a=b=c=0;
while(scanf("%d%d%d", &x, &y,&z) != EOF) {
while (e=getchar() != EOF){
if (c=='R')
a+=1;
else if (c=='B')
b+=1;
else if (c=='Y')
c+=1;
n+=1;
if( ( x==abs(a-b) && y==abs(b-c) && z==abs(a-c) ) || ( x==abs(a-b) && z==abs(b-c) && y==abs(a-c) ) || ( y==abs(a-b) && x==abs(b-c) && z==abs(a-c) ) || ( y==abs(a-b) && z==abs(b-c) && x==abs(a-c) ) || ( z==abs(a-b) && x==abs(b-c) && y==abs(a-c) ) || ( z==abs(a-b) && y==abs(b-c) && x==abs(a-c) ) ) {
a=b=c=0;
if(n>m) {
m=n;
n=0;
}
}
}
printf("%d\n", m);
}
return 0;
}
截止后还是死活想把它弄出来,明明只差一点!
先就是把if里的c都改成e,然后编辑错,加个char e;就好了。然后printf大发这回告诉我循环了12次abc每次都是0……why……明明我改了e了为何还是读不进去。
后来翻书,试试第二行还是用scanf输入而不用getchar,果然……这回有了变化!除了m是8比正确答案多循环了一次外都对了!这回学到了,下次有一长串东西应该用scanf这个标准化输入函数……
最后,多出一次循环是因为多读了一个回车,于是找到解决方法如下:
(1)用getchar()清除
(2)用"%1s"来限制
(3)用格式串中空格或"%*c"来"吃掉"
(4)用函数fflush(stdin)清除全部剩余内容
加个最简单的getchar(),终于在闭卷半小时后调出来啦!
在线编译器果然好慢……折腾死……
以下是正确的代码(多保留了几个printf,实际只需要最后的printf("%d\n", m);):
#includeint main(void) {
int n,x,y,z,a,b,c,m;
char e;
n=m=a=b=c=0;
while(scanf("%d%d%d", &x, &y,&z) != EOF) {
getchar();
while(scanf("%c", &e) != EOF) {
if (e=='R')
a+=1;
else if (e=='B')
b+=1;
else if (e=='Y')
c+=1;
n+=1;
printf("第n次%d%d%d%d\n%d\n%d\n%d\n%d\n", m,x,y,z,a,b,c,n);
if( ( x==abs(a-b) && y==abs(b-c) && z==abs(a-c) ) || ( x==abs(a-b) && z==abs(b-c) && y==abs(a-c) ) || ( y==abs(a-b) && x==abs(b-c) && z==abs(a-c) ) || ( y==abs(a-b) && z==abs(b-c) && x==abs(a-c) ) || ( z==abs(a-b) && x==abs(b-c) && y==abs(a-c) ) || ( z==abs(a-b) && y==abs(b-c) && x==abs(a-c) ) ) {
a=b=c=0;
if(n>m) {
m=n;
n=0;
printf("%d%d%d%d\n%d\n%d\n%d\n%d\n", m,x,y,z,a,b,c,n);
}
}
}
printf("%d%d%d%d\n%d\n%d\n%d\n%d\n", m,x,y,z,a,b,c,n);
}
return 0;
}
http://ideone.com/TCg5LN