Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Problem12 {
  4. public static void main(String args[]) {
  5. Scanner stdin = new Scanner(System.in);
  6. System.out.println("Enter n: ");
  7. int n1 = Integer.parseInt(stdin.nextLine());
  8. System.out.println("Enter timestamps: ");
  9. String[] input = new String[n1];
  10. int[] line = new int[8];
  11. int[] result = new int[4];
  12.  
  13. for (int i = 0; i < n1; i++) {
  14. input[i] = stdin.nextLine();
  15. String[] splitLine = input[i].split(" ");
  16.  
  17. for (int j = 0; j < splitLine.length; j++) {
  18. line[j] = Integer.parseInt(splitLine[j]);
  19. }
  20. //calculations
  21. //convert timestamps to numbers – multiply
  22. //{day1 hour1 min1 sec1 day2 hour2 min2 sec2}
  23. for (int k : line) {
  24. switch (k) {
  25. //day
  26. case 0:
  27. case 4:
  28. line[k] = line[k] * 60 * 60 * 24;
  29. break;
  30. //hour
  31. case 1:
  32. case 5:
  33. line[k] = line[k] * 60 * 60;
  34. break;
  35. //minute
  36. case 2:
  37. case 6:
  38. line[k] = line[k] * 60;
  39. break;
  40. //second
  41. case 3:
  42. case 7:
  43. line[k] = line[k];
  44. break;
  45.  
  46. }
  47. }
  48. //calculate difference – subtract
  49. for (int l = 0; l < 4; l++) {
  50. result[l] = line[l + 4] - line[l];
  51. }
  52. //convert back to time format – divide
  53. for (int m = 0; m < 4; m++) {
  54. switch (m) {
  55. case 0:
  56. result[m] = (((((result[m] / 60) + result[m] % 60) + result[m] % 60) / 60) + result[m] % 24) / 24;
  57.  
  58. break;
  59. case 1:
  60. result[m] = (((result[m] / 60) + result[m] % 60) + result[m] % 60) / 60;
  61.  
  62. break;
  63. case 2:
  64. result[m] = (result[m] / 60) + result[m] % 60;
  65.  
  66. break;
  67. case 3:
  68. result[m] = result[m];
  69. break;
  70. }
  71. }
  72. System.out.print("(" + result[0] + " " + result[1] + " " + result[2] + " " + result[3] + ")" + " ");
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement