总共三道题,共两个小时的时间,题目分别是:
1.输入整型字符串,输出该整数;如:输入“345”,输出345;
参考:
#include<iostream>
#include"string"
using namespace std;
int exp(int n)
{
int num = 1;
while(n > 0)
{
num = num * 10;
}
return num;
}
void main()
{
string str = 0;
while(cin>>str)
{
int num = 0;
int len = str.length();
for(int i = 0; i < len; i++)
{
num = num + (str[i] - 48) * exp(len - i - 1);
}
cout<<num<<endl;
}
}
2.输入一个字符串,输出该字符串的最长连续子串;如:输入“abcd12345abcdefg”,则输出abcdefg;
参考:
#include<iostream>
#include"string"
using namespace std;
void main()
{
string str = 0;
while(cin>>str)
{
int maxlen = 0;//最长子串长度
int currentlen = 0;//当前子串长度
int start = 0;//最长子串开始下标
int len = str.length();
for(int i = 0; i < len; i++)
{
start = i;
while(str[i] == str[i+1] - 1)
{
currentlen++;
i++;
}
currentlen++;
if(currentlen > maxlen)
{
maxlen = currentlen;
}
}
for(i = start; i < maxlen; i++)
cout<<str[i];
cout<<endl;
}
}
3.输入一个整型数组,输出该数组的主要元素,如果不存在主要元素,则输出-1,找出好的算法,需要考虑时间和控件复杂度;(数组的主要元素:数组中该元素出现的次数超过总数组的一半);如:数组A={1,2,3,4,5,5,5,6,5,5,5,5,5},则输出5.
参考:(排序法,最简单)
#include<iostream>
using namespace std;
void main()
{
int n = 0;
while(1)
{
cout<<"input the size of the array:";
cin>>n;
int *arr = new int[n];
cout<<"input the element of the array:"<<endl;
for(int i = 0; i < n; i++)
{
cin>>arr[i];
}
for(i = 0; i < n; i++)
{
for(int j = 0; j < n - i - 1; j++)
{
if(arr[j] > arr[j + 1])
{
int tem = arr[j];
arr[j] = arr[j + 1];
arr[j+1] = tem;
}
}
}
if(arr[n/2] == arr[n/2 + 1])
{
int mainelement = arr[n/2];
int i = 0;
while(arr[i] != mainelement)
{
i++;
}
int start = i;
if(arr[start + n/2] == mainelement)
cout<<mainelement<<endl;
else
cout<<-1<<endl;
}
else
cout<<-1<<endl;
}
}
都是很简单的题目,留给师弟师妹准备多益的机试。关于多益的机试,有一些要求算法的,千万别想太久,最关键的还是要做出来,几乎可以说做出来了都可以进面试了的,祝师弟师妹成功。