2015.09.13 网易游戏在线笔试(运营开发工程师)
题目一:IP测试
#include<iostream> #include<string> #include<vector> #include<algorithm> 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<string> &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<string> result; string str; IP(s,0,4,result,str); sort(result.begin(),result.end()); for(unsigned int i=0; i<result.size(); i++) cout<<result[i]<<endl; // cin>>s; }
题目二:统计输入字母
#include <iostream> #include <string> #include <sstream> #include <vector> #include <map> #include <algorithm> //#include <omp.h> using namespace std; //构造结构体,重载函数调用运算符为比较函数 struct word { string s; int i; bool operator()(const word &a, const word &b) { if (a.i != b.i) return a.i > b.i; else return a.s < b.s; } }; //或者新建比较函数 bool compare(word a, word b) { if (a.i != b.i) return a.i > b.i; else return a.s < b.s; } int main() { //将输入读取到str int n; cin>>n; string s, line; vector<string> str; while(n+1) { char line[100]; gets(line); istringstream stream(line); while (stream>>s) str.push_back(s); n--; } //建立结构体向量 vector<word> v; v.clear(); for (int i=0; i<str.size(); i++) { bool f = true; //表示是否需要新建 for (int j=0; j<v.size(); j++) { if (v[j].s == str[i]) { v[j].i++; f = false; } } if (f) { word *w = new word; w->s = str[i]; w->i = 1; v.push_back(*w); } } sort(v.begin(), v.end(), compare); for (int i=0; i<v.size(); i++) cout << v[i].s << ' ' << v[i].i << endl; // getchar(); return 0; }