728x90
반응형
정수 n이 주어질 때, n을 문자열로 변환하여 return하도록 solution 함수를 완성해주세요.
#include <string>
#include <vector>
using namespace std;
string solution(int n) {
string answer = "";
answer = to_string(n);
return answer;
}
프로그래머스에 새로운 문제도 떴길래 바로 하나 선택해서 풀어보았다.
정수형을 문자열로 변환해주는 것이 키포인트였다.
to_string() 함수를 알았다면 바로 해결할 수 있는 문제였다고 생각한다.
정수 >> 문자열 : to_string()
문자열 >> 정수 : stoi()
https://notepad96.tistory.com/67
C++ String to int, int to String - 문자열 숫자 형변환
1. 문자열 숫자 간 형변환 문자열(string)을 숫자(int)로 형변환하기 위해서 stoi 함수를 사용할 수 있다. stoi 이는 string to integer이 축약된 단어이며 마찬가지로 double형으로 변환하고 싶다면 stod를, lon
notepad96.tistory.com
이 블로그 게시물을 통해 쉽게 이해할 수 있다
다른 분들의 풀이도 꼭 확인하는 편!
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string solution(int n) {
stringstream ss;
ss << n;
string answer = ss.str();
return answer;
}
#include <string>
#include <vector>
#include <sstream>
using namespace std;
string solution(int n) {
stringstream itos;
itos << n;
return itos.str();
}
itos 와 << 연산자를 사용해서도 풀 수 있다
728x90
반응형
'프로그래머스 C++ > Level.0' 카테고리의 다른 글
프로그래머스 C++ Level. 0 편지 (0) | 2023.06.25 |
---|---|
프로그래머스 C++ Level. 0 배열 자르기 (0) | 2023.04.27 |
프로그래머스 C++ Level. 0 중앙값 구하기 (0) | 2023.04.25 |
프로그래머스 C++ Level. 0 특정 문자 제거하기 (0) | 2023.04.12 |
프로그래머스 C++ Level. 0 삼각형의 완성조건(1) (0) | 2023.04.11 |