主题:第39次编程比赛题目
ccpp [专家分:9360] 发布于 2006-08-24 14:37:00
第1题:
半数问题,给定整型数组vote[], 其大小为n,数组中任一元素均为自然数i(i = 1,2,3,...)。
规定函数接口:int Majority (int vote[], int n)
在vote[]中,可能存在一个自然数i,其个数m 超过半数,即m > n/2。若存在这样的i,函数Majority返回m,否则函数返回0。
例:
vote[5] = {1,8,1,100,1}; 其大小为 5。
因为自然数1的个数为3,超过半数,那么调用Majority将返回 3。
题目要求:编写规定函数接口 Majority。
赠送题:
已知f[]和g[]两个数组,大小分别为m和n,其中的元素都已经从小到大排列,且每个数组内的元素都各不相同。
规定函数接口:int Count (int f[], int g[], int m, int n)
函数Count返回两个数组中有多少组元素相同。
例:
f[5] = {1,3,4,7,9}, g[4] = {3,5,7,8};
因为只有2组元素相同,分别是f[1]与g[0] 和 f[3]与g[2],所以函数Count的返回值应为2。
题目要求:编写规定函数接口Count。
回复列表 (共57个回复)
31 楼
hwjian [专家分:0] 发布于 2006-08-25 11:11:00
int cmp(const void*x, const void *y){
return (*(int*)x - *(int*)y);}
int Majority (int vote[], int n)
{
qsort((void*)vote, n, sizeof(int), cmp);
int temp=n>>1;
int count=temp+1;
int flag=1;
for(int i=0;i<temp&&flag;i++)
{
if(vote[i]==vote[temp+i])
{
int j=temp+1;
while(vote[j++]==vote[i])
count++;
flag=0;
}
}
if(flag)
return 0;
else
return count;
}
32 楼
天边蓝 [专家分:1810] 发布于 2006-08-25 13:47:00
#include<cstdlib>
#include<cstdio>
int cmp(const void*x, const void *y)
{
return (*(int*)x - *(int*)y);
}
int Majority(int vote[], int n){
qsort(vote,n,sizeof(int),cmp);
for(int i=0;i<n/2+1;){
for(int j=i+n/2;j>i;j--){
if(vote[j]!=vote[j-1]){
i=j;
j=i+1;
break;
}
}
if(j==i)
return vote[i];
}
return 0;
}
附加:
int Count (int f[], int g[], int m, int n){
int i,j,count=0;
for(i=0,j=0;i<m&&j<n;){
if(f[i]<g[j])
i++;
else if(f[i]>g[j])
j++;
else{
count++;
i++;j++;
}
}
return 0;
}
33 楼
mscorp [专家分:70] 发布于 2006-08-25 13:55:00
看得到题目吗?
34 楼
mscorp [专家分:70] 发布于 2006-08-25 13:56:00
看看你的..
35 楼
neverPE [专家分:1620] 发布于 2006-08-25 14:16:00
#include<time.h>
using namespace std;
int Majority (int vote[], int n)
{
if (n<=1) return n;
const int m=511;
int a[m][2],i;
memset(a,0,sizeof(a));
srand((unsigned)time(NULL));
//随机在vote[]中获得m个数
for (i=0;i<m;i++)
{
int j,ran=vote[unsigned(rand()*10000+rand())%n];
for (j=0;a[j][0]>0 && ran!=a[j][0];j++);
a[j][0]=ran;
a[j][1]++;
}
//找到m个数中,出现次数大于0.4*m的数。满足该要求的数最多2个
int value1=0,value2=0,count1=0,count2=0;
for (i=0;i<m;i++)
if (a[i][1]>m*2/5)
if (value1==0) value1=a[i][0]; else value2=a[i][0];
//判断出现次数是否大于n/2
if (value1==0) return count1;
if (value2==0)
{
for (i=0;i<n;i++)
if (vote[i]==value1) count1++;
}
else
{
for (i=0;i<n;i++)
if (vote[i]==value1) count1++;
else if (vote[i]==value2) count2++;
}
if (count1>n/2) return count1;
if (count2>n/2) return count2;
return 0;
}
36 楼
ITER [专家分:680] 发布于 2006-08-25 14:51:00
赠送题:
int Count(int f[], int g[], int m, int n)
{
int i=0,j=0;
int count = 0;
while(i<m&&j<n)
{
while(f[i]>g[j]&&j<n)
j++;
if(f[i]<g[j]&&i<m)
i++;
else if(f[i]==g[j]&&i<m&&j<n)
{
count++;
i++;
j++;
}
}
return count;
}
37 楼
yunzhou008 [专家分:410] 发布于 2006-08-25 14:51:00
第一题:
#include <iostream>
#include <map>
using namespace std;
int Majority(int vote[],int n)
{
map<int,int> mp;
int MaxVal=0;
for(int i=0;i<n;++i)
{
if(!mp.insert(make_pair(vote[i],1)).second)
{
int val=++mp[vote[i]];
if(val>MaxVal)
MaxVal=val;
}
}
return MaxVal>(n/2)?MaxVal:0;
}
int main()
{
int vote[]={1,8,1,100,1};
cout<<Majority(vote,5)<<endl;
system("pause");
}
38 楼
yunzhou008 [专家分:410] 发布于 2006-08-25 15:11:00
赠送题:
#include <iostream>
using namespace std;
int Count(int f[],int g[],int m,int n)
{
int count=0;
for(int i=0;i<m;++i)
{
int j=0;
while(f[i]<g[j]&&i<m)
{
++i;
}
while(f[i]>g[j]&&j<n)
{
++j;
}
if(f[i]==g[j])
++count;
}
return count;
}
int main()
{
int f[] = {1,3,4,7,9,25};
int g[] = {3,5,7,8,10,20,25};
cout<<Count(f,g,6,7)<<endl;
system("pause");
}
39 楼
yunzhou008 [专家分:410] 发布于 2006-08-25 15:14:00
赠送题:
#include <iostream>
using namespace std;
int Count(int f[],int g[],int m,int n)
{
int count=0;
for(int i=0;i<m;++i)
{
int j=0;
while(f[i]<g[j]&&i<m)
{
++i;
}
while(f[i]>g[j]&&j<n)
{
++j;
}
if(f[i]==g[j])
++count;
}
return count;
}
int main()
{
int f[] = {1,3,4,7,9,25};
int g[] = {3,5,7,8,10,20,25};
cout<<Count(f,g,6,7)<<endl;
system("pause");
}
40 楼
81872686 [专家分:0] 发布于 2006-08-25 15:38:00
....................................
我来回复