U E D R , A S I H C RSS

The Trip/문보창

소감

2005/02/19 Accepted 0:00.029 64

코드

~cpp 
// no10137 - The Trip    
#include <iostream>    
#include <cmath>    
#include <cstdlib>    
using namespace std;    
 
const int MAX = 100;    
 
int exchangeMoney(const int * cost, const int n);    
void showExchange(const int * ex, const int count);    
 
int main()                          // cent단위로 계산    
{    
    int n;                          // 학생수           
    char money[7];                  // 돈    
    int exchangeCost[MAX];          // 교환값    
    int i, c;    
    int count = 0;    
    while (cin >> n)    
    {    
        if (n == 0)    
            break;    
        cin.get();    
        int costs[1000];            // 각 학생들의 지출 비용 
         for (i=0; i<n; i++)    
        {    
            c = 0;    
            while (cin.peek() != 'n')    
            {    
                 if (cin.peek() == '.')    
                      cin.get();    
                 else    
                      cin.get(money[c++]);    
            }    
            money[c] = '';    
            cin.get();    
            costs[i] = atoi(money);    
         }    
         exchangeCost[count++] = exchangeMoney(costs, n);    
    }    
    showExchange(exchangeCost, count);    
    return 0;    
}    
 
int exchangeMoney(const int * cost, const int n)    
{    
    int sum = 0;                    // 합계    
    int aver, r;                    // 평균, 나머지    
    int i;    
    for (i=0; i<n; i++)    
            sum += cost[i];    
    aver = sum/n;    
    r = sum%n;    
    int exchange = 0;    
    for (i=0; i<n; i++)    
    {    
        if (cost[i] > aver)    
        {    
            exchange += cost[i] - aver;    
            if (r > 0)    
            {    
                exchange--;    
                r--;    
            }    
        }    
    }    
    return exchange;    
}    
 
void showExchange(const int * ex, const int count)    
{    
    int i;    
    int dollor, cent;    
    for (i=0; i<count; i++)         // dollor단위로 출력    
    {    
        dollor = ex[i]/100;    
        cent = ex[i]%100;    
        cout << "$" << dollor << ".";    
        if (cent < 10)    
            cout << "0" << cent;    
        else    
            cout << cent;    
            cout << endl;    
    }    
}  
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:13
Processing time 0.0082 sec