Hi everybody. I am trying to solve the problem id 10137 on UVA online:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=1078
It is asked to give the amount of money exchanged so that every
student has the same money (within a cent: 0.01)
Input: n:number of student then n lines each containing the amount in
dollars and cent spent by a student
Output: a line stating the total amount of money, in dollars and
cents, that must be exchanged to equalize the students' costs( within
a cent).
Sample Input:
4
15.00
15.01
3.00
3.01
Sample Output:
$11.99
Here is my C++ program
// START
#include <iostream>
using namespace std;
long int exec(long int *tab,long int n);
int main() {
long int n=0;
double *tab;
long int *tab1;
while(1)
{
cin>>n;
if(n==0)
break;
else
{
tab1=new long int[n];
tab=new double[n];
for(int i=0;i<n;i++)
{
// take the input as double
// then multiply it by 100
// and convert to long int
cin>>tab[i];
tab1[i]=100*tab[i];
}
// print result
double ans=exec(tab1,n)/100.0;
cout<<"$"<<ans<<endl;
}
}
return 0;
}
// To calculate the total amount exchanged
// Every amount is multiplied by 100
long int exec(long int *tab,long int n)
{
long int sum=0,sum1=0,number1=0,number=0,diff=0;
// The amount(goal) every student will have is the sum of all
// amount divided by the number of students.
for(int i=0;i<n;i++)
sum+=tab[i];
long int goal=sum/n,goal1=goal;
// If sum/n is not an integer then the amount could
// also be goal+1
// "number" is the total number of students who will have
// goal+1
if(sum%n!=0)
{
number=sum-goal*n;
goal1=goal+1;
}
// The total amount exchanged is the (minimal) difference
// between all students costs (so that their cost is high than goal)
// and their new costs((goal+1)*number+goal*(number1-number))
// number1 is the total number of students(H) so that their cost
// are high than goal
// sum1 is the total costs of students H
for(int i=0;i<n;i++)
{
if(tab[i]>goal)
{
sum1+=tab[i];
number1++;
}
}
if(number<number1)
diff=number1-number;
if(number>number1)
number=number1;
return sum1-(goal1)*number-(goal*diff);
}
// END
But Iam always getting WRONG ANSWER. I don't know where I am wrong.
Thanks for your help.
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.