Skip to content

Instantly share code, notes, and snippets.

@jason790228
Last active April 18, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason790228/c332f5343007f5ca5adf38c9a1a3244b to your computer and use it in GitHub Desktop.
Save jason790228/c332f5343007f5ca5adf38c9a1a3244b to your computer and use it in GitHub Desktop.
591
#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;
}
@separation
Copy link

separation commented Apr 18, 2018

count_loop 是動詞
用來命名 function 比較恰當
這裡應該要用 loop_count 吧

另外 lambda capture 選定要 capture 哪一個變數會更漂釀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment