第16周oj平台实践项目(3)(4)

Problem C: 相同的数字!

Description

输出两个有序数列(不超过50个)中有多少相同数据,并输出这些数据。
在下面的程序基础上完成:
int sameNum(int *a,int n1,int *b,int n2, int *c);
int main()
{
    int a[50];
    int b[50];
    int c[50];
    int i, n1, n2, n3;
    //读入数据
    ……
    n3 = sameNum(a,n1,b,n2,c);
    if(n3==0)
        cout<<"NULL\n";
    else
    {
        cout<<n3<<endl;
        for(i=0; i<n3; i++)
            cout<<c[i]<<" ";
        cout<<endl;
    }
    return 0;
}
int sameNum(int *a,int n1,int *b,int n2, int *c)
{
}

Input

第一行输入这两组数的个数(不超过50个)。
后面两行分别输入这两组数。同一序列中的数字不会重复。

Output

第一行输出相同数字的个数,第二行输出这些相同的数字。
若没有相同的数字,输出NULL

Sample Input

<pre class="content" style="padding: 8.5px; font-family: Menlo, Monaco, 'Courier New', monospace; font-size: 12.025px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 9px; line-height: 18px; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902); white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span class="sampledata" style="background-image: none; background-attachment: scroll; background-color: rgb(141, 184, 255); font-family: monospace; font-size: 18px; white-space: pre; background-position: 0px 0px; background-repeat: repeat repeat;">7 9 
1 3 4 6 9 12 17
2 3 6 8 10 12 15 19 21</span>
 
 

Sample Output

3
3 6 12
代码:
#include<iostream>
using namespace std;
int sameNum(int *a,int n1,int *b,int n2, int *c);
int main()
{
    int a[50];
    int b[50];
    int c[50];
    int i, n1, n2, n3;
     i=0;
    cin>>n1>>n2;

    while (i<n1)
    cin>>a[i++];
    i=0;
    while (i<n2)
    cin>>b[i++];
    n3 = sameNum(a,n1,b,n2,c);
    if(n3==0)
        cout<<"NULL\n";
    else
    {
        cout<<n3<<endl;
        for(i=0; i<n3; i++)
            cout<<c[i]<<" ";
        cout<<endl;
    }
    return 0;
}
int sameNum(int *a,int n1,int *b,int n2, int *c)
{
    int i=0,j,sum=0;
    while (i<n1)
    {
        j=0;
        while (j<n2)
        {
            if (*(a+i)==*(b+j))
            {
                *(c+sum)=*(b+j);
                sum++;
            }
            j++;
        }
        i++;
    }
    return sum;
}

运行结果:

Problem D: 指针引出奇数因子


Description

编写函数 int fun(int x, int *pp)。其功能是,求出x的所有奇数因子,并按照从小到大的顺序放在pp指向的内存中,函数返回值为这些整数的个数。若x的值为30,数组中的数为1,3,5,15,函数返回4。
用下面的main()函数进行测试:
int main()
{
    int a[50],x,n;
    cin>>x;
    n=fun(x,a);
    cout<<n<<endl;
    for(int i=0; i<n; i++)
        cout<<a[i]<<" ";
    cout<<endl;
    return 0;
}

Input

一个整数

Output

输入的整数的奇因子个数,以及这些奇因子

Sample Input

30

Sample Output

4
1 3 5 15
代码:
#include<iostream>
using namespace std;
int fun(int x, int *pp)
{
    int k=0;
    for(int i=1; i<=x/2; i++)
    {
        if (x%i==0&&i%2!=0)
        {
            *(pp+k)=i;
            k++;
        }
    }
    return k;
}
int main()
{
    int a[50],x,n;
    cin>>x;
    n=fun(x,a);
    cout<<n<<endl;
    for(int i=0; i<n; i++)
        cout<<a[i]<<" ";
    cout<<endl;
    return 0;
}


运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值