![[백준 / BOJ] 2908번 상수 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbGJ6Zu%2FbtrJlFSATxo%2FHJcPvgKz3sIN8rJtKnFr61%2Fimg.png)
[백준 / BOJ] 2908번 상수 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 1. 17. 13:39
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/2908
2908번: 상수
상근이의 동생 상수는 수학을 정말 못한다. 상수는 숫자를 읽는데 문제가 있다. 이렇게 수학을 못하는 상수를 위해서 상근이는 수의 크기를 비교하는 문제를 내주었다. 상근이는 세 자리 수 두
www.acmicpc.net
문제

문제 풀이
C++ 상세 풀이
C++ 풀이 펼쳐보기
조건 덕분에 별로 어렵지 않았던 문제이다. 숫자에 0이 포함되어 있지 않고, 숫자도 3자리라는 제한 조건이 있으므로 swap과 stoi를 이용해 쉽게 해결할 수 있다.
void swap(string& str) {
char temp;
temp = str[0];
str[0] = str[2];
str[2] = temp;
}
void solution(string s1, string s2) {
swap(s1);
swap(s2);
int a = stoi(s1);
int b = stoi(s2);
if (a > b)
cout << a;
else
cout << b;
}
Python 상세 풀이
Python 풀이 펼쳐보기
파이썬은 슬라이싱을 통해 문자열을 바로 뒤집을 수 있으므로 쉽게 풀 수 있다.
A, B = input().split()
A = int(A[::-1])
B = int(B[::-1])
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
void swap(string& str) { | |
char temp; | |
temp = str[0]; | |
str[0] = str[2]; | |
str[2] = temp; | |
} | |
void solution(string s1, string s2) { | |
swap(s1); | |
swap(s2); | |
int a = stoi(s1); | |
int b = stoi(s2); | |
if (a > b) | |
cout << a; | |
else | |
cout << b; | |
} | |
int main() | |
{ | |
string s1, s2; | |
cin >> s1 >> s2; | |
solution(s1, s2); | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A, B = input().split() | |
A = int(A[::-1]) | |
B = int(B[::-1]) | |
if A > B: | |
print(A) | |
else: | |
print(B) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 2941번 크로아티아 알파벳 (C++, Python) (0) | 2022.01.17 |
---|---|
[백준 / BOJ] 5622번 다이얼 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 1152번 단어의 개수 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 1157번 단어 공부 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 2675번 문자열 반복 (C++, Python) (0) | 2022.01.17 |
@Reo :: 코드 아카이브
자기계발 블로그