Translate

Views

Friday, July 7, 2023

Solution UVA: 834 - Continued Fractions


 Problem  VerdictLangTimeBestRankSubmit Time
 | discuss834 - Continued Fractions AcceptedC++110.0000.00029461 mins ago

Suggest:

Example 43 19:

- Rule 
43 div 19 = [2;
43 mod 19 = 5

19 div 5 = 3,
19 mod 5 = 4

5 div 4 = 1,
5 mod 4 = 1

4 div 1 = 4]
4 mod 1 = 0 (r < 1 break)
 
- First i code from this rule, then fix WA in uDebug => Accepted

#include<bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);

    int x, y;
    while(cin >> x >> y){
        cout<<"["<<x/y;
        if (x%y) cout<<";";
        do{
            int r= x%y;
            x= y;
            y= r;
           
            if (y) cout<<x/y;
           

            if (r>1 && x%y) cout<<",";
            else{
                cout<<"]\n";
                break;  
            }
        }while(1);
    }
}

No comments: