Instantly share code, notes, and snippets.

@jason790228 /591.cpp
Last active Apr 18, 2018

Embed
What would you like to do?
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

This comment has been minimized.

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