UVA 10137

來源:互聯網
上載者:User

題目連結地址  10137 - The Trip

/** 10137 - The Trip* 作者 儀冰* 語言 C++* QQ 974817955** 精度處理一小弄。n個學生,每個學生有一個花費,* 求出平均值,求小於平均值的總和(less),* 求出大於平均值的總和(greatly),最後再進行less和grealy比較,輸出最大的。*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int SIZE = 1000;const double EXP = 1e-8;int main(){    int nStudents = 0;             //學生總數    double everyStudentCost[SIZE]; //儲存每個學生的花費    double totalCost = 0.0;        //花費總數    double average = 0.0;          //平均值    double less = 0.0;             //少於平均值    double greatly = 0.0;          //多於平均值    while (cin >> nStudents)    {        if (nStudents == 0)        {            break;        }        //初始化        totalCost = 0.0;        average = 0.0;        less = 0.0;        greatly = 0.0;        //輸入        for (int i=0; i<nStudents; i++)        {            cin >> everyStudentCost[i];        }        //求總花費        for (int i=0; i<nStudents; i++)        {            totalCost += everyStudentCost[i];        }        //求平均值        average = totalCost / nStudents;        //求less和greatly的值        for (int i=0; i<nStudents; i++)        {            if (everyStudentCost[i] < average)            {                //精度處理,保留兩位小數。先乘以100取其整數部分,然後再除以100.00                less += ((int)(100*(average - everyStudentCost[i])) / 100.00);            }            else            {                greatly += ((int)(100*(everyStudentCost[i] - average)) / 100.00);            }        }        //求出less和greatly中的最小值        if (less > greatly)        {            cout.precision(2);            cout.setf(ios::fixed | ios::showpoint);            cout << "$" << less << endl;        }        else        {            cout.precision(2);            cout.setf(ios::fixed | ios::showpoint);            cout << "$" << greatly << endl;        }    }    return 0;}


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.