-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
21-alstjr7437 #74
21-alstjr7437 #74
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋นํธ๋ง์คํน์ธ์ง ๋ชจ๋ฅด๊ณ ์ฒ์์ ์ธ๋ฒ์ ์๊ฐ์ด๊ณผ๋ฅผ ๋ง๋ฌ๋๋ฐ์..
์ด๋ป๊ฒ๋ ์ค์ฌ๋ณด๋ ค๊ณ hashSet ์ ์ฐ๊ณ ํ๋๋ฐ ๊ฒฐ๊ตญ ์คํจํ์ต๋๋ค.
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val n = br.readLine().toInt()
var set = hashSetOf<Int>()
for (i in 0 until n) {
val input = br.readLine().split(" ")
if (input[0] == "all") {
set = (1..20).toHashSet()
continue
}
if (input[0] == "empty") {
set.clear()
continue
}
val number = input[1].toInt()
when (input[0]) {
"add" -> set.add(number)
"check" -> if (set.contains(number)) println(1) else println(0)
"remove" -> set.remove(number)
"toggle" -> if (!set.remove(number)) set.add(number)
}
}
}
kotlin ๋นํธ๋ง์คํน ์ ๋ชฐ๋ผ์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ๋ฉด์ ๋ง๋ค์์ต๋๋ค.
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val n = br.readLine().toInt()
var bit = 0
val sb = StringBuilder()
for (i in 0 until n) {
val input = br.readLine().split(" ")
var number = 0
if (input[0] == "all") {
bit = bit or 0.inv()
continue
}
if (input[0] == "empty") {
bit = 0
continue
}
number = input[1].toInt()
when (input[0]) {
"add" -> bit = bit or (1 shl number - 1)
"check" -> {
val temp = bit and (1 shl number - 1)
sb.append(if (temp != 0) "1" else "0").append("\n")
}
"remove" -> bit = bit and (1 shl number - 1).inv()
"toggle" -> bit = bit xor (1 shl number - 1)
}
}
println(sb.toString())
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import sys
m = int(sys.stdin.readline())
S = set()
for _ in range(m):
temp = sys.stdin.readline().strip().split()
if len(temp) == 1:
if temp[0] == "all":
S = set([i for i in range(1, 21)])
else:
S = set()
else:
func, x = temp[0], temp[1]
x = int(x)
if func == "add":
S.add(x)
elif func == "remove":
S.discard(x)
elif func == "check":
print(1 if x in S else 0)
elif func == "toggle":
if x in S:
S.discard(x)
else:
S.add(x)
์ดํ ์ด์๋ฐ์ ์ด๊ฒ ๋ฆฌ๋ทฐ์ผ? |
๋น์ทํ๊ฒ ๋์์ ๊ทธ๋ฅ ์ฌ๋ ธ๋ค์..! ์ฃ์กํฉ๋๋ค ๐ข |
๐ ๋ฌธ์ ๋งํฌ
์งํฉ
๊ทธ๋ฅ ๋จ์ํ ์งํฉ ๋ฌธ์ ์ธ๋ฐ (์ฌ์ค ์๊ฐ๊ณผ ๋ชธ์ ๋ฌธ์ ๋ก ๋นํธ๋ง์คํน ์ดํด๋ง ํ๋ ๋ฌธ์ ๋ก ์ ์ ํ์ต๋๋ค.)
โ๏ธ ์์๋ ์๊ฐ
ํด๊ฒฐ์ 10๋ถ 3๊ฐ์ง ํ์ด๋ก ํ๋๋ฐ 25๋ถ ์ ๋..?
๐ ์ ์ฒด ๋ฌธ์ ํ์ด
์ผ๋จ ์ฒ์์ ๋ฌธ์ ๋ฅผ ์ ์ ํ๋ฉด์ ๊ตฌํ(set ์ด์ฉ), ๋นํธ๋ง์คํฌ๋ฅผ ์ด์ฉํด์ ํ์ด๋ด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ์ต๋๋ค.
1๏ธโฃ ์งํฉ
๊ทธ๋ฅ python์์ ์งํฉ(set)์ ์ฌ์ฉํด์ ๊ตฌํ์ผ๋ก ํ์ด๋ดค์ต๋๋ค.
set์ add, discard, in ๋ฑ์ ์ฌ์ฉํด์ ์งํฉ์ ์ฌ์ฉํ์ต๋๋ค!
2๏ธโฃ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ ๋นํธ๋ง์คํฌ ์ฌ์ฉ
์ผ๋จ ์งํฉ์ ์์๊ฐ 1~20๊น์ง๋ง ์์ผ๋ฉด ๋์ต๋๋ค.
๊ทธ๋์ 20๊ฐ์ 0์ผ๋ก ๋ค์ด๊ฐ ์๋ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ํด๋น ๋ถ๋ถ์์ add๊ฐ ๋ค์ด์ค๋ฉด ํด๋น ์๋ฆฌ๋ฅผ 1๋ก ๊ต์ฒดํ๋ ๋ฐฉ์์ผ๋ก ์งํํด์คฌ์ต๋๋ค.
3๏ธโฃ ๋นํธ๋ง์คํฌ result๋ง ์ด์ฉํด์
๋นํธ๋ง์คํฌ ํ๋ฉด 101 -> 5 ์ด๋ฐ์์ผ๋ก ๊ฐ ์ด์ง์ ํํ์ผ๋ก ์๋ฅผ ํํํ๋ ์๋ฃ๊ตฌ์กฐ ๊ธฐ๋ฒ์ด๋ฏ๋ก
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
ํ๋์ ๋ฌธ์ ๋ฅผ ํตํด์ ์ฌ๋ฌ๊ฐ์ง ํ์ด๋ฒ์ด ๋์ค๊ณ ๊ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด ๋ค๋ฅธ๊ฑธ ํ์ธํ์ต๋๋ค.
set ์๋ฃ๊ตฌ์กฐ๊ฐ ์ ์ผ ์ค๋ ๊ฑธ๋ ธ๊ณ list์ ๋นํธ๋ง์คํฌ๋ฅผ ํ์ฉํ ๋ฐฉ๋ฒ์ด ๋น์ทํ์ต๋๋ค.(list๊ฐ ๊ฐ๋ ๋๋นจ๋์ต๋๋ค..)
๋นํธ์ฐ์ฐ์ -> 3732
list -> 3632
set ์๋ฃ๊ตฌ์กฐ -> 5080