Problem
Soon we will celebrate New Year 2022, but what does this number mean? As you possibly know, this dating system was invented in AD 525 by Dionysius Exiguus. He chose the birth of Jesus Christ as the starting point of the Years of Our Lord (Anno Domini in Latin, AD for short). All the years before that were counted backwards as years Before Christ (BC for short).
An interesting detail of this dating system is that there is no year 0 --- year 1 BC is immediately followed by AD 1. Because of that, sometimes it is quite tricky to find time difference between two dates if these dates belong to two different eras.
To simplify this task, please write a program that computes how many years passed between January 1st of two years given in the input.
Input
Two years are specified on two sequential input lines. Each year is specified in one of two forms:
- as letters AD, followed by a space and a positive integer without leading zeroes in range 1..9999;
- as a positive integer without leading zeroes in range 1..9999, followed by a space and letters BC.
The years may be specified in arbitrary order --- the earlier year is not necessarily given first.
Output
The only line of the output must contain one integer: the number of years that passed between January 1st of the earlier year and January 1st of the later year.
Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a=sc.next(),b=sc.next(),c=sc.next(),d=sc.next();
int x=0, y=0;
if(a.equals("AD"))
x=Integer.parseInt(b)-1;
else if(b.equals("BC"))
x=Integer.parseInt(a)*-1;
if(c.equals("AD"))
y=Integer.parseInt(d)-1;
else if(d.equals("BC"))
y=Integer.parseInt(c)*-1;
System.out.println(Math.abs(x-y));
}
}
'Algorithm > Baekjoon' 카테고리의 다른 글
Baekjoon 3053 택시 기하학 JAVA (0) | 2022.03.13 |
---|---|
Baekjoon 7662 이중 우선순위 큐 JAVA (0) | 2022.03.10 |
Baekjoon 17265 나의 인생에는 수학과 함께 JAVA (0) | 2022.03.10 |
Baekjoon 1926 그림 JAVA (0) | 2022.03.10 |
Baekjoon 5525 IOIOI JAVA (0) | 2022.03.10 |
댓글