More actions
imported>Unknown No edit summary |
(Repair batch-0003 pages from live compare) |
||
| Line 12: | Line 12: | ||
{ | { | ||
int n; // 학생 수 | int n; // 학생 수 | ||
int student | int student[1000]; // 학생들이 사용한 돈 | ||
int total; // 총합 | int total; // 총합 | ||
int aver; // 평균 | int aver; // 평균 | ||
int studentExchanged | int studentExchanged[1000]; // 공평하게 이동된 후의 학생들이 쓴 돈 | ||
int exchange; // 전체 이동되는 돈 | int exchange; // 전체 이동되는 돈 | ||
int temp; | int temp; | ||
| Line 28: | Line 28: | ||
double input; | double input; | ||
cin >> input; | cin >> input; | ||
student | student[i] = (input + 0.005) * 100; | ||
total += student | total += student[i]; | ||
} | } | ||
aver = total / n; | aver = total / n; | ||
for (i = 0; i < n; i++) | for (i = 0; i < n; i++) | ||
{ | { | ||
if (student | if (student[i] != aver) | ||
temp += student | temp += student[i] - aver; | ||
studentExchanged | studentExchanged[i] = aver; | ||
} | } | ||
for (i = 0; temp >= 1 && i < n; i++) | for (i = 0; temp >= 1 && i < n; i++) | ||
{ | { | ||
if (studentExchanged | if (studentExchanged[i] == aver) | ||
{ | { | ||
studentExchanged | studentExchanged[i] = aver + 1; | ||
temp -= 1; | temp -= 1; | ||
} | } | ||
| Line 52: | Line 52: | ||
for (j = i + 1; j < n; j++) | for (j = i + 1; j < n; j++) | ||
{ | { | ||
if (student | if (student[i] > student[j]) | ||
{ | { | ||
int temp; | int temp; | ||
temp = student | temp = student[i]; | ||
student | student[i] = student[j]; | ||
student | student[j] = temp; | ||
} | } | ||
if (studentExchanged | if (studentExchanged[i] > studentExchanged[j]) | ||
{ | { | ||
int temp; | int temp; | ||
temp = studentExchanged | temp = studentExchanged[i]; | ||
studentExchanged | studentExchanged[i] = studentExchanged[j]; | ||
studentExchanged | studentExchanged[j] = temp; | ||
} | } | ||
| Line 72: | Line 72: | ||
for (i = 0; i < n; i++) | for (i = 0; i < n; i++) | ||
{ | { | ||
if (studentExchanged | if (studentExchanged[i] > student[i]) | ||
{ | { | ||
exchange += studentExchanged | exchange += studentExchanged[i] - student[i]; | ||
} | } | ||
} | } | ||
| Line 90: | Line 90: | ||
---- | ---- | ||
[[TheTrip]] | [[TheTrip]] | ||
Latest revision as of 00:29, 27 March 2026
소감
- 통과O
- double형의 정밀도때문에 계속 삽질했음(예를 들어 9.03의 경우 9.029999999999...으로 입력됨). 지식검색 결과 컴퓨터구조상 어쩔 수 없다고 함. 문자열로 입력받는 방법말고 좋은 해결 방법있으면 가르쳐주세요.
- double형 함부로 쓸 게 못된다는걸 뼈저리게 느낌
- 알고리즘 - 평균값을 구한다음에 평균값보다 많이 쓴 돈(평균값 넘는 돈 - 평균값)이 이동하면 됨
소스
#include <iostream>
using namespace std;
int main()
{
int n; // 학생 수
int student[1000]; // 학생들이 사용한 돈
int total; // 총합
int aver; // 평균
int studentExchanged[1000]; // 공평하게 이동된 후의 학생들이 쓴 돈
int exchange; // 전체 이동되는 돈
int temp;
int i, j;
while (cin >> n && n != 0)
{
total = 0.0;
exchange = 0.0;
temp = 0.0;
for (i = 0; i < n; i++)
{
double input;
cin >> input;
student[i] = (input + 0.005) * 100;
total += student[i];
}
aver = total / n;
for (i = 0; i < n; i++)
{
if (student[i] != aver)
temp += student[i] - aver;
studentExchanged[i] = aver;
}
for (i = 0; temp >= 1 && i < n; i++)
{
if (studentExchanged[i] == aver)
{
studentExchanged[i] = aver + 1;
temp -= 1;
}
}
// 정렬
for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
{
if (student[i] > student[j])
{
int temp;
temp = student[i];
student[i] = student[j];
student[j] = temp;
}
if (studentExchanged[i] > studentExchanged[j])
{
int temp;
temp = studentExchanged[i];
studentExchanged[i] = studentExchanged[j];
studentExchanged[j] = temp;
}
}
for (i = 0; i < n; i++)
{
if (studentExchanged[i] > student[i])
{
exchange += studentExchanged[i] - student[i];
}
}
cout.setf(ios::fixed);
cout.precision(2);
cout << "$" << (exchange / 100.0) << endl;
}
return 0;
}