w1 and w2 are the number of 1s when each number is replaced by a binary number, and d is the number of 0s and 1s separately in the same digits when comparing the two numbers.

I've solved it as much as I can, and I think the answer is correct, but they say it's wrong. If you can help me, please.

```
testcase= int(input())
for i in range(testcase):
num1, num2 = map(int, input().split())
b1 = int(bin(num1)[2:])
b2 = int(bin(num2)[2:])
s1 = bin(num1)[2:]
s2 = bin(num2)[2:]
ans1 = 0
ans2 = 0
while b1 :
b1 //= 10
ans1 += 1
while b2 :
b2 //= 10
ans2 += 1
w1 = 0
w2 = 0
d = 0
for j in range(ans1):
if s1[j] == '1' :
w1 += 1
for k in range(ans2):
if s2[k] == '1' :
w2 += 1
while ans1 > ans2 :
s2 ='0' + s2
ans2 += 1
if ans1 == ans2 :
for l in range(ans1):
if s1[l] != s2[l]:
d += 1
while ans2 > ans1 :
s1 ='0' + s1
ans1 += 1
if ans1 == ans2:
for m in range(ans1):
if s1[m] != s2[m]:
d += 1
print(w1,w2,d, sep=" ")
```

2022-09-20 10:20

It's hard to look into the code you wrote. (Because I'm not good enough...) So I just wrote a new one.

It seems that the problem is not particularly limited, so we solved it assuming that bit AND operation can be used.

```
# You can run it right away with "Execute Code".
num1 = 1
num2 = 17
print(int(bin(num1)[2:]), int(bin(num2)[2:]), sep=' /') # The binary conversion is done only once here and not in the actual processing.
w1 = 0
w2 = 0
d = 0
digit = 1
while digit <= num1 or digit <= num2 :
num1 ThisDigitEquals1 = bowl(num1 & digit) # This digit of num1 and digit are bit-comparison
num2ThisDigitEquals1 = bool(num2 & digit)
print(num1ThisDigitEquals1, num2ThisDigitEquals1, sep = ' vs ')
if (num1ThisDigitEquals1) :
w1 += 1
if (num2ThisDigitEquals1) :
w2 += 1
if (digit <= num1 and digit <= num2 and num1ThisDigitEquals1 != num2ThisDigitEquals1) :
d += 1
digit *= 2
print(w1, w2, d, sep = ' / ')
```

Your code seems to be counting something for each of `num1`

and `num2`

, and my code compares the two together. Anyway, after solving it like that, it seems to be a problem when the binary conversion digits of `num1`

and `num2`

are different.

If you look at the code above, there will be conditions that look unnecessarily long.

```
if (digit <= num1 and digit <= num2 and num1ThisDigitEquals1 != num2ThisDigitEquals1) :
```

This is a necessary condition even if it looks like this. Because the current while loop is "`num1`

or `num2`

until both sides are separated, ask and go to the next digit!" But **To compare two digits of a number, both of them must be as large as those digits.If either of them is not as large as the number of digits, there is no place to reverse and compare, so you have to burn and die.**

**So I need additional conditions for comparison. If one digit is too small to compare with the next digit, **

`if (num1ThisDigitEquals1 != num2ThisDigitEquals1) :`

**If you judge only with this, it seems that there is a bug where the d value goes up once more at the end. For 1 vs 17 of the above code, the correct answer for d value should be 0, but if you run it as it is, it becomes 1.**

**I'm not sure how the above approach and observation relate to your current code, whether it is related in the first place, etc. Please understand that I just tried it and take a look again. I hope it's useful.**

2022-09-20 10:20

Popular Tags

Popular Questions

© 2022 pinfo. All rights reserved.