您的位置:首页 > 其它

第17周项目-有相同的数字?

2012-12-20 16:41 253 查看
/*
* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作    者:徐凯旋
* 完成日期:2012 年 12 月 20 日
* 版 本 号:v 1.0
*/
#include<iostream>
using namespace std;
bool existthesame(int *a,int n1,int *b,int n2);
int main()
{
int a[]={1,4,7,8};
int b[]={2,5,6,9,10};
int n1=sizeof(a)/sizeof(a[0]);
int n2=sizeof(b)/sizeof(b[0]);
bool flag=existthesame(a,n1,b,n2);
if(flag==true)
cout<<"两个有序数组中存在相同的数字!\n";
else
cout<<"两个有序数组中不存在相同的数字!\n";
return 0;
}
bool existthesame(int *a,int n1,int *b,int n2)
{
int i,j;
for(i=0;i<n1;i++)
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
return true;
else
return false;
}
return 0;
}

运行结果:



#include<iostream>
using namespace std;
bool existthesame(int *a,int n1,int *b,int n2);
int main()
{
int a[]={1,4,7,8};
int b[]={1,4,7,8};
int n1=sizeof(a)/sizeof(a[0]);
int n2=sizeof(b)/sizeof(b[0]);
bool flag=existthesame(a,n1,b,n2);
if(flag==true)
cout<<"两个有序数组中存在相同的数字!\n";
else
cout<<"两个有序数组中不存在相同的数字!\n";
return 0;
}
bool existthesame(int *a,int n1,int *b,int n2)
{
int i,j;
for(i=0;i<n1;i++)
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
return true;
else
return false;
}
return 0;
}

运行结果:

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