"SWEA 2056 연월일 달력"의 두 판 사이의 차이

9번째 줄: 9번째 줄:
==C++==
==C++==
<source lang='cpp'>
<source lang='cpp'>
#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> daysOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
    int T;
    cin >> T;
    string s;
    for(int tc=1; tc<=T; tc++) {
        cin >> s;
        int month = atoi(s.substr(4,2).c_str());
        int day = atoi(s.substr(6).c_str());
        cout << "#" << tc << " ";
        if( 1<=month && month<=12 && 1<=day && day<=daysOfMonth[month-1] ) {
            cout << s.substr(0,4) << "/" << s.substr(4,2) << "/" << s.substr(6) << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
}
</source>
</source>



2019년 1월 14일 (월) 22:04 판

1 개요

SWEA 2056 연월일 달력
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 1-1

2 C++

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> daysOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
    int T;
    cin >> T;
    string s;
    for(int tc=1; tc<=T; tc++) {
        cin >> s;
        int month = atoi(s.substr(4,2).c_str());
        int day = atoi(s.substr(6).c_str());
        cout << "#" << tc << " ";
        if( 1<=month && month<=12 && 1<=day && day<=daysOfMonth[month-1] ) {
            cout << s.substr(0,4) << "/" << s.substr(4,2) << "/" << s.substr(6) << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
}

3 Java

import java.util.Scanner;
class Solution {
    public static void main(String args[]) {
        int daysOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int tc=1; tc<=T; tc++) {
            String s = sc.next();
            int month = Integer.valueOf(s.substring(4,6));
            int day = Integer.valueOf(s.substring(6,8));
            String res = "-1";
            if( 1<=month && month<=12 && 1<=day && day<=daysOfMonth[month-1] ) {
                res = String.format("%s/%s/%s", s.substring(0,4), s.substring(4,6), s.substring(6,8));
            }
            System.out.format("#%d %s\n", tc, res);
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}