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