問題
Every person shakes hands once with every other person. A handshake between A and B is the same event as one between B and A. Nobody shakes their own hand.
Implement handshake_count(people).
Input
people: The number of people.
Output
Return the total number of distinct handshakes.
制約
0 ≤ people ≤ 1,000,000,000. The answer fits a signed 64-bit integer.
例
入力:
people = 4
期待される出力:
expected = 6
入力:
long long people = 4;
期待される出力:
long long expected = 6;
入力:
long people = 4;
期待される出力:
long expected = 6;
解説
The pairs are AB, AC, AD, BC, BD, and CD: six handshakes.
入力:
people = 1
期待される出力:
expected = 0
入力:
long long people = 1;
期待される出力:
long long expected = 0;
入力:
long people = 1;
期待される出力:
long expected = 0;
解説
One person has nobody else to greet.
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
Each handshake is just an unordered pair of distinct people.
ヒント 2
Count how many pairs can be formed from all people without double-counting.
ヒント 3
Be careful that zero or one person means no handshakes at all.
目安の計算量
O(1) time, O(1) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。