반응형
문제
https://www.acmicpc.net/problem/15439
15439번: Vera and Outfits
Vera owns N tops and N pants. The i-th top and i-th pants have colour i, for 1 ≤ i ≤ N, where all N colours are different from each other. An outfit consists of one top and one pants. Vera likes outfits where the top and pants are not the same colour.
www.acmicpc.net
풀이
문제를 해석해보면 상의, 하의를 다른 색으로 입는 조합의 경우를 구하는 문제입니다.
예를 들어 N이 3이라면,
빨강, 파랑, 초록의 상,하의가 존재한다고 가정해봅시다.
그렇다면 나올 수 있는 조합은
빨파, 빨초, 파빨, 파초, 초빨, 초파 가 될 것입니다.
즉, 자기 자신과 같은 색이 아닌 n - 1개의 색상을 고를 수 있고, 총 색상이 n개 이므로
총 경우의 수인 n * (n - 1)을 출력해주면 되는 문제입니다.
소스코드
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
let n = Int(readLine()!)! | |
print(n * (n - 1)) |
후기
수식을 쉽게 떠올릴 수 있는 문제였습니다.
반응형
'PS > 백준' 카테고리의 다른 글
[BOJ] 백준 1010 다리 놓기 (Swift) (0) | 2023.04.03 |
---|---|
[BOJ] 백준 11050 이항 계수 1 (Swift) (0) | 2023.04.02 |
[BOJ] 백준 20920 영단어 암기는 괴로워 (Swift) (0) | 2023.04.02 |
[BOJ] 백준 26069 붙임성 좋은 총총이 (Swift) (0) | 2023.04.01 |
[BOJ] 백준 25192 인사성 밝은 곰곰이 (Swift) (0) | 2023.04.01 |