第十六周OJ平台Problem B: 有相同数字?

来源:互联网 发布:淘宝积分卖家页面 编辑:程序博客网 时间:2024/06/05 20:44

Description

输入两个数组中要存放的元素个数及元素值(不超过50个),判断这两个数组中是否有相同的数字。
在下面的程序基础上完成:
#include<iostream>
using namespace std;
bool existthesame(int *a,int n1,int *b,int n2); //n1个数据的a数组中和n2个数据的b数组中是否有相同元素
int main()
{
    int a[50];
    int b[50];
    int i, n1, n2;
    //读入数据
    ……
    bool flag=existthesame(a,n1,b,n2);
    if(flag==true)
        cout<<"YES\n";
    else
        cout<<"NO\n";
    return 0;
}
bool existthesame(int *a,int n1,int *b,int n2)
{
 
}

Input

共有两组数。每组数包括:这组数的个数n,以及这n个数字。(n<=50)

Output

当两组数中有相同数字时,输出YES,否则,输出NO

Sample Input

6 1 7 8 10 12 174 2 7 12 25

Sample Output

YES

 

代码

<pre class="html" name="code">#include<iostream>using namespace std;bool existthesame(int *a,int n1,int *b,int n2); //n1个数据的a数组中和n2个数据的b数组中是否有相同元素int main(){    int a[50];    int b[50];    int i, n1, n2;    cin>>n1;    for (i=0; i<n1; i++)        cin>>a[i];    cin>>n2;    for (i=0; i<n2; i++)        cin>>b[i];    bool flag=existthesame(a,n1,b,n2);    if(flag==true)        cout<<"YES\n";    else        cout<<"NO\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-i+1; j++)        {            if (*(a+i)==*(b+j))                return true;        }    }}

 

运行结果:

 

学习心得:

好好学习 天天向上

0 0
原创粉丝点击