Egyptian Fraction
This file contains 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
#include <bits/stdc++.h> | |
using namespace std; | |
#define INF 1<<30 | |
#define MAX 10005 | |
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); | |
typedef long long ll; | |
void printEgyptian(int nr, int dr) | |
{ | |
if(dr == 0 || nr == 0) return; | |
if(dr%nr == 0){ | |
cout << "1/"<<(dr/nr); | |
return; | |
} | |
if(nr%dr == 0){ | |
cout << nr/dr; | |
return; | |
} | |
if(nr > dr){ | |
cout << nr/dr << " + "; | |
printEgyptian(nr%dr, dr); | |
return; | |
} | |
int n = dr/nr + 1; | |
cout << "1/" << n << " + "; | |
printEgyptian(nr*n-dr, dr*n); | |
} | |
int main() | |
{ | |
FASTIO | |
///* | |
//double start_time = clock(); | |
#ifndef ONLINE_JUDGE | |
freopen("in.txt", "r", stdin); | |
freopen("out.txt", "w", stdout); | |
freopen("error.txt", "w", stderr); | |
#endif | |
//*/ | |
int nr, dr; | |
cin >> nr >> dr; | |
cout << "Egyptian Fraction Representation of "<<nr << "/"<<dr<< " is \n"; | |
printEgyptian(nr,dr); | |
//double end_time = clock(); | |
//printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000); | |
return 0; | |
} |
No comments:
Post a Comment