美文网首页
051:指针练习:MyMax

051:指针练习:MyMax

作者: Lyn谷神不死 | 来源:发表于2018-01-31 23:19 被阅读0次

    051:指针练习:MyMax
    总时间限制: 1000ms 内存限制: 65536kB
    描述
    编写一个 MyMax函数,可以用来求任何数组中的最大值 使得程序按要求输出

    #include <iostream>
    using namespace std;
    // 在此处补充你的代码
    int Compare1(void * n1,void * n2)
    {
        int * p1 = (int * )n1;
        int * p2 = (int * )n2;
        return ((*p1)%10) - ((*p2)%10);
    }
    int Compare2(void * n1,void * n2)
    {
        int * p1 = (int * )n1;
        int * p2 = (int * )n2;
        return *p1 - *p2;
    }
    #define eps 1e-6
    int Compare3(void * n1,void * n2)
    {
        float * p1 = (float * )n1;
        float * p2 = (float * )n2;
        if( * p1 - * p2 > eps)
            return 1;
        else if(* p2 - * p1 > eps)
            return -1;
        else
            return 0; 
    }
    
    int main()
    {
        int t;
        int a[10];
        float d[10];
        cin >> t;
        while(t--) {
            int n;
            cin >> n;
            for(int i = 0;i < n; ++i)
                cin >> a[i];
            for(int i = 0;i < n; ++i)
                cin >> d[i];
            int * p = (int *) MyMax(a,sizeof(int),n,Compare1);
            cout << * p << endl;
            p = (int *) MyMax(a,sizeof(int),n,Compare2);
            cout << * p << endl;
            float * pd = (float * )MyMax(d,sizeof(float),n,Compare3);
            cout << * pd << endl;
        }
        return 0;
    }
    

    输入
    第一行是测试数据组数 t
    对每组数据:
    第一行是整数n (1<=n<=10)
    第2行是 n个整数
    第3行是n个浮点数
    输出
    对每组数据:

    先输出n个整数中个位数最大的数(答案保证唯一)
    再输出n个整数中最大的数
    再输出n个浮点数中最大的数
    样例输入
    2
    5
    31 20 100 7 8
    30.1 100.2 2.5 9.8 48.4
    2
    1 2
    0.1 0.2
    样例输出
    8
    100
    100.2
    2
    2
    0.2
    来源
    Guo Wei
    代码

    #include <iostream>
    using namespace std;
    // 在此处补充你的代码
    void * MyMax(void *be,int width,int num,int(*f)(void * ,void * ))
    {
        void * q=be;
        for(int i=0;i<num;i++)
        {
            if(f(q,(char *)be+width*i)<0)
            {
                q=(char *)be+width*i;
            }
        }
        return q;
    }
    int Compare1(void * n1,void * n2)
    {
        int * p1 = (int * )n1;
        int * p2 = (int * )n2;
        return ((*p1)%10) - ((*p2)%10);
    }
    int Compare2(void * n1,void * n2)
    {
        int * p1 = (int * )n1;
        int * p2 = (int * )n2;
        return *p1 - *p2;
    }
    #define eps 1e-6
    int Compare3(void * n1,void * n2)
    {
        float * p1 = (float * )n1;
        float * p2 = (float * )n2;
        if( * p1 - * p2 > eps)
            return 1;
        else if(* p2 - * p1 > eps)
            return -1;
        else
            return 0; 
    }
    
    int main()
    {
        int t;
        int a[10];
        float d[10];
        cin >> t;
        while(t--) {
            int n;
            cin >> n;
            for(int i = 0;i < n; ++i)
                cin >> a[i];
            for(int i = 0;i < n; ++i)
                cin >> d[i];
            int * p = (int *) MyMax(a,sizeof(int),n,Compare1);
            cout << * p << endl;
            p = (int *) MyMax(a,sizeof(int),n,Compare2);
            cout << * p << endl;
            float * pd = (float * )MyMax(d,sizeof(float),n,Compare3);
            cout << * pd << endl;
        }
        return 0;
    }
    

    注意:
    1、[Error] ld returned 1 exit status
    经常是因为之前的运行窗口没关
    2、[Error] void value not ignored as it ough
    可能是对void类型函数进行取值,所以报错
    3、error: too few arguments to function
    函数参数不对

    相关文章

      网友评论

          本文标题:051:指针练习:MyMax

          本文链接:https://www.haomeiwen.com/subject/rqrrzxtx.html