Skip to content
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

9-g0rnn #39

Merged
merged 3 commits into from
Dec 10, 2024
Merged

9-g0rnn #39

merged 3 commits into from
Dec 10, 2024

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Dec 7, 2024

πŸ”— 문제 링크

A->B

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

15m

✨ μˆ˜λ„ μ½”λ“œ

Aμ—μ„œ BκΉŒμ§€ 숫자λ₯Ό λ§Œλ“œλ €κ³  ν•˜λ©΄ μ–΄λ ΅κ³  Bμ—μ„œ A둜 숫자λ₯Ό λ§Œλ“€μ–΄μ•Όν•©λ‹ˆλ‹€.
그러면 이젠 κ°„λ‹¨ν•΄μ§‘λ‹ˆλ‹€. 짝수면 λ‚˜λˆ„λ©΄ 되고 1둜 λλ‚˜λ©΄ 1을 λΉΌμ£Όλ©΄ λ©λ‹ˆλ‹€!

ν•œκ°€μ§€ μ£Όμ˜μ μ€ λ§Œλ“€ 수 μ—†λŠ” 경우λ₯Ό 잘 μƒκ°ν•΄μ•Όν•©λ‹ˆλ‹€. μ΄λŠ” μ§μˆ˜λ„ μ•„λ‹ˆκ³  1둜 λλ‚˜μ§€ μ•ŠλŠ” 경우둜 Aμ—μ„œ BκΉŒμ§€ 숫자λ₯Ό λ§Œλ“œλŠ” 도쀑에 λ‚˜μ˜¬ 수 μ—†λŠ” 수λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ²˜μŒμ— Aλ₯Ό queue에 λ„£μ–΄μ„œ
1의 자릿수λ₯Ό 1둜 μΆ”κ°€ν•˜κ±°λ‚˜, 2λ₯Ό κ³±ν•˜λŠ” ν–‰μœ„λ₯Ό ν•˜κ³  λ‹€μ‹œ λ„£λŠ”
그런 λ‘œμ§μ„ μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.

κ΅¬ν˜„μ„ 쑰금 ν•˜λ‹€κ°€λ³΄λ‹ˆ B->A 둜 μ°Ύμ•„κ°€λŠ” 편이 였λ₯˜λ₯Ό μž‘κΈ°μ— 더 쉽닀고 μƒκ°ν•΄μ„œ
μ²˜μŒμ— μƒκ°ν•œ 방식은 λ˜μ‘ŒμŠ΅λ‹ˆλ‹€.

그리고 B->A 방식 κ΅¬ν˜„ 직전 κ· ν˜Έλ‹˜ μ„€λͺ… ν•œλ²ˆ 슀-윽 λ‘˜λŸ¬λ³΄κ³  μ‹œμž‘ν–ˆμŠ΅λ‹ˆλ‹€.

자릿수λ₯Ό λ‹€λ£¨κΈΈλž˜ string을 μ¨μ•Όν•˜λ‚˜ λΌλŠ” 생각도 μ•„μ£Ό μž μ‹œ ν–ˆλŠ”λ°
10을 λ‚˜λˆ„λ©΄ λ˜λŠ” κ²ƒμ΄λΌλŠ” 생각에 int둜 κ·ΈλŒ€λ‘œ μ§„ν–‰ν•˜μ˜€κ³ , μ •λ‹΅μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

μ„€λͺ…을 ν•œλ²ˆ 보고 ν’€μ–΄μ„œμΈμ§€,
κ· ν˜Έλ‹˜κ³Ό 풀이가 μ™„μ „νžˆ λ˜‘κ°™μŠ΅λ‹ˆλ‹€

CPP CODE
#include <iostream>

using namespace std;

int main() {
    int A, B;
    cin >> A >> B;

    int cnt = 1;
    while (A < B) {
        if (B % 2 == 0) B /= 2;
        else if (B % 10 == 1) B /= 10;
        else break;
        cnt++;
    }

    if (A == B) cout << cnt;
    else cout << -1;

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants