12342 - Tax Calculator
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 12342 Tax Calculator
You are using floating point with ceil and multiplying by 10.0
see: http://floating-point-gui.de/
see: http://floating-point-gui.de/
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 8
- Joined: Fri Jan 17, 2014 3:35 pm
Re: 12342 Tax Calculator
Why am I got wrong answer?
Code: Select all
got ACCEPTED. I used the wrong tax calculation algorithm before XD
Last edited by terry646623 on Wed Mar 12, 2014 11:24 am, edited 2 times in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 12342 Tax Calculator
brianfry713 wrote:Don't use floating point at all in this problem.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 8
- Joined: Fri Feb 28, 2014 9:21 am
Re: 12342 Tax Calculator
Getting WA. Can not figure out the 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;
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 12342 Tax Calculator
brianfry713 wrote:brianfry713 wrote:Don't use floating point at all in this problem.
Check input and AC output for thousands of problems on uDebug!
Re: 12342 - Tax Calculator
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.
By the way, I've shared my test cases on uDebug.
Re: 12342 - Tax Calculator
I got WA.
tested all the inputs in udebug..
I've searched for AC codes in google and there are so many ac codes with double type and ceil function like my code. Then what's wrong with my code..?
Here's 2 links of AC code's:
(Links are deleted after getting ac)
Here's my code... plz help...
Some other critical output can be of great help for debugging...
I'll delete the code and links aftre getting AC. thanks..

I've searched for AC codes in google and there are so many ac codes with double type and ceil function like my code. Then what's wrong with my code..?
Here's 2 links of AC code's:
(Links are deleted after getting ac)
Here's my code... plz help...
Code: Select all
Got AC...
I'll delete the code and links aftre getting AC. thanks..
Last edited by axelblaze on Mon Sep 22, 2014 4:29 pm, edited 2 times in total.
Re: 12342 - Tax Calculator
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.
Change your code
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;
Last edited by lighted on Mon Oct 06, 2014 4:15 pm, edited 2 times in total.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 12342 - Tax Calculator
Thanks a lot.. I got ac...



Re: 12342 - Tax Calculator
Instead of checking if tax is a floating point, ceil can be used.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- New poster
- Posts: 13
- Joined: Tue Jul 08, 2014 7:14 pm
Re: 12342 - Tax Calculator
can't understand y "46 no line ===> if(tax-result > 1e-6 ) result++; " is not working , here's my code
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;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 12342 - Tax Calculator
Don't use floating point at all in this problem.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 13
- Joined: Tue Jul 08, 2014 7:14 pm
Re: 12342 - Tax Calculator
To brainfry,
can't understand, what should i use instead of floating point ?? how can obey this condition "If the calculated tax is a floating point then it must be replaced by the smallest integer greater than the payable tax." without using floating point
can't understand, what should i use instead of floating point ?? how can obey this condition "If the calculated tax is a floating point then it must be replaced by the smallest integer greater than the payable tax." without using floating point
Re: 12342 - Tax Calculator
Actually Brainfry is right. It is always better to avoit floating point calculation because it is very easy to get precision error or other mistakes.
For this problem to avoid floating point you could multiple by some constant number to work only with integers. You could use long long for tax and all values of tax keep multiplied by 100.
However I solved this problem with floating point. You must be very very careful in this case. In your code you have integer division mistake. You must convert to double to avoid integer divison. It can be done in different ways.
Change your code to
Don't forget to remove your code after getting accepted. 
For this problem to avoid floating point you could multiple by some constant number to work only with integers. You could use long long for tax and all values of tax keep multiplied by 100.
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)
}
Change your code to
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);

A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 12342 - Tax Calculator
Hi guys,
I am getting WA consistently. I have checked Inputs frm uDebug. I found that my code doesn't allow me to work with the numbers like 10000000000 and few more like this.
How can I deal with such long floats...? Is there any way ?
My code will be deleted if the code gets Accepted.
I am getting WA consistently. I have checked Inputs frm uDebug. I found that my code doesn't allow me to work with the numbers like 10000000000 and few more like this.
How can I deal with such long floats...? Is there any way ?
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;
}
Last edited by vcrazy2 on Sun Oct 26, 2014 4:49 pm, edited 1 time in total.