- /*
- * Copyright (c) 2012, 烟台大学计算机学院
- * All rights reserved.
- * 文件名称:test.cpp
- * 作者:王俊
- * 完成日期:2012 年12月23日
- * 版本号:v1.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; }