您的位置:首页 > 其它

【算法题】找出两个已序数组,是否含有相同的数字

2013-06-22 11:40 204 查看
问题描述:

找出两个已序数组,是否含有相同数字。

代码解决:

/*
version  1.0
Created  16:15 2011-6-29
Author   fan
*/
#include <iostream>
using namespace std;

bool SameNumber(int arrA[],int arrB[],int lengthA,int lengthB)
{
int i,j;
i=0;
j=0;
while(i<lengthA &&j<lengthB)
{
if (arrA[i]<arrB[j])  i++;
else
if (arrA[i]>arrB[j]) j++;
else
return true;
}
return false;
}

void InputArray(int arrX[],int length)
{
int i;
for (i=0;i<length;i++)
{
cin>>arrX[i];
}
}

int main()
{
int arrA[10];
int arrB[10];
int lengthA;
int lengthB;

cin>>lengthA;
InputArray(arrA,lengthA);
cin>>lengthB;
InputArray(arrB,lengthB);

if (SameNumber(arrA,arrB,lengthA,lengthB))
{
cout<<"the two Array have the same number!"<<endl;
}
else
{
cout<<"the two Array don't have the same number!"<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: