0%

CCF错题本

简单错误

1、注意每个循环的结束位置。
2、算两个数的平均值,结果使用四舍五入。

1
2
3
4
5
6
7
8
9
10
11
12
13
float re = (database[index - 1] + database[index]) / 2.0;
if (re > 0)
{
mid = (int)floor((database[index - 1] + database[index]) / 2.0 + 0.5);
}
else if(re < 0)
{
mid = (int)floor((database[index - 1] + database[index]) / 2.0 - 0.5);
}
else
{
mid = 0;
}

3、使用cout不要指望它能自己输出有效位数,如果想要保留一位小数输出必须自己设置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (it = result.begin(); it != result.end(); it++)
{
if ((*it) != int(*it))
{
cout.setf(ios_base::fixed, ios_base::floatfield);//没有这个是一位有效数字
cout.precision(1);
//printf("%.1lf ", (*it));
}
else
{
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(0);
//printf("%d ", int(*it));
}
cout << (*it) << " ";
}