这道题目读到最后就是个求平均数的问题,这真的是一道水到极点的题目,本来以为会在数字表达上面有些难度,于是写了个round,然后两次compile error,系统不识别round函数,可是这在我的vs上面能跑的,没办法写了一个myround实现数据元整,后来发现完全没有必要,AC之后看了其他孩子的代码,压根就没数据后两位的处理,果然还是我想多了,水题,不推荐做。突然发现自己已经习惯拿到一个问题直接写一个类了,明明想用C来写的,不知道该高兴呢,还是该桑心呢
Source Code
Problem: 1004 | User: zhyh2010 | |
Memory: 220K | Time: 0MS | |
Language: C++ | Result: Accepted |
- Source Code
#include <iostream> #include <cmath> //#include <iomanip> using namespace std; class MEAN { public: MEAN(); ~MEAN(); void input(); void output(); void algorithm(); protected: int myround(float key); private: static const int MM = 12; float m_data[MM]; float m_aver; }; MEAN::MEAN() { /*m_data[0] = 100.00; m_data[1] = 489.12; m_data[2] = 12454.12; m_data[3] = 1234.10; m_data[4] = 823.05; m_data[5] = 109.20; m_data[6] = 5.27; m_data[7] = 1542.25; m_data[8] = 839.18; m_data[9] = 83.99; m_data[10] = 1295.01; m_data[11] = 1.75;*/ m_aver = 0; } MEAN::~MEAN() { } void MEAN::input() { for (int i = 0; i != MM; ++i) { cin >> m_data[i]; } } void MEAN::output() { cout << "$"<<m_aver << endl; } void MEAN::algorithm() { float sum = 0; for (int i = 0; i != MM; ++i) { sum += m_data[i]; } m_aver = sum / MM; int temp; temp = myround(m_aver * 100); m_aver = temp / 100.0; } int MEAN::myround(float key) { return floor(key + 0.5); } int main(int argc,char ** argv) { MEAN instance; instance.input(); instance.algorithm(); instance.output(); return 0; }