Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ZigZag02 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int rows = sc.nextInt();
- int cols = sc.nextInt();
- if (rows == 1 || cols == 1){
- System.out.println(getValue(0,0));
- return;
- }
- long result = getValue(0,0);
- int row = 1;
- int col = 1;
- int dRow = -1;
- int dCol = +1;
- while (!atCorner (row, col, rows, cols)) {
- result += getValue(row, col);
- int nextRow = row;
- int nextCol = col + dCol;
- if (nextCol<0 || nextCol > cols - 1) {
- dCol *= -1;
- dRow*=-1;
- }
- row += dRow;
- col += dCol;
- dRow *= -1;
- }
- result += getValue(row, col);
- System.out.println(result);
- }
- private static long getValue (int row, int col) {
- int sum = row + col;
- return (long) (sum*3)+1;
- }
- private static boolean atCorner(int row, int col, int rows, int cols) {
- return ((row <= 0 || row >= rows - 1) && (col <= 0 || col >= cols -1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement