[algorithm] 백준 2752번 세수정렬

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

 

 

우선 필자는 배열을 선언하고 입력받은 값을 배열 인덱스에 할당해준 후 for문을 활용하여 출력하는 방식으로

코드를 짰다. 

 

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int a;
    int arr[3] = {};

    for (int tmp = 0; tmp < 3; tmp++)
        {
            cin >> a;
            arr[tmp] = a;
        }
    sort(arr, arr+3);
    
    for (int b : arr)
        {
            cout << b << " ";
        }
    


    
    return 0;
}

 

 

다른 분들의 풀이를 확인해봤더니 배열을 선언하지 않고도 풀 수 있는 재미있는 방법이 있었다

// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/c58003e0efd546baad2e58671aa8f7f3
#include <bits/stdc++.h>
using namespace std;

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

  int a, b, c; // 입력
  cin >> a >> b >> c;
  int d, e, f; // 크기 순으로 출력할 세 수
  d = min({a,b,c});
  f = max({a,b,c});
  e = a+b+c-d-f;
  cout << d << ' ' << e << ' ' << f;
}

 

최솟값과 최댓값을 맨 앞과 맨 뒤로 두고

맨 처음 세 수를 더한 거에 최댓값 최솟값을 뺀 값을 중간값으로 설정하는 방식이었다

 

간단한 문제라도 풀이가 정말 다양하게 나올 수 있구나를 느꼈고

창의적인 풀이라고 생각했다.

 

참고로  입력값을 배열에 할당해줄 때

cin >> arr[i];

이런식으로 해줘도 무방하다.

 

반응형

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

[algorithm] 백준 2480번 주사위 세개  (0) 2025.10.12
[algorithm] 백준 2753번 윤년  (0) 2025.10.12
[algorithm] 백준 9498번 시험 성적  (0) 2025.10.12
[algorithm] 백준 10869번 사칙연산  (0) 2025.10.10
[algorithm] 백준 10171번 고양이  (0) 2025.10.10
'algorithm/기초 코드' 카테고리의 다른 글
  • [algorithm] 백준 2480번 주사위 세개
  • [algorithm] 백준 2753번 윤년
  • [algorithm] 백준 9498번 시험 성적
  • [algorithm] 백준 10869번 사칙연산
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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.3
20puddle
[algorithm] 백준 2752번 세수정렬
상단으로

티스토리툴바