当前位置: 首页> 教育> 高考 > 【工资计算 / 2】

【工资计算 / 2】

时间:2025/7/13 1:12:17来源:https://blog.csdn.net/m0_73669127/article/details/142170153 浏览次数:0次

题目

a3be384e5a5c4cc5b6359f8c52c2e5a7.png

枚举

#include <bits/stdc++.h>
using namespace std;
int T;
int a[] = {0,1500,4500,9000,35000,55000,80000,1000000};
int b[] = {0,3,10,20,25,30,35,45};
int check(int x)
{if(x <= 3500) return x;x -= 3500;int tax = 0;for(int i = 1; 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()
{cin >> T;for(int x = 0; ; x += 100){if(check(x) == T){cout << x;break;}}return 0;
}

右区间二分

#include <bits/stdc++.h>
using namespace std;
int T;
int a[] = {0, 1500, 4500, 9000, 35000, 55000, 80000, 1000000};
int b[] = {0, 3, 10, 20, 25, 30, 35, 45};
int check(int x)
{if(x <= 3500) return x;x -= 3500;double tax = 0;for(int i = 1; i < 8; i++){if(x >= a[i-1]){tax += (double)(min(x, a[i]) - a[i-1]) / 100 * b[i];}}return x + 3500 - tax;
}
int main()
{cin >> T;int l = T, r = T * 2;while (l < r){int mid = (l + r) >> 1;if(check(mid) >= T) r = mid;else l = mid + 1;}cout << l;return 0;
}

左区间二分(错,注意答案必须是100的倍数,结合精度丢失,可以判断必须采用右区间二分)

比如check(10000) (右区间边界)= check(10001)(左区间边界) = 9255

check(9999) < 9255

如果右区间二分,会得到10000,左区间二分会得到 >= 10001的值

#include <bits/stdc++.h>
using namespace std;
int T;
int a[] = {0, 1500, 4500, 9000, 35000, 55000, 80000, 1000000};
int b[] = {0, 3, 10, 20, 25, 30, 35, 45};
int check(int x)
{if(x <= 3500) return x;x -= 3500;double tax = 0;for(int i = 1; i < 8; i++){if(x >= a[i-1]){tax += (double)(min(x, a[i]) - a[i-1]) / 100 * b[i];}}return x + 3500 - tax;
}
int main()
{cin >> T;int l = T, r = T * 2;while (l < r){int mid = (l + r + 1) >> 1;if(check(mid) <= T) l = mid;else r = mid-1;}cout << l;return 0;
}

关键字:【工资计算 / 2】

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: