Informatika gyűjtemény

Egy szinttel feljebb mb_gomb.cpp

2004050607080910

NézetNyomtat

mb_gomb.cpp (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: us-ascii
Méret: 1 KB
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>

using namespace std;

float calcheight(int*);
void next_order();
bool compare(int, int);

int n;
int* r;
int* ro;

int main()
{
    n = 21;
    r = new int[n];
    ro = new int[n];

    r[0]=30;
    r[1]=31;
    r[2]=32;
    r[3]=33;
    r[4]=34;
    r[5]=35;
    r[6]=36;
    r[7]=37;
    r[8]=38;
    r[9]=39;
    r[10]=40;
    r[11]=41;
    r[12]=42;
    r[13]=43;
    r[14]=44;
    r[15]=45;
    r[16]=46;
    r[17]=47;
    r[18]=48;
    r[19]=49;
    r[20]=50;

    ro[0]=19;
    ro[1]=17;
    ro[2]=15;
    ro[3]=13;
    ro[4]=11;
    ro[5]=9;
    ro[6]=7;
    ro[7]=5;
    ro[8]=3;
    ro[9]=1;
    ro[10]=0;
    ro[11]=2;
    ro[12]=4;
    ro[13]=6;
    ro[14]=8;
    ro[15]=10;
    ro[16]=12;
    ro[17]=14;
    ro[18]=16;
    ro[19]=18;
    ro[20]=20;

    /*sort(ro, ro+n, compare);

    while(true)
    {
        printf("O: %d %d %d %d %d\n", r[ro[0]], r[ro[1]], r[ro[2]], r[ro[3]], r[ro[4]]);
        printf("H: %f\n", calcheight(r));

        next_order();
        cin.get();
    }*/

    printf("H: %f\n", 1000*calcheight(r));

    cin.get();
}

float calcheight(int* r)
{
    float h=r[ro[0]];
    for(int i=0; i<n-1; i++)
    {
        h+=10*sqrt(2*r[ro[i]]+2*r[ro[i+1]]-100);
    }
    h+=r[ro[n-1]];

    return h;
}

bool compare(int p1, int p2)
{
    return p1<p2;
}

void next_order()
{
    bool done=false;
    for(int i=n-2; i>=0 && !done; i--)
    {
        if(ro[i+1]>ro[i])
        {
            int ind=i+1;
            for(int j=i+1; j<=n-1; j++)
                if(ro[j]>ro[i] && ro[j]<ro[ind])
                    ind=j;

            int v=ro[ind];
            ro[ind]=0;
            sort(ro+i, ro+n, compare);
            ro[i]=v;

            done=true;
        }
    }
}
(Vissza)