zoukankan      html  css  js  c++  java
  • hihocoder——微软在线笔试T1

    声明:不知道会不会涉及保密协议,只是单纯记录一下自己的代码。

    输入一个字符串,和x y z三个整数

    将字符串字符逐个放入盒子,当前n个字符个数的差等于x y z时,前n个字符消失,再将剩余的字符放入盒子。

    求出盒子放过的最多个字符。

    (原题目是颜色不同的球入盒)

    eg:输入(“RRYBRBRYBRY”,4,1,2),输出7

    【思路】

    设计三个函数:

    1.3个数的差的绝对值是否等于给定的3个数——bool isvanish(int x, int y, int z, int a, int b, int c)

    2.字符串前几个字符可以消失——int isvanish(string s, int x, int y, int z)

    3.盒中放入最多的字符数

    【my code】

    (不完整,最后还得加个套子,略)

    bool isvanish(int x, int y, int z, int a, int b, int c)
    {
        int aa=abs(a-b);
        int bb=abs(b-c);
        int cc=abs(a-c);
        if(aa==x){
            if((bb==y&&cc==z)||(bb==z&&cc==y))
                return true;
        }
        if(aa==y){
            if((bb==x&&cc==z)||(bb==z&&cc==x))
                return true;
        }
        if(aa==z){
            if((bb==y&&cc==x)||(bb==x&&cc==y))
                return true;
        }
        return false;
    }
    int isvanish(string s, int x, int y, int z)
    {
        int count[3]={0};
        int c, i;
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='R') c=0;
            else if(s[i]=='Y') c=1;
            else if(s[i]=='B') c=2;
            count[c]++;
            if(isvanish(x, y, z, count[0], count[1], count[2]))
                return i+1;
        }
        return 0;
    }
    int longest(int vanish, int leftball)
    {
        return vanish>leftball?vanish:leftball;
    }

    【总结】

    要学会把功能分开实现,不要总是整体考虑。

    一部分一部分按步骤实现,有助于快速有效地A题。

  • 相关阅读:
    Mybatis专栏文章整理成册《Mybatis进阶》!!!
    Mybatis的几种传参方式,你了解吗?
    HDU 1890
    POJ 2186
    HDU 2896
    POJ 1322
    POJ 1276
    POJ 1208
    POJ 1189
    POJ 1178
  • 原文地址:https://www.cnblogs.com/ketchups-notes/p/4427856.html
Copyright © 2011-2022 走看看