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

来源:互联网 发布:苹果6移动网络快捷键 编辑:程序博客网 时间:2024/04/27 23:08
/*  * 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;  }  

运行结果:

 

原创粉丝点击