题解 | #识别有效的IP地址和掩码并进行分类统计#

密码验证合格程序

http://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

C++,前面两个条件直接判断,第三个条件用set和vector判断

#include <bits/stdc++.h>
using namespace std;

int juge_char(const string &str,int k)
{
     int m[4]={0,0,0,0},num=0;
    for(int i=0;i<k;i++)
    {
        if(str[i]>='A'&&str[i]<='Z')
        {
            m[0]=1;
        }
        else if(str[i]>='a'&&str[i]<='z')
        {
            m[1]=1;
        }
        else if(str[i]>='0'&&str[i]<='9')
        {
            m[2]=1;
        }
        else
        {
            m[3]=1;
        }
    }
    for(int j=0;j<4;j++)
    {
        if(m[j]==1)
        {
            num++;
        }
    }
    if(num>=3)
    {
        return 1;
    }
    else
    {
        return 0;
    }

}
int main()
{
    int nsize=0,juge=0;
    string subs;
    vector<string> sub1;
    set<string> sub2;
    string taget;
    string k="OK";
    string G="NG";
    while(cin>> taget)
    {
        nsize=taget.size();
        if(nsize<=8)
        {
            cout<<G<<endl;               //条件一判断
        }
        else
        {
            juge=juge_char(taget, nsize);    //条件二判断
            if(juge)
            {
                for(int i=0;i<nsize-2;i++)
                {
                    subs=taget.substr(i,3);//从i开始拷贝3字符串到subs
                    sub1.push_back(subs);
                    sub2.insert(subs);
                }
                if(sub1.size()==sub2.size()) //条件三判断
                {
                    cout<<k<<endl;  
                }
                else
                {
                    cout<<G<<endl;  
                }
                sub1.clear();//用完清零,供下一次使用
                sub2.clear();
            }
            else
            {
              cout<<G<<endl;  
            }

        }
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务