玄学问题

题面传送门

不知道为啥取两位小数的时候我的代码运行出来会四舍五入
而题解代码不会

我的代码

#include <stdio.h>
int main(void)
{
	FILE *fp = fopen("data.txt","r");
	int i;
	double boundaries[] = {0, 1e5, 2e5, 4e5, 6e5, 1e6};
	double part[] = {0.1, 0.075, 0.05, 0.03, 0.015, 0.01};
	double in, ans = 0;
	fscanf(fp, "%lf", &in);
	for(i=5; i>=0; --i)
		if(in > boundaries[i])
		{
			ans = ans + part[i]*(in - boundaries[i]);
			in = boundaries[i];
		}
	fp = fopen("my.txt", "w");
	fprintf(fp, "%.2f", ans);
	return 0;
}

题解代码

#include "stdio.h"
int main()
{
	FILE *fp = fopen("data.txt", "r");
    double a,b;
    fscanf(fp, "%lf",&a);
    if(a<=100000)
    b=a*0.1;
    else if(100000<a && a<=200000)
    b=100000*0.1+(a-100000)*0.075;
        else if(200000<a  &&  a<=400000)
        b=100000*0.175 + (a-200000)*0.05;
            else if(400000<a  &&  a<=600000)
            b=100000*0.175 +200000*0.05 + (a-400000)*0.03;
                else if(600000<a  &&  a<=1000000)
                b=100000*0.175 + 200000*0.05 + 200000*0.03 + (a-600000)*0.015;
                    else if(a>1000000)
                    b=100000*0.175 + 200000*0.05 + 200000*0.03 + 400000*0.015 + (a-1000000)*0.01;
	fp = fopen("test.txt", "w");
    fprintf(fp, "%.2lf\n",b);
    return 0;
} 

数据生成器

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
	FILE *fp = fopen("data.txt", "w");
	srand(time(NULL));
	fprintf(fp, "%d", 100000+rand());
	return 0;
}

会出现差异的数据
输入:131989
我的输出:12399.18
题解输出:12399.17

经修改发现,如果取六位小数那么输出均为12399.175000

我直接裂开
希望各路网友帮忙指出错误,或者可能出现错误的地方

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章