ACCEPTED !!! The opener in Dhaka regional 2004/05
____________________________________________________________________
Question: All Integer Average
____________________________________________________________________
#include "iostream.h"
#include "math.h"
#include "iomanip.h"
int HCF(int a, int b) {
if(a % b == 0) return b;
return HCF(b, a % b);
}
void printFraction(int n, int d) {
int neg = 0;
if(n < 0) {
neg = 1;
n *= -1;
}
if(d == 1) {
if(neg) cout << "- " << n << endl;
else cout << n << endl;
}
else {
int whole = n / d;
int fnum = n % d;
int fden = d;
int flen = int(floor(log(fden) / log(10)) + 1);
int wlen = 0;
if(whole > 0) wlen = int(floor(log(whole) / log(10)) + 1);
int offset = wlen + flen;
if(neg) offset += 2;
cout << setw(offset) << fnum << endl;
if(neg) cout << "- ";
if(whole > 0) cout << whole;
for(int i = 0; i < flen; i++) cout << "-";
cout << setw(offset) << fden << endl;
}
}
int main()
{
int count = 1;
while(1) {
int n;
cin >> n;
if(n == 0) break;
int sum, den = n;
cin >> sum;
for(int i = 2; i <= n; i++) {
int num;
cin >> num;
sum += num;
}
int c = HCF(abs(sum), den);
sum /= c;
den /= c;
cout << "Case " << count << ":\n";
printFraction(sum, den);
count++;
}
return 0;
}
____________________________________________________________________
Wednesday, December 12, 2007
All Integer Average
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment