Baekjun 1439 Python question

Asked 2 weeks ago, Updated 2 weeks ago, 3 views

S = input()
count = 0
for i in range(len(S)-1):
    if S[i] != S[i+1]:
        count += 1
print((count + 1) // 2)

This one and

S = int(input())
count = 0
for i in range(len(S)-1):
    if S[i] != S[i+1]:
        count += 1
print((count + 1) // 2)

This phrase and why doesn't it work at the bottom, s is an integer value

python

2022-09-20 10:47

2 Answers

The problem is, perhaps, asking about the each digit of any natural number.

If you treat that natural number as a string, the code becomes really simple.

But if you really treat that natural number as a natural number and solve the problem, it gets complicated.

That's why your code doesn't work. At least this much should be done so that S can be used as an integer.

# Not tested. Just look at how cumbersome and troublesome it is to take each digit of an integer.
S = int(input())
max10sq = 1
while S > max10sq :
  max10sq *= 10
max10sq /= 10
count = 0
while S > 0 :
  greaterDigit = ceil(S / max10sq)
  S %= max10sq
  smallerDigit = ceil(S / max10sq)
  max10sq /= 10
  if greaterDigit != smallerDigit :
    count += 1

I don't know if that's the answer.


2022-09-20 10:47

input returns the str object. You have to provide an int object, but it doesn't work, so it works differently.

a = input()
print(type(a)
print(type(int(a))


2022-09-20 10:47

If you have any answers or tips


© 2022 pinfo. All rights reserved.