View on GitHub

Maratona-Extensao

Editorial Semana 10 - Recursividade

Lista de ExercĂ­cios 08

URI 1028 - Figurinhas
#include <bits/stdc++.h>
using namespace std;

int mdc(int x, int y){
    if (y==0) return x;
    return mdc (y, x % y);  
}

int mdc1(int x, int y){
    int resto;

    do{
        resto = x % y;

        x = y;
        y = resto;

    } while (resto != 0);

    return x;
}

int main() {
    
    int n;
    cin >> n;

    while(n--){
        int f1, f2;
        cin >> f1 >> f2;

        // cout << __gcd(f1, f2) << endl;
        // cout << mdc1(f1, f2) << endl;
        cout << mdc(f1, f2) << endl;
    }
        
    return 0;
}
URI 1153 - Fatorial Simples
#include <bits/stdc++.h>

using namespace std;

int f(int n){
    if(n==1 or n==0){
        return 1;
    }
    return n*f(n-1);
}

int main(){
    int n;
    cin >> n;
    cout << f(n) << endl;

    return 0;
}
URI 1161 - Soma de Fatoriais
#include <bits/stdc++.h>
using namespace std;

#define ll long long

ll fatorial(ll x){

    if(x == 0){
        return 1;
    }

    return fatorial(x-1)*x;

}

int main() {
    
    ll n, m;

    while(cin >> n >> m){

        ll res = fatorial(n) + fatorial(m);
        cout << res << endl;

    }
   
    return 0;
}
URI 1169 - Trigo no Tabuleiro
#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main(){
    int n, x;
    cin >> n;
    
    while(n--){
        ll tot = 0, graos = 0, gramas = 0, casa = 1;
        cin >> x;
        
        if(x == 63) cout << "768614336404564 kg" << endl;
        else if( x == 64) cout << "1537228672809129 kg" << endl;
        else{
            for(int j = 1; j <= x; j++) casa *= 2;  
            
            cout << (casa/12)/1000 << " kg" << endl;
        }
        
    }
    
    return 0;
}
URI 2166 - Raiz Quadrada de 2
#include <bits/stdc++.h>

using namespace std;

double fracao(double n){
    if(n==0){
        return 0; 
    } else if(n==1){
        return 1/2.0;
    }
    return 1.0/(2+fracao(n-1));
}

int main() {
    int n;
    cin >> n;
    
    cout.precision(10);
    cout.setf(ios::fixed);

    cout << 1.0+fracao(n) << endl;
	
    return 0;
}