Re: 12342 Tax Calculator
Posted: Tue Dec 17, 2013 2:37 am
You are using floating point with ceil and multiplying by 10.0
see: http://floating-point-gui.de/
see: http://floating-point-gui.de/
Code: Select all
got ACCEPTED. I used the wrong tax calculation algorithm before XD
brianfry713 wrote:Don't use floating point at all in this problem.
Code: Select all
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
long long amount,tax;
int tc,i;
cin>>tc;
for(i=1; i<=tc; i++)
{
// tax = 0;
cin>>amount;
if(amount<=180000)
tax = 0;
else
{
amount -= 180000;
if(amount<=300000)
{
tax = ceil(amount*10/100);
}
else if(amount>300000 && amount <=700000)
{
// tax = 30000;
amount -= 300000;
tax = 30000 + ceil(amount*15/100);
}
else if(amount>700000 && amount <= 1000000)
{
// tax = 90000;
amount -= 700000;
tax = 90000 + ceil(amount*20/100);
}
else if(amount>1000000)
{
// tax = 150000;
amount -= 1000000;
tax = 150000 + ceil(amount*25/100);
}
if(tax < 2000)
tax = 2000;
}
cout<<"Case "<<i<<": "<<tax<<endl;
}
}
brianfry713 wrote:brianfry713 wrote:Don't use floating point at all in this problem.
Thanks for this really key piece of advice. I wrote the program one way but changed my approach when I came here looking for test cases and saw what you'd written.brianfry713 wrote:Don't use floating point at all in this problem.
Code: Select all
Got AC...
According to problem description we must print integer greater than tax in case of floating point. So must check this case.If the calculated tax is a floating point then it must be replaced by the smallest integer greater than the payable tax.
Code: Select all
break;
}
cout<<"Case "<<++count<<": ";
if(ans<2000 && ans!=0) ans=2000;
in = ans;
if (ans - in > 1e-6) in++;
cout<<in<<endl;
Code: Select all
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long long n,t,cnt;
double tax;
int result;
cin>>t;
cnt=1;
while(t--)
{
cin>>n;
if(n<=180000)
cout<<"Case "<<cnt<<": 0"<<endl;
else
{
if(n<=480000)
{
n=n-180000;
tax=((10*n)/100);
}
else if(n<=880000)
{
n=n-480000;
tax=30000+((15*n)/100);
}
else if(n<=1180000)
{
n=n-880000;
tax=30000+60000+((20*n)/100);
}
else if(n>1180000)
{
n=n-1180000;
tax=30000+60000+60000+((25*n)/100);
}
result=tax;
if(tax-result > 1e-6) result++;
if(result<2000)
cout<<"Case "<<cnt<<": 2000"<<endl;
else
cout<<"Case "<<cnt<<": "<<result<<endl;
}
cnt++;
}
return 0;
}
Code: Select all
tax= 10 * n;
..
tax = 30000 + 15 * n;
..
tax = 30000 + 60000 + 20 * n;
..
tax = 30000 + 60000 + 60000 + 25 * n;
..
if (tax % 100 == 0)
{
// answer is integer = tax / 100
// print tax / 100
} else {
// answer is floating point number = tax / 100
// print (tax + 99) / 100; // ceil (tax)
}
Code: Select all
tax=((10*n)/100.0);
..
tax=30000+((15.0*n)/100);
..
tax=30000+60000+(double(20*n)/100.0);
..
tax=30000+60000+60000+((double(25)*n)/100.0);
It works ok! But you can also change it to ceilRedCode119 wrote:can't understand y "46 no line ===> if(tax-result > 1e-6 ) result++; " is not working
Code: Select all
result = ceil(tax);
Code: Select all
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
// The Code got accepted...Thanks
// Original Code has been DELETED.
return 0;
}