一、
Input
The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.
Output
The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters
in the output.
很简单的一个题目,不过中间对于一个优先级问题困扰了好久,一步步尝试才找到问题。
temp=((int)(m*100))/100.0+((int)temp/5)*0.01;
temp=((int)m*100)/100.0+((int)temp/5)*0.01;
这两句代码,唯一的区别在于m*100是否加括号,但结果却不同,看来以前对于强制转换的认识有偏差。
比如=1.12,执行(int)(m*100) 得到112,而执行(int)m*100得到100。
最终AC的代码如下:
#include<iostream>
using namespace std;
int main()
{
double money=0;
float m;
for(int i=0;i<12;i++)
{
cin>>m;
money+=m;
}
m=money/12;
float temp=(int)(m*1000)%10;
temp=((int)(m*100))/100.0+((int)temp/5)*0.01;
cout<<"$"<<temp;
return 0;
}