Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

c | c++ function return optimization

how do I optimize this code? I actually want to create a function like swap_and_return which should return from the function if done. but I could not do it.

we can see that I wrote a lot of print values and swap values which I could've written maybe inside of swap function.

what are your suggestions?

void swap(int *i, int *j) {
    int t = *i;
    *i = *j;
    *j = t;
}

void print_values(int x, int y, int z) {
    cout << "x=" << x << ", y=" << y << ", z=" << z << "
";
}

float min_max(int x, int y, int z) {
    float avg = 0;
    if(x<y && x<z){
            if (y < z) {
                swap(&x, &z);
                print_values(x, y, z);
                avg = (x + z) / 2.0;
                return avg;
            }

            else {
                swap(&x, &y);
                print_values(x, y, z);
                avg = (x + y) / 2.0;
                return avg;
            }
    }

    else if(y<x && y<z){
            if (x < z) {
                swap(&y, &z);
                print_values(x, y, z);
                avg = (y + z) / 2.0;
                return avg;
            }

            else {
                swap(&y, &x);
                print_values(x, y, z);
                avg = (y + x) / 2.0;
                return avg;
            }
    }

    else if(z<y && z<x){
            if (x < y) {
                swap(&z, &y);
                print_values(x, y, z);
                avg = (z + y) / 2.0;
                return avg;
            }

            else {
                swap(&z, &x);
                print_values(x, y, z);
                avg = (z + x) / 2.0;
                return avg;
            }
    }

    return avg;
    
}

int main()
{
    float value = min_max(5, 1, 2);
    cout << value << "
";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just sort the items and do your math on the smallest and largest:

#include <iostream>
#include <algorithm>

void print_values(int x, int y, int z) {
    std::cout << "x=" << x << ", y=" << y << ", z=" << z << "
";
}

float min_max(int x, int y, int z) {

    float avg = 0;
    int items[3] = { x,y,z };

    std::sort(items, items + 3);
    print_values(items[2], items[1], items[0]);
    avg = (items[0] + items[2]) / 2.0;
    return avg;
}


int main()
{
    float value = min_max(1,2,5);
    std::cout << value << "
";
}

If you can't use std::sort, you can easily sort a 3-item array very quickly

#define SWAP_IF_GT(a, b) {if (a > b) {int tmp=a;a=b;b=tmp;}}

float min_max(int x, int y, int z) {
    float avg = 0;

    SWAP_IF_GT(x,y);
    SWAP_IF_GT(y,z);
    SWAP_IF_GT(x,y);

    print_values(z,y,x);
    avg = (x+z) / 2.0;
    return avg;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...