去哪儿网2015校招研发类笔试题

从网上找到的题目,自己做了一遍

1、二分查找

2、给定一个字符串,得到这个字符串中首先出现两次的那个字符

方法:可以用一个hash_map或者数组来存储字符出现的次数,一旦有一个出现了2次,就返回该字符

3、尝试在以下文本中搜索并打印出包含单词"your"(不区分大小写)的句子,并按照出现次数从高到低排序 

Make yourself at home
None of your business
I will be more careful
 How about going to a move?
Your life is your own affair

方法:统计每行中your出现的次数,然后 进行排序

代码:

  1 #include<iostream>
  2 #include<hash_map>
  3 #include<vector>
  4 #include<string>
  5 #include<sstream>
  6 #include<algorithm>
  7 using std::vector;
  8 using std::string;
  9 using std::cout;
 10 using std::cin;
 11 using std::endl;
 12 using std::hash_map;
 13 using std::istringstream;
 14 //题目1:二分查找
 15 int bin_search(int num[], int start, int end, int n);
 16 int bin_search(int num[],int length,int n)
 17 {
 18     if (num==0||length<=0)
 19     {
 20         return false;
 21     }
 22 
 23     return bin_search(num, 0, length - 1, n);
 24 }
 25 
 26 int bin_search(int num[],int start,int end,int n)
 27 {
 28     if (num==NULL||start<0||end<start||end<0)
 29     {
 30         throw std::exception("Invalid input");
 31     }
 32 
 33     if (start==end)
 34     {
 35         if (num[start] == n)
 36         {
 37             return start;
 38         }
 39         else
 40             return -1;
 41     }
 42 
 43     int mid = start + (end - start) / 2;
 44     if (num[mid]==n)
 45     {
 46         return mid;
 47     }
 48     else if (num[mid]<n)
 49     {
 50         return bin_search(num, mid + 1, end, n);
 51     }
 52     else
 53     {
 54         return bin_search(num, start, mid - 1, n);
 55     }
 56 }
 57 
 58 //题目2:字符串中第一次出现2次的字符
 59 
 60 char occur2times(char* ch)
 61 {
 62     char word = '\0';
 63     if (ch==NULL)
 64     {
 65         return word;
 66     }
 67 
 68     std::size_t length = strlen(ch);
 69     std::hash_map<char, int> hashmap;
 70     for (int i = 0; i < length;++i)
 71     {
 72         std::hash_map<char, int>::const_iterator itr= hashmap.find(ch[i]);
 73         if (itr==hashmap.cend())
 74         {
 75             hashmap.insert(std::make_pair(ch[i], 1));
 76         }
 77         else
 78         {
 79             word = itr->first;
 80             break;
 81         }
 82     }
 83 
 84     return word;
 85 }
 86 
 87 /*题目3:
 88 尝试在以下文本中搜索并打印出包含单词"your"(不区分大小写)的句子,并按照出现次数从高到低排序
 89 Make yourself at home
 90 None of your business
 91 I will be more careful
 92 How about going to a move?
 93 Your life is your own affair
 94 */
 95 bool cmpValue(const std::pair<int, int>& lhs, const std::pair<int, int>& rhs)
 96 {
 97     return lhs.second > rhs.second;  
 98 }
 99 
100 void SentenceYour(const std::vector<string>& inputstr, std::vector<int>& output)
101 {
102     if (inputstr.empty())
103     {
104         return;
105     }
106 
107 
108     //int *count = new int[inputstr.size()];
109     //memset(count, 0, sizeof(int)*inputstr.size());
110     
111     hash_map<int, int> count;
112     for (vector<string>::size_type line_num = 0; line_num != inputstr.size(); ++line_num)
113     {
114         istringstream line(inputstr[line_num]);
115         string word;
116         while (line>>word)
117         {
118             std::transform(word.begin(), word.end(), word.begin(), ::tolower);
119             if (word=="your")
120             {
121                 ++count[line_num];
122             }
123         }
124     }
125 
126     vector<std::pair<int, int>> counts(count.begin(),count.end());
127 
128     std::sort(counts.begin(), counts.end(), cmpValue);
129     for (vector<std::pair<int, int>>::const_iterator citr = counts.cbegin(); citr != counts.cend();++citr)
130     {
131         if (citr->second!=0)
132         {
133             output.push_back(citr->first);
134         }
135     }
136 }
137 int main()
138 {
139     
140     //题目1:
141     int num[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
142     int n = 0;
143     int pos = bin_search(num, 8, n);
144     if (pos!=-1)
145     {
146         std::cout << n << "has found at " <<pos<< std::endl;
147     }
148     else
149     {
150         std::cout << n << "has not found" << std::endl;
151     }
152 
153     //题目2
154     char ch[] = "abcdedab";
155     char a = occur2times(ch);
156     if (a!='\0')
157     {
158         std::cout << "first char occured 2 times is " << a << std::endl;
159     }
160     else
161     {
162         std::cout << "no char occured 2 times  " << std::endl;
163 
164     }
165     
166 
167     //题目3:
168     vector<string> input;
169     cout << "输入文本\n";
170     string line;
171     while(getline(cin,line))
172     {
173         input.push_back(line);
174     }
175     vector<int> output;
176     SentenceYour(input, output);
177     if (!output.empty())
178     {
179         cout << "sentence have your are:\n";
180         vector<int>::const_iterator itr = output.begin();
181         while (itr!=output.end())
182         {
183             cout << input[*itr++] << endl;
184         }
185     }
186     else
187     {
188         cout << "no sentence have your\n";
189 
190     }
191     return 0;
192 }

输出:

转载于:https://www.cnblogs.com/haoliuhust/p/4350086.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值