알고리즘/C++
코드업 기초 100제 : 1019 C++ (cout.width cout.fill)
지휘리릭
2020. 2. 15. 14:01
입력받은 연, 월, 일을 yyyy.mm.dd 형식으로 출력한다.
(%02d를 사용하면 2칸을 사용해 출력하는데, 한 자리 수인 경우 앞에 0을 붙여 출력한다.)
이렇게 비효율적으로 푸는게 맞는지 모르겠다. 분명 적합한 함수가 있을텐데..
C 가 굉장히 다 짜야하는 비효율적인 언어라고 생각했는데,,, C++ 아직 낯설다 ㅠ
cout.width : 출력 자릿수 설정
cout.fill() : 출력 자릿수를 맞추고 빈 자리를 무엇으로 채울건지
#include<iostream>
using namespace std;
int main() {
int y, m, d;
cin >> y;
cin.ignore(256, '.');
cin >> m;
cin.ignore(256, '.');
cin >> d;
cout.width(4); cout.fill('0');
cout << y << ".";
cout.width(2); cout.fill('0');
cout << m << ".";
cout.width(2); cout.fill('0');
cout << d << endl;
}