第16周oj平台实践项目(1)(2)

Problem A: 逆序输出数组

Description

下面的程序,采用指针访问的方式,从键盘给数组a[N]输入n个数据(n小于100),然后对元素值按逆序存放后输出。请补充完整下面的程序。
#include <iostream>
using namespace std;
const int N=100;
int main()
{
    int a[N],*p,*q;
    cin>>n;
    for(p=a; p<___(1)____; p++)
        ___(2)___;
    p=__(3)____;
    q=___(4)_____;
    while(p<q)
    {
        int r=*p;
        *p=*q;
        *q=r;
        ___(5)___;
        ___(6)___;
    }
    for(p=a; p<a+n; p++)
        cout<<*p<<' ';
    cout<<endl;
}

Input

共n+1个整数,先输入n值,再输入n个整数

Output

与输入顺序正好相反的n个整数

Sample Input

8 2 5 1 9 6 3 2 7

Sample Output

7 2 3 6 9 1 5 2
代码:
#include <iostream>
using namespace std;
const int N=100;
int main()
{
    int a[N],*p,*q,n;
    cin>>n;
    for(p=a; p<(a+n); p++)
        cin>>*p;
    p=a;
    q=a+n-1;
    while(p<q)
    {
        int r=*p;
        *p=*q;
        *q=r;
        p++;
        q--;
    }
    for(p=a; p<a+n; p++)
        cout<<*p<<' ';
    cout<<endl;
}

运行结果:

Problem B: 有相同数字?


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 17
4 2 7 12 25

Sample Output

YES
代码:
/*
  输入两个数组中要存放的元素个数及元素值(不超过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=0, n1, n2;
    cin>>n1;
    while (i<n1)
        cin>>a[i++];
    i=0;
    cin>>n2;
    while (i<n2)
        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=0,j;
    while (i<n1)
    {
        j=0;
        while (j<n2)
        {
            if (*(a+i)==*(b+j))
                return true;
                j++;
        }
        i++;
    }
    return false;
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值