网易C++内推笔试编程题代码

第一道,01交错串,代码如下:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
using size_type = string::size_type;

int main()
{
	string str;
	
	while(cin>>str){
		
		size_type start = 0;
		size_type end = 0;
		size_type maxlen = 1; 
		
		for(size_type i=1;i<str.size();++i){
			if(str[i] == str[end]){
				maxlen = max(end-start+1,maxlen);
				start = i;
				end = start;
			}else{
				++end;
			}
		}
		maxlen = max(end-start+1,maxlen);
		cout<<maxlen<<endl;
	}
	return 0;
}
第二道,数列反转,代码如下:
#include <iostream>
#include <deque>
using namespace std;

int main()
{
	deque<long> deq;
	deque<long>::size_type len;
	
	while(cin>>len){
		deq.resize(0);
		if(len%2==0){
			for(deque<long>::size_type i=0;i<len;++i){
				int tmp;
				cin>>tmp;
				if(i%2==0){
					deq.push_back(tmp);
				}else{
					deq.push_front(tmp);
				}
			}
		}else{
			for(deque<long>::size_type i=0;i<len;++i){
				int tmp;
				cin>>tmp;
				if(i%2==0){
					deq.push_front(tmp);
				}else{
					deq.push_back(tmp);
				}
			}
		}
		for(deque<long>::size_type i=0;i<deq.size();++i){
			cout<<deq[i];
			if(i!=deq.size()-1)
				cout<<' ';
		}
		cout<<endl;
	}
	return 0;
}
第三道,疯狂队列,代码如下:
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;

int main()
{
	vector<long> vec;
	vector<long>::size_type len;
	
	while(cin>>len){
		deque<int> deq;
		vec.resize(len);
		long crazy = 0;
		
		for(vector<long>::size_type i=0;i<len;++i){
			cin>>vec[i];
		}
		
		sort(vec.begin(),vec.end());
		deq.push_back(vec.back());

		for(vector<long>::size_type i=0;i<len-1;++i){
			long front,back;
			front = abs(vec[i]-deq.front());
			back = abs(vec[i]-deq.back());
			
			if(front >= back){
				deq.push_front(vec[i]);
				crazy += front;
				
			}else{
				deq.push_back(vec[i]);
				crazy += back;
			}
		}
		cout<<crazy<<endl;
	}
	return 0;
}
不知道大家有没有更好的方法
#网易##C++工程师#
全部评论
我一个测试做得全是开发的编程,不知该哭还是该笑😁
点赞
送花
回复
分享
发布于 2017-08-12 18:57
我面的iOS,笔试题和你的一样,前面的选择题好迷,还考android.....c++不怎么熟悉,编程题用的php,分别100%,30%,90%.......看来还是用c++好点,之前对比了一下,php的耗时和内存都高多了。。。。我感觉我是因为这个不能全过
点赞
送花
回复
分享
发布于 2017-08-12 19:18
秋招专场
校招火热招聘中
官网直投
第三道ac了么,感觉有问题:对排好序的数组进行比较插入到deque中有时会得不到最优解,eg,1-2-3-4-5-6,按照你的方法得到的序列是5-3-1-6-2-4,但是序列3-5-1-6-2-4更好。
点赞
送花
回复
分享
发布于 2017-08-12 19:44
C++的题感觉比较简单啊。。
点赞
送花
回复
分享
发布于 2017-08-12 20:06
第一个可以直接打啊,输入只有一种字符输出1,两种字符输出2,两种以上就是0了
点赞
送花
回复
分享
发布于 2017-08-13 17:48

相关推荐

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