详细解决方案
相同数字??
热度:13 发布时间:2024-01-15 03:28:02.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]);cout<<"a[]={1,4,7,8}"<<endl;cout<<"b[]={2,5,6,9,10}"<<endl;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 *p,*q; bool same=false; for(p=a;p<a+n1&&!same;++p) { for(q=b;q<b+n2&&!same;++q) if (*p==*q) same=true; } return same;
}
运行结果: