只要加一個基準值 將相差小於基準值的兩數視做相同
#include <stdio.h>
const double capaity = 1e-6; //可以更小 1e-12;
// (99999 - 100000) / 100000 = -1e-5;
int main()
{
int goods, val, valn;
scanf("%d", &goods);
while (goods--)
{
scanf("%d%d", &val, &valn);
double rat = (double)(valn - val) / val * 100;
if(rat >= 0.0)printf("%.2lf%% ",rat+capaity);
else printf("%.2lf%% ",rat-capaity);
if (rat >= 10.0 || rat <= -7.0) printf("dispose\n");
else printf("keep\n");
}
return 0;
}
貌似沒有測到這個
if(rat >= 0.0)printf("%.2lf%% ",rat+capaity);
else if(rat - capaity > -0.005) printf("0.00%% "); //避免-0.00;
else printf("%.2lf%% ",rat-capaity);