Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Problem12 {
- public static void main(String args[]) {
- Scanner stdin = new Scanner(System.in);
- System.out.println("Enter n: ");
- int n1 = Integer.parseInt(stdin.nextLine());
- System.out.println("Enter timestamps: ");
- String[] input = new String[n1];
- int[] line = new int[8];
- int[] result = new int[4];
- for (int i = 0; i < n1; i++) {
- input[i] = stdin.nextLine();
- String[] splitLine = input[i].split(" ");
- for (int j = 0; j < splitLine.length; j++) {
- line[j] = Integer.parseInt(splitLine[j]);
- }
- //calculations
- //convert timestamps to numbers – multiply
- //{day1 hour1 min1 sec1 day2 hour2 min2 sec2}
- for (int k : line) {
- switch (k) {
- //day
- case 0:
- case 4:
- line[k] = line[k] * 60 * 60 * 24;
- break;
- //hour
- case 1:
- case 5:
- line[k] = line[k] * 60 * 60;
- break;
- //minute
- case 2:
- case 6:
- line[k] = line[k] * 60;
- break;
- //second
- case 3:
- case 7:
- line[k] = line[k];
- break;
- }
- }
- //calculate difference – subtract
- for (int l = 0; l < 4; l++) {
- result[l] = line[l + 4] - line[l];
- }
- //convert back to time format – divide
- for (int m = 0; m < 4; m++) {
- switch (m) {
- case 0:
- result[m] = (((((result[m] / 60) + result[m] % 60) + result[m] % 60) / 60) + result[m] % 24) / 24;
- break;
- case 1:
- result[m] = (((result[m] / 60) + result[m] % 60) + result[m] % 60) / 60;
- break;
- case 2:
- result[m] = (result[m] / 60) + result[m] % 60;
- break;
- case 3:
- result[m] = result[m];
- break;
- }
- }
- System.out.print("(" + result[0] + " " + result[1] + " " + result[2] + " " + result[3] + ")" + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement