valitomi

Untitled

Apr 27th, 2021 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.  
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. int a = sc.nextInt();
  9. int b = sc.nextInt();
  10. if(a==1||b==1){
  11. System.out.println(1);
  12. } else {
  13. long[][] matrix = new long[a][b];
  14. long num = 1;
  15. long sum = 1;
  16.  
  17.  
  18.  
  19. for (int i = 0; i < matrix.length; i++) {
  20. for (int j = 0; j < matrix[matrix.length - 1].length; j++) {
  21. matrix[i][j] = num;
  22. num *= 2;
  23. }
  24. num = matrix[i][1];
  25. }
  26.  
  27. int i = 0;
  28. int j = 0;
  29. int c = 1;
  30. int d = 1;
  31. while (true) {
  32. if (i == a - 1) {
  33. c *= -1;
  34. }
  35. if (i == 0) {
  36. c = 1;
  37. }
  38. i = i + c;
  39. if (j == b - 1) {
  40. d *= -1;
  41. }
  42. if (j == 0) {
  43. d = 1;
  44. }
  45. j = j + d;
  46. sum += matrix[i][j];
  47.  
  48. if ((i == 0 && j == 0) || (i == a - 1 && j == b - 1) || (i == a - 1 && j == 0) || (j == b - 1 && i == 0)) {
  49. System.out.println(sum);
  50. break;
  51. }
  52. }
  53. }
  54.  
  55.  
  56.  
  57. }
  58. }
Add Comment
Please, Sign In to add comment