20250827 baekjoon
This commit is contained in:
parent
714cba765e
commit
8424b1dca2
29
code_study/Baekjoon/c/16953.c
Normal file
29
code_study/Baekjoon/c/16953.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int A, B;
|
||||||
|
scanf("%d %d",&A, &B);
|
||||||
|
|
||||||
|
int flag = 0;
|
||||||
|
int count = 1;
|
||||||
|
while(B >= A) {
|
||||||
|
if (A==B) {
|
||||||
|
flag = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (B%2==0) {
|
||||||
|
B /= 2;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else if (B%10==1) {
|
||||||
|
B /= 10;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%d\n", flag ? count : -1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
27
code_study/Baekjoon/swift/16953.swift
Normal file
27
code_study/Baekjoon/swift/16953.swift
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
if let input = readLine(), let nums = input.split(separator: " ").compactMap({Int($0)}) as? [Int], nums.count == 2 {
|
||||||
|
let A: Int = nums[0]
|
||||||
|
var B: Int = nums[1]
|
||||||
|
|
||||||
|
var count: Int = 1
|
||||||
|
var flag: Bool = false
|
||||||
|
while B >= A {
|
||||||
|
if A == B {
|
||||||
|
flag = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if B%2 == 0 {
|
||||||
|
B /= 2
|
||||||
|
count+=1
|
||||||
|
}
|
||||||
|
else if B%10 == 1 {
|
||||||
|
B = (B-1)/10
|
||||||
|
count+=1
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print(flag ? count : -1)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user