Last active
August 29, 2015 14:07
-
-
Save codepainkiller/bc0269724c2462256437 to your computer and use it in GitHub Desktop.
Algoritmo de selección de actividades - C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * C++ - Algoritmo de selección de actividades | |
| * | |
| * Copyright 2014 Martin Cruz Otiniano | |
| * | |
| * Site: martincruz.me | |
| */ | |
| #include<iostream> | |
| #define MAX 50 | |
| using namespace std; | |
| int S[MAX] ; // solucion | |
| int c[MAX] ; // comienzo de cada actividad | |
| int f[MAX] ; // final de cada actividad | |
| /* Ingreso de datos | |
| ------------------------------------------------------------------------*/ | |
| void ingresar_actividades(int n) | |
| { | |
| for(int i=0; i<n; i++) | |
| { | |
| do | |
| { | |
| cout << " Actividad " << i+1 << endl<< endl ; | |
| cout << "\tinicio : " ; cin >> c[i] ; | |
| cout << "\tfinal : " ; cin >> f[i] ; | |
| cout << endl ; | |
| }while( c[i] > f[i] ) ; // el inicio no puede ser mayor que el final | |
| } | |
| } | |
| /* Mostrando solucion | |
| ------------------------------------------------------------------------*/ | |
| void mostrar_datos(int n) | |
| { | |
| cout<<" Actividades ingresadas. "<<endl<<endl ; | |
| cout<<"\t\t Ai : " ; | |
| for(int i=0; i<n; i++) | |
| cout<< i+1 <<' '; | |
| cout<<endl<<"\t\t-------------------------"<<endl ; | |
| cout<<"\t\t inicio : " ; | |
| for(int i=0; i<n; i++) | |
| cout<< c[i] <<' '; | |
| cout<<endl ; | |
| cout<<"\t\t fin : " ; | |
| for(int i=0; i<n; i++) | |
| cout<< f[i] <<' '; | |
| } | |
| /* Ordenando actividades | |
| ------------------------------------------------------------------------*/ | |
| void Ordenar ( int n) | |
| { | |
| int aux1, aux2, aux3, band = 1 ; | |
| for(int i=n-1; i>0 && band==1; i--) | |
| { | |
| band = 0; | |
| for(int j=0; j<i; j++) | |
| { | |
| if( f[j] > f[j+1]) | |
| { | |
| aux1 = f[j] ; | |
| f[j] = f[j+1] ; | |
| f[j+1] = aux1 ; | |
| aux2 = c[j] ; | |
| c[j] = c[j+1] ; | |
| c[j+1] = aux2 ; | |
| band = 1 ; | |
| } | |
| } | |
| } | |
| mostrar_datos(n) ; | |
| } | |
| /* Devolviendo solucion | |
| ------------------------------------------------------------------------*/ | |
| void devolver_solucion() | |
| { | |
| cout<<endl<<" Solucion voraz: \n\n\t" ; | |
| int i = 0 ; | |
| while( 1 ) | |
| { | |
| cout<<"A" << S[i] + 1 << "" ; | |
| i ++ ; | |
| if(S[i]==0) | |
| break ; | |
| } | |
| } | |
| /* Algoritmo de solucion | |
| ------------------------------------------------------------------------*/ | |
| void algoritmo_seleccion( int n ) | |
| { | |
| Ordenar ( n ) ; | |
| int z , k = 1 ; | |
| S[0] = 0 ; | |
| z = 0 ; | |
| for( int i=1; i<n; i++ ) | |
| { | |
| if( c[i] >= f[z] ) | |
| { | |
| S[k] = i ; // actividad seleccionada | |
| z = i ; | |
| k++ ; | |
| } | |
| } | |
| devolver_solucion() ; | |
| } | |
| /* Visualizancion en pantalla | |
| ------------------------------------------------------------------------*/ | |
| void pantalla() | |
| { | |
| cout<<endl ; | |
| cout<<"\t ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» "<<endl; | |
| cout<<"\t º ALGORITMO SELECCION DE ACTIVIDADES º "<< endl ; | |
| cout<<"\t º ---------------------------------------------- º "<< endl ; | |
| cout<<"\t ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ "<<endl<<endl; | |
| } | |
| /* Funcion Principal | |
| ------------------------------------------------------------------------*/ | |
| int main() | |
| { | |
| system("color 0b"); pantalla(); | |
| int N ; // numero de actividades | |
| cout<<" Ingrese numero de actividades: "; | |
| cin>> N ; | |
| cout<<endl ; | |
| ingresar_actividades( N ); | |
| system("cls"); pantalla(); | |
| algoritmo_seleccion( N ) ; | |
| cout<<endl<<endl ; | |
| system("pause"); | |
| return 0 ; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment