[softeer level1] 주행거리 비교하기(C++, JAVA, Python)

lsh235·2023년 2월 6일
0

CodingTest

목록 보기
4/7
post-thumbnail

문제

https://softeer.ai/practice/info.do?idx=1&eid=1016
첫째줄에 두차량 A,B의 주행거리가 한칸의 공백을 두고 주어진다.
비교하여 주행거리가 큰 차량을 출력 같을 경우 same

해석

두 차량의 주행거리를 비교하여 주행거리가 더 큰 차량을 출력하고 같을경우 same을 출력하더록 한다.

구현

  1. 주행거리 A B를 입력받는다.
  2. 비교하여 더 큰 차량을 출력한다.

C++

#include<iostream>

using namespace std;

int main(int argc, char** argv)
{
	int A, B;
  	// 1.
	cin >> A >> B;

  	// 2.
	if (A < B) {
		cout << "B";
	} else if (A > B) {
		cout << "A";
	} else if (A == B) {
		cout << "same";
	}

	return 0;
}

JAVA

import java.util.*;
import java.io.*;

public class Main
{
  	// 1.
    public static void main(String args[]) throws IOException {
        BufferedReader ranges = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(ranges.readLine(), " ");

        int A = Integer.parseInt(st.nextToken()); 
        int B = Integer.parseInt(st.nextToken()); 

  		// 2.
        if(A < B) {
            System.out.println("B");
        } else if(A > B) {
            System.out.println("A");
        } else if (A == B) {
            System.out.println("same");
        }
    }
}

Python

import sys

// 1.
A, B = map(int, input().split())

// 2.
if(A < B):
    print("B")
if(A > B):
    print("A")
if(A == B):
    print("same")

새로 알게 된 부분

Python
1. A, B = sys.stdin.readline().split()로 입력을 받았는데 99999와 100000 비교시 A가 더 크게 인식이 되었다. 타입을 확인해보니 str로 받아져서 그런 것 같다.
2. 파이썬으로 코딩테스트시 input()대신 sys.stdin.readline()을 쓰는 경우가 많은데 input()으로 입력을 받을경우 시간초과가 발생 할 수도 있기 때문이다.
3. 고로 정해진 개수를 입력 받을때는 map을 활용해
A, B = map(int,sys.stdin.readline().split()) 다음과 같이 입력 받을 수 있도록 하자

0개의 댓글