题解:P15872 [NOI1998] 个人所得税

· · 题解

题解:P15872 [NOI1998] 个人所得税

看题没啥难度,硬模拟就行。

不过如果思路只有这么一行的话也不会有这么个题解了。

因为题很简单,但工程量又很大,分享几个可以让码量变小,更好看懂的方法。

思路一

因为题意已经很明确了,我们可以直接用 if-else 写。这样就写出了我们的第一份 AC 代码。

::::info[code]

#include <iostream>
#include <iomanip>
using namespace std;
int n;
struct qw
{
    int pay[20];
    int income[20];
}a[50010];
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    string info;
    cin >> info;
    double ans = 0;
    while(info != "#"){
        char tmp;
        int id, mm, dd, money;
        cin >> id >> mm >> tmp >> dd >> money;
        if(info == "PAY")
            a[id].pay[mm] += money;
        else{
            double tmp = money;
            if(tmp <= 4000)
                tmp -= 800;
            else
                tmp *= 0.8;
            if(tmp < 0)
                tmp = 0;
            if(tmp > 20000){
                ans += 20000 * 0.2;
                tmp -= 20000;
                if(tmp > 30000){
                    ans += 30000 * 0.3;
                    tmp -= 30000;
                    ans += tmp * 0.4;
                }
                else
                    ans += tmp * 0.3;
            }
            else
                ans += tmp * 0.2;
        }
        cin >> info;
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= 12; j++){
            int tmp = a[i].pay[j];
            if(tmp > 800){
                tmp -= 800;
                if(tmp > 500){
                    ans += 500 * 0.05;
                    tmp -= 500;
                    if(tmp > 1500){
                        ans += 150;
                        tmp -= 1500;
                        if(tmp > 3000){
                            ans += 3000 * 0.15;
                            tmp -= 3000;
                            if(tmp > 15000){
                                ans += 15000 * 0.2;
                                tmp -= 15000;
                                if(tmp > 20000){
                                    ans += 20000 * 0.25;
                                    tmp -= 20000;
                                    if(tmp > 20000){
                                        ans += 20000 * 0.3;
                                        tmp -= 20000;
                                        if(tmp > 20000){
                                            ans += 20000 * 0.35;
                                            tmp -= 20000;
                                            if(tmp > 20000){
                                                ans += 20000 * 0.4;
                                                tmp -= 20000;
                                                ans += tmp * 0.45;
                                            }
                                            else
                                                ans += tmp * 0.4;
                                        }
                                        else
                                            ans += tmp * 0.35;
                                    }
                                    else
                                        ans += tmp * 0.3;
                                }
                                else
                                    ans += tmp * 0.25;
                            }
                            else
                                ans += tmp * 0.2;
                        }
                        else
                            ans += tmp * 0.15;
                    }
                    else
                        ans += tmp * 0.1;
                }
                else
                    ans += tmp * 0.05;
            }
        }
    }
    cout << fixed << setprecision(2) << ans;
}

:::: 但是,如你所见,这种写法的码量过大,分支太多,很容易漏写东西或者把逻辑写乱。

这样就引出了以下几种方案。

思路二

不难发现,上述思路最大的痛点在超长的 if-else 里。如果我们可以把这个优化掉,就能省很多事。

我们可以使用数组,存下每一个档的区间金额限制和税率,在计算的时候,使用循环直接调用计算即可。

这样,我们就把手写的多层 if-else 转化成了一个循环,省事很多。

这样,我们就写出了我们的第二份 AC 代码。

::::info[code]

#include <iostream>
#include <iomanip>
using namespace std;
int n;
struct qw
{
    int pay[20];
    int income[20];
}a[50010];
const int pay_limit[] = {500, 1500, 3000, 15000, 20000, 20000, 20000, 20000};
const double pay_rate[] = {0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45};
const int income_limit[] = {20000, 30000};
const double income_rate[] = {0.20, 0.30, 0.40};
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    string info;
    cin >> info;
    double ans = 0;
    while(info != "#"){
        char tmp;
        int id, mm, dd, money;
        cin >> id >> mm >> tmp >> dd >> money;
        if(info == "PAY")
            a[id].pay[mm] += money;
        else{
            double tmp = money;
            if(tmp <= 4000)
                tmp -= 800;
            else
                tmp *= 0.8;
            if(tmp < 0)
                tmp = 0;
            for(int j = 0; j < 2; j++){
                if(tmp > income_limit[j]){
                    ans += income_limit[j] * income_rate[j];
                    tmp -= income_limit[j];
                }
                else{
                    ans += income_rate[j] * tmp;
                    tmp = 0;
                    break;
                }
            }
            ans += income_rate[2] * tmp;
        }
        cin >> info;
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= 12; j++){
            int tmp = a[i].pay[j];
            tmp = max(tmp - 800, 0);
            for(int i = 0; i < 8; i++){
                if(tmp > pay_limit[i]){
                    ans += pay_limit[i] * pay_rate[i];
                    tmp -= pay_limit[i];
                }
                else{
                    ans += pay_rate[i] * tmp;
                    tmp = 0;
                    break;
                }
            }
            ans += pay_rate[8] * tmp;
        }
    }
    cout << fixed << setprecision(2) << ans;
}

::::

思路三

其实这样,已经没有什么痛点了,但我们必须精益求精!

联系现实,我们不难发现一个东西:速算扣除数。

有了这个东西,我们可以把每一个月的应交税,用公式算出来,而不用循环。

联系速算扣除数的定义,我们可以写出以下代码,先把这个算出来: ::::info[计算工资、薪金收入的代码及结果]

#include <iostream>
using namespace std;
const double limit[] = {0, 500, 2000, 5000, 20000, 40000, 60000, 80000, 100000};
const double rate[] = {0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45};
double a[20];
int main() {
    a[0] = 0; 
    cout << "0";
    for (int i = 1; i < 9; i++) {
        a[i] = a[i - 1] + limit[i] * (rate[i] - rate[i - 1]);
        cout << ", " << a[i];
    }
    return 0;
}

输出:

0, 25, 125, 375, 1375, 3375, 6375, 10375, 15375

:::: ::::info[计算一次性劳务报酬收入的代码及结果]

#include <iostream>
using namespace std;
const double limit[] = {0, 20000, 50000};
const double rate[] = {0.20, 0.30, 0.40};
double a[20];
int main() {
    a[0] = 0; 
    cout << "0";
    for (int i = 1; i < 3; i++) {
        a[i] = a[i - 1] + limit[i] * (rate[i] - rate[i - 1]);
        cout << ", " << a[i];
    }
    return 0;
}

输出:

0, 2000, 7000

:::: 我们再按这个思路,用 lower_bound 找出每一笔收入对应的区间,用公式算出要交的税,就可以 AC 了。 ::::info[code]

#include <iostream>
#include <iomanip>
using namespace std;
int n;
struct qw
{
    int pay[20];
    int income[20];
}a[50010];
const int pay_limit[] = {0, 500, 2000, 5000, 20000, 40000, 60000, 80000, 100000};
const double pay_rate[] = {0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45};
const double pay_deduction[] = {0, 25, 125, 375, 1375, 3375, 6375, 10375, 15375};
const int income_limit[] = {0, 20000, 50000};
const double income_rate[] = {0.20, 0.30, 0.40};
const double income_deduction[] = {0, 2000, 7000};
int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    string info;
    cin >> info;
    double ans = 0;
    while(info != "#"){
        char tmp;
        int id, mm, dd, money;
        cin >> id >> mm >> tmp >> dd >> money;
        if(info == "PAY")
            a[id].pay[mm] += money;
        else{
            double tmp = money;
            if(tmp <= 4000)
                tmp -= 800;
            else
                tmp *= 0.8;
            if(tmp <= 0){
                cin >> info;
                continue;
            }
            int k = lower_bound(income_limit, income_limit + 3, tmp) - income_limit - 1;
            ans += tmp * income_rate[k] - income_deduction[k];
        }
        cin >> info;
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= 12; j++){
            int tmp = a[i].pay[j];
            tmp -= 800;
            if(tmp <= 0)
                continue;
            int k = lower_bound(pay_limit, pay_limit + 9, tmp) - pay_limit - 1;
            ans += tmp * pay_rate[k] - pay_deduction[k];
        }
    }
    cout << fixed << setprecision(2) << ans;
}

:::: 好了,我们成功为一道橙题写了三种解法,你也来试试吧!