输入描述:
一组或多组长度超过2的字符串。每组占一行
输出描述:
如果符合要求输出:OK,否则输出NG
上代码:
#include<iostream>
using namespace std;
string judge(string str)
{
if(str.length() <= 8)
return "NG";
int count1= 0 , count2 = 0, count3 = 0 , count4 = 0;
for(int i = 0 ; i < str.length() ; i++)
{
if(str[i] >= 'a' && str[i] <= 'z')
count1 = 1 ;
else if(str[i] >= 'A' && str[i] <= 'Z')
count2 = 1;
else if(str[i] >= '0' && str[i] <= '9')
count3 = 1;
else
count4 = 1;
}
int sum = count1+count2+count3+count4;
if(sum < 3)
return "NG";
int flag = 0;
for(int i = 0 ; i < str.size()-3 ; i++)
{
if(flag)
break;
for(int j = i+1 ; j < str.size()-2 ; j++)
{
string temp1 = str.substr(i,3);
string temp2 = str.substr(j,3);
if(temp1.compare(temp2) == 0)
{
flag = 1;
break;
}
}
}
if(flag)
return "NG";
else
return "OK";
}
int main()
{
string str;
while(cin >> str)
{
cout<< judge(str) << endl;
}
return 0;
}
思路分析:
这道题我是将判断完全写成了一个函数里;但是可以不可以多个函数嘞,抽时间写一下;写成两个函数去判断,通过最终判断返回值是否正常,来看返回是NG还是OK