본문 바로가기

programming/algorithm 공부

[Algorithm] Handshake / 악수


> Quest.

At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes?



> Input.

Input Format 

The first line contains the number of test cases T, T lines follow. 

Each line then contains an integer N, the total number of Board of Directors of Acme.



> Output.

Output Format

Print the number of handshakes for each test-case in a new line.


Constraints

1 <= T <= 1000 

0 < N < 106


Sample Input


Sample Output

1



> solve

요약하자면, 1) 첫 입력값 T 는 '몇번 돌려볼 것인가?' 입니다. 2) 그 아래 입력값들은 사람 수 입니다. 사람 수에 따라 몇번 악수가 이루어 지겠는가 하는 문제입니다. 


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int T; 
    scanf("%d",&T);
    for(int a0 = 0; a0 < T; a0++){
        int N; 
        scanf("%d",&N);
        N = N*(N-1)/2;
        printf("%d\n", N);
    }
    return 0;
}

이 문제는 중고등학교 시절 시그마 공식을 이용하였습니다.




반응형