Advertisement
cardel

Prueba discretas

Jun 19th, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int ecuacionRecurrencia(int n){
  7.     switch(n){
  8.         case 1:
  9.             return 2;
  10.         case 2:
  11.             return 4;
  12.         case 3:
  13.             return 7;
  14.         case 4:
  15.             return 12;
  16.         default:
  17.             return 2*ecuacionRecurrencia(n-1)-ecuacionRecurrencia(n-2)+ecuacionRecurrencia(n-3);
  18.     }
  19. }
  20.  
  21. string inttobinary(int n){
  22.     string salida = "";
  23.     if(n>0){
  24.         while(n >= 1){
  25.             salida = to_string(n%2)+salida;
  26.             n = n/2;       
  27.         }
  28.        
  29.         if(n==1){
  30.             salida = "1"+salida;
  31.         }
  32.     }
  33.     else{
  34.         salida = "0";
  35.     }
  36.     return salida;
  37. }
  38.  
  39.  
  40. int main(){
  41.  
  42.     //cout << inttobinary(10) << endl;
  43.     for(int i=1; i<=30; i++){
  44.         int contador = 0;
  45.         for(int j=0; j<pow(2,i); j++){
  46.             string equivalente = inttobinary(j);
  47.             size_t found = equivalente.find("101");
  48.             if(found==string::npos){
  49.                 //cout << equivalente << endl;
  50.                 contador++;
  51.             }
  52.         }
  53.        
  54.         cout << "Bits " <<i<<" : "<<contador<<" Ecuacion recurrencia "<<ecuacionRecurrencia(i)<<endl;
  55.    
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement