微软2016校园招聘在线笔试——第一题

题目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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 参与eos的ico后,如何认领eos代币视频,为什么做这个视频教程,是因为eos的ico的代币不是自动发放的,是需...
    刀哥BTCACCN阅读 3,754评论 0 1
  • 嘉嘉,一个让我喜欢让我忧的女孩儿! 嘉嘉第一次来到我身边,是在二三班的小豆包学习了一个学期后的事了。过完年新学期伊...
    清浅光阴阅读 3,262评论 0 4
  • 我是一枚红豆 我匀圆万颗相似 引多少人暗数成痴 我嵌入玲珑骰子 引多少人入骨相思 古往今来 我已承载了太多的 诗情...
    静铃音阅读 2,444评论 22 23
  • 今天太累了,就把这个当打卡吧,也和小伙伴们分享一下今天的心得。〈每日一问〉昨天有心友私信老师,问为什么大家有体悟这...
    田心远阅读 1,228评论 0 0