Data Structure 顺序表练习题(两道)

数据结构 课堂实验二 实验报告

第一题

题目

在一个整数序列a1,a2,…,an中,若存在一个数,大于它的整数数量和小于它的整数数量相等,则该数被称为“中间数”。请编写程序实现整数序列的“中间数”测试。具体样例:
/*样例输入 5 //包含5个整数的序列
3 4 6 6 7 //随机产生一个整数序列
则输出
-1 //输出-1表示整数序列“3 4 6 6 7”中不存在中间数
说明:
在一个整数序列中,可能存在多个中间数。
整数序列在程序中由随机数生成,程序输出“中间数”的值,若中间数不存在,则输出-1。

代码

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
    //要用到的函数
    void SortArray(int a[],int length);
    int findMidNumber(int a[],int length);
    //待查询的数组的大小
    int size;
    //输入提示语句
    cout<<"enter the size of your random elements' array: "<<endl;
    //进行输入操作
    cin>>size;
    //创造一个随机数数组
    //size=4;
    int *a = new int[size];
    for(int i=0;i<size;i++)
    {
        //a[i]=i;
        a[i]=rand();
    }
    //这个数组需要排序操作
    SortArray(a,size);
    //开始找中位数
    cout<<findMidNumber(a,size)<<endl;
    return 0;
}
//找中位数的程序
/*设计思路:
一个头指针和一个尾指针,分别指向数组的头和数组的尾
然后开始同步逼近
只要能够相等那么指向的数就是中间数,
(奇数可以走到一起)如果就算走到一起了也不能相等,那么中间数不存在。
(偶数不能走到一起)如果就是相邻呢
*/
int findMidNumber(int a[],int length)
{
    //头指针
    int *head;
    head=&a[0];
    //尾指针
    int *tail;
    tail=&a[length-1];
    //寻找
    if(length%2==0)
    {
        for(int i=0;head-1!=tail;i++)
    {
        //判断相等
        if(*head==*tail)
        {
            return *head;
        }
        else
        {
            head=head+1;
            tail=tail-1;
        }
    }
    }
    else
    {
        for(int i=0;head+2!=tail;i++)
    {
        //判断相等
        if(*head==*tail)
        {
            return *head;
        }
        else
        {
            head=head+1;
            tail=tail-1;
        }
    }
    }
    cout<<"not Found."<<endl;
    return -1;
}
//调整为从小到大排列
void SortArray(int a[],int length)
{
    cout<<"The array is going to be SORTED."<<endl;
    int temp;
    for(int i=0;i<length;i++)
    {
        for(int j=i+1;j<length;j++)
        {
            if(a[i]>a[j])
            {
                //到时候可以改进成swap函数
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    cout<<"The array has been SORTED."<<endl;
}

第二题

题目

股票收益:假设你知道接下来n天的某只股票的价格序列a1,a2,…,an,请编写程序实现确定最优的买入时间和卖出时间以获得最大收益。股票价格数组在程序中由随机数组成,程序输出最终可以获得的最大收益和买入卖出时间,若无收益则输出0。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main()
{
    void findMaxProfit(int a[],int n);
    cout<<"Profit Maximi`在这里插入代码片`ze."<<endl;
    //创建一个随机数数组
    int n;
    cout<<"Input n:";
    cin>>n;
    int *a=new int[n];
    for(int i=0;i<n;i++)
    {
        a[i]=rand();
    }

    //运用编写出来的函数
    findMaxProfit(a,n);
    return 0;
}
void findMaxProfit(int a[],int n)
{
    //把这段代码封装到函数里面去

    //其实这个问题不用进行原数据的拷贝,因为只要求出时间就可以?
    //但是这就意味着需要指针,因为我不想记住每个利率对应的编号?

    //等下,好像这个问题可以简化

    /*
    大体的思路如下:
    只要是数组内的比较就行?
    越靠前出现的数据作为被减数,
    靠后出现的数据作为减数,
    放一个标记值,如果这个差值变大了,那么就更新,反之就不动。
    */

    int temp=0;
    int buy=0;
    int sale=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(a[j]-a[i]>temp)
            {
                temp=a[j]-a[i];
                buy=i+1;
                sale=j+1;
            }
        }
    }
    cout<<"Max Profit is "<<temp<<"."<<endl;
    cout<<"While buy it at date "<<buy<<", sale it at date "<<sale<<endl;

}
发布了9 篇原创文章 · 获赞 3 · 访问量 215

猜你喜欢

转载自blog.csdn.net/weixin_44491160/article/details/104230231