Wednesday, October 10, 2007

Coupons

ACCEPTED in the 5th attempt !! Too many careless errors....
____________________________________________________________________
Question: Coupons
____________________________________________________________________

#include "iostream.h"
#include "math.h"
#include "iomanip.h"

long long int HCF(long long int big, long long int small) {
if(big % small == 0) return small;
return HCF(small, big % small);
}

long long int LCM(long long int a, long long int b) {
return a * b / HCF(a, b);
}

long long int nLCM(int n) {
if(n == 1) return 1;
return LCM(n, nLCM(n - 1));
}

int countDigits(long long int n) {
int i;
if(!n) return 1;
for(i = 0; n; i++) n/=10;
return i;
}

int max(int a, int b) {
return (a > b)? a : b;
}

int main() {
int n;
while(cin >> n) {
long long int numer = 0;
long long int denom = nLCM(n);
for(int k = 1; k <= n; k++) numer += denom / k;
denom /= n;
long long int hcf = HCF(numer, denom);
numer /= hcf;
denom /= hcf;
long long int whole = numer / denom;
numer = numer % denom;
int wn = countDigits(whole);
int fn = max(countDigits(numer), countDigits(denom));
if(numer == 0) cout << whole << endl;
else {
cout << setw(wn + 1) << " " << numer << endl;
cout << whole << " " << setw(fn) << setfill('-') << "" << endl;
cout << setw(wn + 1) << setfill(' ') << " " << denom << endl;
}
}
return 0;
}

____________________________________________________________________

No comments:

Contributors