轩辕互动Exoweb笔试题(一)
来源:互联网 发布:java 7下载地址 编辑:程序博客网 时间:2024/04/28 04:42
1 定义一个数组中,若某个值出现的次数在一半以上,则为执行者。如下数组: A[8]={1,2,2,4,2,4,2,2} 由于2出现上午次数5次大于5/8>0.5, 所以2为数组A[8]的执行者。
解法一:
/****************************************************************
* Function: Find the number who appears over half of the total times in an array
*
* @Author: Zheng Haishu Date: 2009-12-31
****************************************************************/
#include<iostream>
using namespace std;
void Administer(int find[], int length)
{
int i,j;
int found = 0; // 0 indicates not found the administer
// 1 indicates found the administer
int num = 0;
for(i=0; i<length&&!found; i++)
{
num = find[i];
int count = 0;
for(j=0; j<length; j++)
{
if(num == find[j])
count++;
}
if(count>length/2)
{
cout<<num<<" is an administer"<<endl;
found = 1; // if found administer, break the loop
}
}
}
int main(void)
{
int array[] = {1,2,2,4,2,4,2,2};
Administer(array, 8);
return 0;
}
运行结果:
2 is an administer
解法二:
/****************************************************************
* Function: Find the number who appears over half of the total times in an array
*
* @Author: Zheng Haishu Date: 2009-12-31
****************************************************************/
#include<iostream>
using namespace std;
int Administer(int find[], int length)
{
int number;
int nTimes=0;
int i;
for(i = 0; i < length; i++)
{
if(nTimes == 0)
{
number = find[i];
nTimes = 1;
}
else
{
if(number == find[i])
nTimes++;
else
nTimes--;
}
}
return number;
}
int main(void)
{
int array[] = {1,2,2,4,2,4,2,2};
int administer = Administer(array, 8);
cout<<administer<<" is an administer."<<endl;
return 0;
}
运行结果:
2 is an administer
2 平衡点:比如int[] numbers = {1,3,5,7,8,25,4,20}; 25前面的总和为24,25后面的总和也是24,25这个点就是平衡点;假如一个数组中的元素,其前面的部分等于后面的部分,那么这个点的位序就是平衡点。要求:返回任何一个平衡点
解:
#include<iostream>
using namespace std;
void Equilibrium(int find[], int length)
{
int i,j,k;
int lSum; // sum of left side
int rSum; // sum of right side
for(i=0; i<length; i++)
{
lSum=0;
rSum=0;
for(j=0;j<i;j++)
{
lSum += find[j];
}
for(k=i+1; k<length; k++)
{
rSum += find[k];
}
if(lSum == rSum)
{
cout<<find[i]<<" is an equilibrium point"<<endl;
}
}
}
int main(void)
{
int array[]={1,3,5,7,8,25,4,20};
Equilibrium(array, 8);
return 0;
}
运行结果:
25 is an equilibrium point
3 实现一个去除整型数组中绝对值相同的数字.最后返回一个长度
#include<iostream>
using namespace std;
void NewLength(int find[], int length)
{
int i,j;
for(i=0; i<length; i++)
{
if(find[i] == 0)
continue;
for(j=i+1; j<length; j++)
{
if(find[j] == find[i] || find[j] == (-find[i]))
{
find[i] = 0;
find[j] = 0;
break;
}
}
}
int newLength = 0;
for(i=0; i<length; i++)
{
if(find[i] != 0)
{
cout<<find[i]<<" ";
newLength++;
}
}
cout<<endl;
cout<<"Length of new array is "<<newLength<<endl;
}
int main(void)
{
int array[] = {1,2,2,3,-4,4,5,6,7,-6};
NewLength(array, 10);
return 0;
}
运行结果:
1 3 5 7
Length of new array is 4
4 找排序后的值,在原数组中的下标
#include<iostream>
using namespace std;
#define length 8
void SortAndFind(int find[])
{
int i,j,temp;
int originArray[length];
for(i=0; i<length; i++)
{
originArray[i] = find[i];
}
for(j=1; j<=length-1; j++)
{
for(i=0;i<=length-1-j;i++)
{
if(find[i] > find[i+1])
{
temp = find[i];
find[i] = find[i+1];
find[i+1] = temp;
}
}
}
int number;
cout<<"Input a number: ";
cin>>number;
for(i=0; i<length; i++)
{
if(number == originArray[i])
{
cout<<"The index of "<<number<<" is "<<i<<endl;
}
}
}
int main(void)
{
int array[] = {4,3,2,8,1,5,7,6};
SortAndFind(array);
return 0;
}
运行结果:
Input a number: 4
The index of 4 is 0
- 轩辕互动Exoweb笔试题(一)
- 轩辕互动Exoweb笔试题(三)
- 轩辕互动Exoweb笔试题(二):小猪吃米
- exoweb(轩辕互动)机试+面试
- 轩辕互动的python面试题(转)
- 软件工程师笔试(北京轩辕科技)
- 轩辕互动 面试题 数括号
- 网易互动笔试真题
- 11年千橡互动(人人网)C++笔试题
- 轩辕剑--资料集(一)
- 轩辕互动面试题两道比较复杂的
- 轩辕互动面试题两道比较复杂的
- 笔试题(一)
- 笔试题(一)
- 【笔试练习】百度笔试题(一)
- 华为笔试题(一)
- 笔试题练习(一)
- C++ 笔试题(一)
- 超级难度数学问题
- TimesTen cache connection数据同步时间监测
- 漏油 fuel leak
- 全面解析5种常见悬挂—多连杆独立悬挂
- 洛杉矶别名知多少?
- 轩辕互动Exoweb笔试题(一)
- ftp主动模式和被动模式
- SQL*PLUS命令的使用大全(下)
- 全面解析5种常见悬挂——拖曳臂式悬挂
- WATIN如何处理确认对话框
- Opencv 2.0在Windows下的安装与配置
- 很羞愧,自己还是家里蹲
- 一个非常简单的菜单栏折叠控制方法
- 全面解析5种常见悬挂——连杆支柱悬挂