8번 "1998년생인 내가 태국에서는 2541년생?!"

식을 직접 세워서 계산하는 문제

 

문제

ICPC Bangkok Regional에 참가하기 위해 수완나품 국제공항에 막 도착한 팀 레드시프트 일행은 눈을 믿을 수 없었다. 공항의 대형 스크린에 올해가 2562년이라고 적혀 있던 것이었다. 불교 국가인 태국은 불멸기원(佛滅紀元), 즉 석가모니가 열반한 해를 기준으로 연도를 세는 불기를 사용한다. 반면, 우리나라는 서기 연도를 사용하고 있다. 불기 연도가 주어질 때 이를 서기 연도로 바꿔 주는 프로그램을 작성하시오.

 

입력

서기 연도를 알아보고 싶은 불기 연도 y가 주어진다. (1000 ≤ y ≤ 3000)

 

출력

불기 연도를 서기 연도로 변환한 결과를 출력한다.

 

문제를 풀기 위해서는 불기와 서기의 차이에 대한 정보가 필요한데 제목에 힌트가 있다. 두 값의 차인 543의 값을 사용해서 불기를 서기로 표시할 수 있다.

 

"불기 - 543 = 서기"

 

C++

#include <iostream>
using namespace std;
int main(){
    int input;
    cin >> input;
    cout << input - 543;
}

 

C#

using System;
class Program{
    static void Main(string[] args){
        string input = Console.ReadLine();
        int val = int.Parse(input);
        Console.WriteLine(val - 543);
    }
}

 

Python

str = input()
val = int(str)
print(val - 543)

 

C#과 Python의 경우에는 입력은 문자열로만 반환되기 때문에 이를 정수계산하기 위해서는 타입 변환의 과정이 필요하다.

 

9번 "나머지"

문제

(A+B)%C는 ((A%C) + (B%C))%C 와 같을까? (A×B)%C는 ((A%C) × (B%C))%C 와 같을까? 세 수 A, B, C가 주어졌을 때, 위의 네 가지 값을 구하는 프로그램을 작성하시오.

 

입력

첫째 줄에 A, B, C가 순서대로 주어진다. (2 ≤ A, B, C ≤ 10000)

 

출력

첫째 줄에 (A+B)%C, 둘째 줄에 ((A%C) + (B%C))%C, 셋째 줄에 (A×B)%C, 넷째 줄에 ((A%C) × (B%C))%C를 출력한다.

 

C++

#include <iostream>
using namespace std;
int main(){
    int a, b, c;
    cin >> a;
    cin >> b;
    cin >> c;
    cout << (a+b)%c << endl;
    cout << ((a%c)+(b%c))%c << endl;
    cout << (a*b)%c << endl;
    cout << ((a%c)*(b%c))%c << endl;
    return 0;
}

 

C#

using System;
class Program{
    static void Main(string[] args){
        string input = Console.ReadLine();
        string[] arr = input.Split(" ");
        int a = int.Parse(arr[0]);
        int b = int.Parse(arr[1]);
        int c = int.Parse(arr[2]);
        
        Console.WriteLine((a+b)%c);
        Console.WriteLine(((a%c)+(b%c))%c);
        Console.WriteLine((a*b)%c);
        Console.WriteLine(((a%c)*(b%c))%c);
    }
}

 

Python

str = input();
arr = str.split(" ");
a = int(arr[0]);
b = int(arr[1]);
c = int(arr[2]);
print((a+b)%c);
print(((a%c)+(b%c))%c);
print((a*b)%c);
print(((a%c)*(b%c))%c);

 

이 문제는 받아쓰기만 틀리지 않고 잘하면 어려움이 없는 문제였다.

 

10번 "곱셈"

문제

(세 자리 수) × (세 자리 수)는 다음과 같은 과정을 통하여 이루어진다.

https://www.acmicpc.net/problem/2588

 

(1)과 (2)위치에 들어갈 세 자리 자연수가 주어질 때 (3), (4), (5), (6)위치에 들어갈 값을 구하는 프로그램을 작성하시오.

 

입력

첫째 줄에 (1)의 위치에 들어갈 세 자리 자연수가, 둘째 줄에 (2)의 위치에 들어갈 세 자리 자연수가 주어진다.

 

출력

첫째 줄부터 넷째 줄까지 차례대로 (3), (4), (5), (6)에 들어갈 값을 출력한다.

 

우선 입력받은 두 값을 곱셈하여 저장해 놓고 첫 번째 입력받은 값을 기준으로 하고 두 번째 입력값을 자릿수별로 잘라서 변수에 저장한다. 그리고 각 자릿수마다 기준 값과 곱한 값을 출력하고 처음 계산한 곱셈을 출력하면 될듯하다.

 

C++

#include <iostream>
#include <string>
using namespace std;
int main(){
    int val1;
    int val2;
    cin >> val1;
    cin >> val2;
    int result4 = val1*val2;
    
    string str = to_string(val2);
    int a = str[2] - '0';
    int b = str[1] - '0';
    int c = str[0] - '0';
    
    int result1 = val1*a;
    int result2 = val1*b;
    int result3 = val1*c;
    
    cout << result1 << endl;
    cout << result2 << endl;
    cout << result3 << endl;
    cout << result4 << endl;
    
    return 0;
}

 

C#

using System;
class Program{
    static void Main(string[] args){
        string str_1 = Console.ReadLine();
        string str_2 = Console.ReadLine();
        
        int val_1 = int.Parse(str_1);
        int val_2 = int.Parse(str_2);
        int a = (int)char.GetNumericValue(str_2[2]);
        int b = (int)char.GetNumericValue(str_2[1]);
        int c = (int)char.GetNumericValue(str_2[0]);
        
        int result_1 = val_1 * a;
        int result_2 = val_1 * b;
        int result_3 = val_1 * c;
        int result_4 = val_1 * val_2;
        
        Console.WriteLine(result_1);
        Console.WriteLine(result_2);
        Console.WriteLine(result_3);
        Console.WriteLine(result_4);
    }
}

 

C#에서 단일 문자의 경우 int.Parse는 에러가 발생한다. 따라서 문자를 정수로 전환하기 위해서 char.GetNumericValue를 사용한다. 

 

또는 문자가 숫자일 경우에는 아스키코드를 활용해서 정수로 변환할 수 있다.

아스키코드에서 '0' 문자는 48이다. 그리고 그 뒤로 연속으로 값이 증가하는데 즉 '1' - '0' = 49 - 48 이므로 결과는 1로 정수 값을 구할 수 있게 된다.

 

int a = (int)char.GetNumericValue(str_2[2]);
int b = (int)char.GetNumericValue(str_2[1]);
int c = (int)char.GetNumericValue(str_2[0]);

// ASCII
int a = str_2[2] - '0';
int b = str_2[1] - '0';
int c = str_2[0] - '0';

 

Python

str_1 = input()
str_2 = input()
val_1 = int(str_1)
val_2 = int(str_2)
a = int(str_2[2])
b = int(str_2[1])
c = int(str_2[0])
result_1 = val_1 * a
result_2 = val_1 * b
result_3 = val_1 * c
result_4 = val_1 * val_2
print(result_1)
print(result_2)
print(result_3)
print(result_4)
728x90
반응형

+ Recent posts