题目一:IP测试
#include#include #include #include using namespace std; //对一个可能的数进行判断 int TESTIP(string s) { if(s.empty()||s.size()>3) return 0; if(s[0]=='0'&&s.length()>1) return 0; //将s转化为c风格字符串后转化为数字 int num=atoi(s.c_str()); if( num>=0 && num<=255 ) return 1; return 0; } //从start开始将s划分为num部分,结果存入result void IP(string &s,int start,int num,vector &re,string &str) { if(num==0) { str.pop_back(); if(str.size()==s.size()+3) { re.push_back(str); } return; } int len=str.length(); int i; //划分出第一位可能的数 for(i=1; i<=3&&start+i<=(int)s.length(); i++) { string ss=s.substr(start,i); if(!TESTIP(ss)) continue; //若合适则添加到str后面,并继续划分剩下的数 str=str+ss+'.'; IP(s,start+i,num-1,re,str); str.erase(len,i+1); } }int main(){ string s; cin>>s; vector result; string str; IP(s,0,4,result,str); sort(result.begin(),result.end()); for(unsigned int i=0; i >s;}
题目二:统计输入字母
#include#include #include #include #include