Pages

Sunday 25 November 2012

UVA - 11470 - Square Sums


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int ind = 1;
        while (true) {
            int n = Integer.parseInt(br.readLine());
            if (n == 0) {
                break;
            }
            int arr[][] = new int[n][n];
            for (int i = 0; i < n; i++) {
                String[] str = br.readLine().split(" ");
                for (int j = 0; j < n; j++) {
                    arr[i][j] = Integer.parseInt(str[j]);
                }
            }
            sb.append("Case ").append(ind).append(":");
            int sum[] = squareSum(arr);
            for (int i = 0; i < sum.length; i++) {
                sb.append(" ").append(sum[i]);
            }
            sb.append("\n");
            ind++;
        }
        System.out.print(sb);
    }

    static int[] squareSum(int arr[][]) {
        int n = arr.length;
        int[] temp;
        if (n % 2 == 0) {
            temp = new int[n / 2];
        } else {
            temp = new int[(n / 2) + 1];
        }
        int i = 0, j = n;
        while (i < j) {
            for (int z = i; z < j; z++) {
                temp[i] += arr[z][j - 1] + arr[z][i];
            }
            for (int z = i + 1; z < j - 1; z++) {
                temp[i] += arr[i][z]+ arr[j - 1][z];
            }
            i++;
            j--;
        }
        if(n%2==1)
            temp[temp.length-1]/=2;
        return temp;
    }
}

No comments:

Post a Comment