Código más eficiente

Iniciado por prometheus48, 1 Enero 2012, 15:59 PM

0 Miembros y 3 Visitantes están viendo este tema.

prometheus48

Hola,

Estaba yo con el ordenador y de repente se me ocurrió hacer un programa el cual te pide 3 números y después te dice cuál es el más grande.
Pero no solo eso, lo que quería era hacer un programa en C++, que sea lo mas rápido posible, que gaste muy pocos recursos y que ocupe muy poca memoria.
Osea, un programa simple, pero programado de tal forma que sea perfecto.
Hice un código muy simple intentando llamar lo menos posible a funciones,
bueno, aquí va mi código, espero que alguien me diga como mejorarlo, o que posteen
su propia versión

Código (cpp) [Seleccionar]
#include <iostream>



int main()
{
    int a,b,c;
    std::cout<<"Type 3 numbers ";
    std::cin>>a;
    std::cin>>b;
    std::cin>>c;
    if(a>b and a>c)
    {
           std::cout<<"The biggest number is \n" <<a<<std::endl;
           std::cin.get();
    }
    if(b>a and b>c)
    {
           std::cout<<"The biggest number is  \n"<<b<<std::endl;
           std::cin.get();
    }
    if(c>a and c>b)
    {
           std::cout<<"The biggest number is \n"<<c<<std::endl;
           std::cin.get();
    }       
    system("PAUSE");
    return EXIT_SUCCESS;
}
[/code==cpp] 
Salu2!
"Si tú tienes una manzana, y yo otra, y las intercambiamos, tu sigues teniendo una manzana, y yo sigo teniendo una manzana.
Pero, si tu tienes una idea, y yo otra, y nos las intercambiamos, tu tienes dos ideas, y yo tengo dos ideas"
The knowledge is free

folostia

Aquí te dejo mi versión,pero está en C:


/*el mayor de tres números*/
#include <stdio.h>
#include <stdlib.h>

int main(){
    int mayor,num1,num2;
    printf("Escribe tres numeros: ");
    scanf("%i %i %i", &max,&num1,&num2);
   
    if (max < num1){
        max=num1;
    }
    if (max < num2){
        max=num2;
    }   
    printf("\nEl mayor es: %i",max);
    system("PAUSE");
    return 0;
}
"Antes de que un software pueda ser reusable, primero ha de ser usable." (Ralph Johnson)
Usuario Linux:547941

Leyer

#2
Mi version, usar system pause? :-X

Código (cpp) [Seleccionar]

#include <iostream>
using namespace std;
int main(int argc,char *argv[]){
int n1,n2,n3;
cout<<"Type 3 numbers\n";
cin>>n1>>n2>>n3;
int _M=(n1>n2)?(n1>n3)?n1:n3:(n2>n3)?n2:n3;
       cout<<"The biggest number is " <<_M;
       cin.get();
       cin.get();
return 0;
}

alexis33de

#3
Mi versión hecha en Borland  ;D. No leí q decía sin funciones XD, nueva versión !!!
Código (cpp) [Seleccionar]
#include <iostream.h>
#include <conio.h>

int main()
{
int numeros[2];
   int max=0;
  cout<<"coloque los 3 numeros";
   for(int i=0;i<3;i++)
   {
    cin>>numeros[i];
        max=numeros[0];
       if(numeros[i]>max)
  {
    max=numeros[i];
         }

   }
  cout<<max;
  getch();
  return 0;
}

prometheus48

Aun más simplificado
Código (cpp) [Seleccionar]


#include <iostream>



int main()
{
    int a,b,c;
    std::cout<<"Type 3 numbers ";
    std::cin>>a>>b>>c;
    if(a>b and a>c)std::cout<<"The biggest number is \n" <<a;
    std::cin.get();
    if(b>a and b>c)std::cout<<"The biggest number is  \n"<<b;
    std::cin.get();
    if(c>a and c>b) std::cout<<"The biggest number is \n"<<c;
    std::cin.get();
    return 0;
}
[/code=cpp]
"Si tú tienes una manzana, y yo otra, y las intercambiamos, tu sigues teniendo una manzana, y yo sigo teniendo una manzana.
Pero, si tu tienes una idea, y yo otra, y nos las intercambiamos, tu tienes dos ideas, y yo tengo dos ideas"
The knowledge is free

Sagrini

El último NO es más simplificado que los otros, al contrario.
Os dejo el mío ;)

#include <stdio.h>

int main ()
{
int a, b, c; printf ("Escribe tres numeros: "); scanf ("%d %d %d", &a, &b, &c);
if (a<b) a=b; if (a<c) a=c; printf ("El mayor numero ingresado es %d\n\n", a);
getchar (); return 0;
}


ace332

#6
Otra más en C  ;D. No se cuan rápido sea, me dio flojera comparar :laugh:

#include <stdio.h>

#define permutar(a,b) {int t=a;a=b;b=t;}

int main()
{
 int a,b,c;
 scanf("%d%d%d",&a,&b,&c);
 if(b<a)permutar(a,b);
 if(c<b)permutar(b,c);
 printf("%d\n",c);
 return 0;
}


PD: Ahora que lo veo mejor... es el sort burbuja para 3 elementos

Saludos

Sagrini

El tuyo no me gusta :P Tanta llamada a funciones te retrasa mucho. El mío te supera (por milésimas, pero lo hace) en cuestión de tiempo y ciclos ^^

alexis33de

Y como se saca los tiempos, no se como se hace en mi compilador  ;D , seria bueno q alguien coloque los tiempos d todos los codigod  :rolleyes:

BlackZeroX

#9
...
en CPP
Código (cpp) [Seleccionar]


#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;

/// EntryPoint
int main(int argc, char *argv[])
{
    int a = 16,
        b = 1500,
        c = 14;

    cout << "El maximo valor fue: " << max(a,max(b, c)) << endl;
    cin.get();

    return EXIT_SUCCESS;
}



en C-ANSI:



#include <stdio.h>
#include <stdlib.h>
#define max(a,b) a>b?a:b

/// EntryPoint
int main(int argc, char *argv[])
{
    int a = 16,
        b = 15,
        c = 14;

    printf("El maximo valor fue: %d", max(a, max(b, c)));
    getchar();

    return EXIT_SUCCESS;
}



Tambien puedes suplantar max(a, max(b, c)) por:



((a>((b>c)?b:c))?a:((b>c)?b:c))
///o tambien por (Son identicos solo quite los parentesis cosa NO recomendada!¡):
a>b>c?b:c?a:b>c?b:c



En ambos codigos... pero no se entiende muy bien que digamos, mejor deja max(a, max(b, c)) ya que se entiende muchisimo mejor!¡.

Dulces Lunas!¡.
The Dark Shadow is my passion.