玄學問題

題面傳送門

不知道爲啥取兩位小數的時候我的代碼運行出來會四捨五入
而題解代碼不會

我的代碼

#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

我直接裂開
希望各路網友幫忙指出錯誤,或者可能出現錯誤的地方

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