티스토리 뷰
Difficulty : Gold V
Problem Description
Sungwon is on a diet.
Sungwon remembered previous weight, and he took a new weight.
He screamed because his weight was increased.
He said 'G kilagrams', G is the difference between square of current weight and square of previous weight.
The output is all weights possible with Sungwon's current weight.
The weight must be natural number. If there aren't any weight, print '-1'.
Solution
Because the weight must be natural number, I took advantages of it by using square root.
Math is for library, and 'G' is the input.
I is the minimum weight. If the square of the weight is less than 'G', it can't be able to get any current weight.
'count' means the count of current weights to identify if there are not any current weight.
If the number increases, the difference between the square of two numbers gets bigger and bigger.
If the difference between 'i^2' and '(i-1)^2' is equal to 'G', it is the largest number 'i' can be.
By rearranging the expression, 'i^2 - i^2 + 2i - 1 <= G -> 2i <= G+1'. It is in the 5th line.
In each sequence, if the square root of 'i^2 - G' is natural number, 'i' can be current weight. So print and count it.
Be careful of the case that 'i^2' is equal to 'G'. In this case, the previous weight must be 0, but 0 is not appropriate for weight. So except this case.
If 'count' is 0, it means there are not any current number. So print '-1'.
Today's English
take advantage of ~ : ~를 활용하다
square root : 제곱근
square : 제곱
rearrange : 재정렬하다
expression : 식
appropriate : 적절한
Today's Code
math.ceil : 올림
'프로그래밍 문제풀이 > 백준 문제풀이' 카테고리의 다른 글
2212번: 센서 (0) | 2022.11.22 |
---|---|
1388번: 바닥 장식 (0) | 2022.11.19 |
1188번: 음식 평론가 (0) | 2022.11.15 |
1111번: 색칠 1 (0) | 2022.11.15 |
1548번: 부분 삼각 수열 (0) | 2022.11.11 |