直接枚举
#include<iostream>
#include<cstring>
#include<cstring>
using namespace std;
int get(int x)
{
if(x<=3500) return x;
int a[] = {0,1500,4500,9000,35000,55000,80000,1000000};
int b[] = {0,3,10,20,25,30,35,45};
x-=3500;
int tax = 0;
for(int i = 0; i<8; i++)
if(x>=a[i-1])
tax += (min(x,a[i])-a[i-1])/100*b[i];
return x+=3500-tax;
}
int main()
{
int t;
cin>>t;
//已知小明这个月税后所得为 TT 元,请问他的税前工资 SS 是多少元。
for(int i = 0; ; i+=100)
if(get(i) == t)
{
cout<<i<<endl;
break;
}
return 0;
}
二分
#include<iostream>
#include<cstring>
#include<cstring>
using namespace std;
int get(int x)
{
if(x<=3500) return x;
int a[] = {0,1500,4500,9000,35000,55000,80000,1000000};
double b[] = {0,0.03,0.10,0.20,0.25,0.30,0.35,0.45};
if(x<=3500) return x;
int tax = x-3500;
double sum = 0;
for(int i = 0; i<8; i++)
{
if(tax>=a[i]) sum += (a[i]-a[i-1])*b[i];
else
{
sum += (tax - a[i-1])*b[i];
break;
}
}
return x-sum;
}
int main()
{
int t;
cin>>t;
//已知小明这个月税后所得为 TT 元,请问他的税前工资 SS 是多少元。
int l = t,r = t*2;
while(l<r)
{
int mid = (l+r)>>1;
if(get(mid) >= t) r = mid;
else l = mid+1;
}
cout<<l;
return 0;
}
二分(+0.5)四舍五入
#include<iostream>
#include<cstring>
#include<cstring>
using namespace std;
int get(int x)
{
if(x<=3500) return x;
int a[] = {0,1500,4500,9000,35000,55000,80000,1000000};
double b[] = {0,0.03,0.10,0.20,0.25,0.30,0.35,0.45};
x-=3500;
int tax = 0;
for(int i = 0; i<8; i++)
if(x>=a[i-1])
tax += (min(x,a[i])-a[i-1])*b[i]+0.5;
return x+=3500-tax;
}
int main()
{
int t;
cin>>t;
//已知小明这个月税后所得为 TT 元,请问他的税前工资 SS 是多少元。
int l = t,r = t*2;
while(l<r)
{
int mid = (l+r)>>1;
if(get(mid) >= t) r = mid;
else l = mid+1;
}
cout<<l;
return 0;
}