今天去IBM二面,考了三道算法题,这是第一道。
这句就是:
- int i = (int)(a + 0.5);
- #include <stdio.h>
- int main()
- {
- float a;
- printf("Please Input a float and press enter:\n");
- scanf("%f",&a);
- int i = (int)(a/10.0 + 0.5)*10;
- int j = (int)(a + 0.5);
- float b = (int)((a * 100) + 0.5)/100.0;
- printf("i = %d\n",i);//个位四舍五入
- printf("j= %d\n",j);//四舍五入成整型
- printf("b = %0.2f\n",b);//保留两位小数
- return 0;
- }
运行结果: