10137 - Need help
Posted: Sat Nov 23, 2002 9:02 pm
I think it is a easy problem, but i am getting WA. Please help me with some tricky input and output
Thanks
Thanks
Code: Select all
if (value[i] < average) {
totall += average - value[i];
}
Code: Select all
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <string>
#include <strstream>
using namespace std;
int main()
{
int A, amounts[1000], d, e, i, h, j, k, n, p, R, S, T;
string a, b, ch;
strstream ss;
cin >> n;
while (n) {
for (i=0; i<n; ++i) {
d=0;
p=0;
cin >> a;
S=a.size();
for (j=0; j<S; ++j) {
if (d) ++p;
if (a[j]!='.') b+=a[j];
else ++d;
}
ss << b;
ss >> h;
for (k=0; k<2-p; ++k)
h*=10;
amounts[i]=h;
ss.clear();
b.erase();
}
T=accumulate(amounts, amounts+n, 0)/n;
sort(amounts, amounts+n);
A=0;
R=0;
e=0;
for (i=0; i<n; ++i)
if (amounts[i]<T)
R+=(T-amounts[i]);
if (accumulate(amounts, amounts+n, 0)%n) {
for (i=0; i<n; ++i)
if (amounts[i]>T) {
A+=(amounts[i]-T);
++e;
}
R+=(A-R)-e;
}
ch=R%100<10?".0":".";
cout << "$" << R/100 << ch << R%100 << endl;
cin >> n;
}
return 0;
}
Code: Select all
3
10.01
15.25
18.96
0
Code: Select all
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <numeric>
#include <string>
#include <strstream>
using namespace std;
int main()
{
int A, amounts[1000], d, e, i, h, j, k, n, p, R, S, T;
string a, b, ch;
strstream ss;
cin >> n;
while (n) {
for (i=0; i<n; ++i) {
d=0;
p=0;
cin >> a;
S=a.size();
for (j=0; j<S; ++j) {
if (d) ++p;
if (a[j]!='.') b+=a[j];
else ++d;
}
ss << b;
ss >> h;
for (k=0; k<2-p; ++k)
h*=10;
amounts[i]=h;
ss.clear();
b.erase();
}
T=accumulate(amounts, amounts+n, 0)/n;
e=accumulate(amounts, amounts+n, 0);
e=e%n;
sort(amounts, amounts+n);
R=0;
i=0;
j=n-1;
while (i<j) {
if (e && amounts[j]-amounts[i]<=1) break;
if (!e && amounts[j]==amounts[i]) break;
++amounts[i];
--amounts[j];
++R;
if (e) {
if(amounts[i]==T+1) ++i;
if(amounts[j]==T+1) { --j; --e; }
} else {
if(amounts[i]==T) ++i;
if(amounts[j]==T) --j;
}
}
ch=R%100<10?".0":".";
cout << "$" << R/100 << ch << R%100 << endl;
cin >> n;
}
return 0;
}
Code: Select all
4
25.00
25.00
25.00
28.00
0