Advertisement
cardel

Código 2n + 1 ejemplo

Feb 21st, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int problem(int a){
  5.       if(a==1){
  6.           return 1;
  7.      }
  8.      else{
  9.           if(a%2==0){
  10.               return 1 + problem(a/2);
  11.           }
  12.           else{
  13.              return 1 +problem(3*a+1);
  14.           }
  15.      }    
  16.  
  17. }
  18.  
  19. int main(){
  20.  
  21.       int a = 0;
  22.       int b = 0;
  23.      
  24.       while(cin >> a){
  25.           cin >> b;
  26.           int min = a;
  27.           int max = b;
  28.           if (a > b){
  29.               min = b;
  30.               max = a;
  31.           }
  32.           int count = 0;
  33.           for(int i=min; i<=max; i++){
  34.                  int aux=problem(i);
  35.                 if(aux>count) count = aux;
  36.           }
  37.           printf("%i %i %i\n",a,b,count);
  38.       }
  39.        
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement