[algorithm] 백준 10093번 숫자

2025. 10. 12. 12:33·algorithm/기초 코드
반응형

 

 

두 수 사이에 있는 수의 개수는 두 수를 뺀 값에 -1 을 해주면 되고

두 수 사이에 있는 수는 for 문으로 a 에서 +1, +2, +3 ... 해주는 식으로 설정했다

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int a,b;
    cin >> a >> b;

    cout << b-a-1 << "\n";
    int c = a;
    
    for (int tmp = 1; tmp < (b-a); tmp++)
        {
            if (c != b)
            {
                cout << a + tmp << " ";
                c = a + tmp;
            }
        }
    return 0;
}

 

 

하지만 틀렸다는 결과를 받았다

 

생각을 해보니 a가 b보다 작다는 말이 명시적으로 주어지지 않았다

a가 b보다 큰 경우도 처리해주어야 한다

 

또한 for문 안에 if문이 굳이 필요가 없었다

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int a,b;
    cin >> a >> b;

    if (a > b)
    {
        cout << a-b-1 << "\n";
        int c = b;
    
        for (int tmp = 1; tmp < (a-b); tmp++)
            {
                cout << b + tmp << " ";
                c = b + tmp;
            
            }
    }

    else if (a < b)
    {
        cout << b-a-1 << "\n";
        int c = a;
    
        for (int tmp = 1; tmp < (b-a); tmp++)
            {
                cout << a + tmp << " ";
                c = a + tmp;
            
            }
    }

    
    return 0;
}

 

이번에는 a와 b가 같은경우와 차이가 1인 경우를 처리해주지 않아 틀렸다

그 경우에는 그냥 0을 출력하도록 처리해주면 된다

 

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    
    int a,b;
    cin >> a >> b;

    if (a > b)
    {
        cout << a-b-1 << "\n";
        int c = b;
    
        for (int tmp = 1; tmp < (a-b); tmp++)
            {
                cout << b + tmp << " ";
                c = b + tmp;
            
            }
    }

    else if (a < b)
    {
        cout << b-a-1 << "\n";
        int c = a;
    
        for (int tmp = 1; tmp < (b-a); tmp++)
            {
                cout << a + tmp << " ";
                c = a + tmp;
            
            }
    }

    else
        cout << 0;


    
    return 0;
}

 

 

이번에는 30점을 받게되었다

 

 

서브태스크를 보니 int 범위를 벗어나는 값을 처리해주지 않아 overflow가 발생한 것이였다

즉 long 또는 long long 타입을 사용해야 한다

 

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    
    long long a,b;
    cin >> a >> b;

    if (a > b)
    {
        cout << a-b-1 << "\n";
        
        for (long long tmp = 1; tmp < (a-b); tmp++)
            {
                cout << b + tmp << " ";
            }
    }

    else if (a < b)
    {
        cout << b-a-1 << "\n";
        
    
        for (long long tmp = 1; tmp < (b-a); tmp++)
            {
                cout << a + tmp << " ";
            }
    }

    else
        cout << 0;

    return 0;
}

 

이런식으로 해주면 되고

앞선 풀이 들을 보면 c 변수를 따로 선언했는데

굳이 그럴 필요가 없었어서 없애주었고

long long이 아닌 long으로 해줘도 정답처리가 되긴했다

 

또 다른 창의적인 풀이가 있어서 소개하겠다

// Authored by : pha-ran (soft18)
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/c6e91442976c4a2a8e6c626462aceced
#include <bits/stdc++.h>
using namespace std;

int main(void){
  ios::sync_with_stdio(0);
  cin.tie(0);
  
  long long a, b;
  cin >> a >> b;
  if (a > b) swap(a,b);
  if (a == b || b - a == 1) cout << 0;
  else {
    cout << b - a - 1 << "\n";
    for(long long i = a+1; i < b; i++)
      cout << i << " ";
  }
}

a가 b보다 큰 경우 swap으로 처리를 해줬고

i 값을 출력 값으로 설정하여 처리한 부분이 인상깊었다.

반응형

'algorithm > 기초 코드' 카테고리의 다른 글

[algorithm] 백준 2576번 홀수  (0) 2025.10.18
[algorithm] 백준 2490번 윷놀이  (0) 2025.10.18
[algorithm] 백준 2480번 주사위 세개  (0) 2025.10.12
[algorithm] 백준 2753번 윤년  (0) 2025.10.12
[algorithm] 백준 2752번 세수정렬  (0) 2025.10.12
'algorithm/기초 코드' 카테고리의 다른 글
  • [algorithm] 백준 2576번 홀수
  • [algorithm] 백준 2490번 윷놀이
  • [algorithm] 백준 2480번 주사위 세개
  • [algorithm] 백준 2753번 윤년
20puddle
20puddle
20puddle 님의 블로그 입니다.
  • 20puddle
    20puddle 님의 블로그
    20puddle
  • 전체
    오늘
    어제
    • 분류 전체보기 (100)
      • Spring boot (5)
      • git & github (5)
      • algorithm (47)
        • theory (3)
        • 배열 (7)
        • 연결 리스트 (3)
        • 스택 (5)
        • 큐 (3)
        • 덱 (2)
        • 기초 코드 (19)
        • BFS (5)
      • AI (43)
        • Machine Learning (35)
        • Deep Learning (8)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Aimers
    딥러닝
    인공지능
    홀드아웃 검증
    DL
    Java
    ML
    오프라인 학습
    AI
    git
    알고리즘
    게시판
    list
    주성분변환
    Spring Boot
    백준
    연결리스트
    github
    머신러닝
    그레디언트 클리핑
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.3
20puddle
[algorithm] 백준 10093번 숫자
상단으로

티스토리툴바