Last active
April 18, 2018 10:41
-
-
Save jason790228/c332f5343007f5ca5adf38c9a1a3244b to your computer and use it in GitHub Desktop.
591
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <numeric> | |
using namespace std; | |
int main() | |
{ | |
size_t count_loop(1); | |
size_t brick_num(0); | |
while ((cin >> brick_num) && (brick_num != 0)) | |
{ | |
vector<size_t> bricks; | |
for (size_t i = 0; i < brick_num; i++) | |
{ | |
size_t temp_brick; | |
cin >> temp_brick; | |
bricks.push_back(temp_brick); | |
} | |
size_t avg_brick_num = accumulate(bricks.begin(), bricks.end(), 0) / brick_num; | |
size_t move_step(0); | |
for_each(bricks.rbegin(), bricks.rend(), [&](int n) { if (n > avg_brick_num) move_step += (n - avg_brick_num); }); | |
move_step -= accumulate(bricks.begin(), bricks.end(), 0) % brick_num; | |
cout << "Set #" << count_loop << endl; | |
cout << "The minimum number of moves is " << move_step << "." << endl << endl; | |
count_loop++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
count_loop 是動詞
用來命名 function 比較恰當
這裡應該要用 loop_count 吧
另外 lambda capture 選定要 capture 哪一個變數會更漂釀