for Robot Artificial Inteligence

16.sorting structure

|

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct Car
{
    int speed, price;
    char name[20];
}cars[100];// Global variable are initialized: number - 0, char - NULL(nothing)

bool cmp(Car A, Car B)
{
    if (A.speed!=B.speed)
        return A.speed > B.speed;
    else
        return A.price<B.price;

    /*
    return (A.speed/A.price) > B.speed/B.price)
    */

}

int main()
{
    cars[1].speed = 100; cars[1].price=50;
    cars[2].speed = 200; cars[1].price=147;
    cars[3].speed = 200; cars[1].price=153;
    cars[4].speed = 132; cars[1].price=344;
    cars[5].speed = 97; cars[1].price=45;
    cars[6].speed = 97; cars[1].price=89;

    sort(cars+1, cars+7, cmp);

    for (int i = 1; i<=6; i++)
    {
        cout<<cars[i].speed<<"\n";
    }

    return 0;
}

Comments