原本認為這題非常麻煩

但是看了冰塊的想法後,再借用讀取"N = 3"這個字串的方法後,這題就變得很簡單了。

當成一維陣列去處理,小心數值範圍跟負數。

------------------------------------------------------------------

#include <stdio.h>

typedef long long int LLI;
#define Sym 1 /* symmetric */
#define Non_sym 0
#define MAX 101

/* Regard the two dimensional array as one dimensional array. */

int main()
{
    LLI _case, test = 1, flag, N, i, arr[MAX*MAX];

    scanf("%lld", &_case);
    while(_case--)
    {
        flag = Sym;
        char str[2];

        scanf("%s%s%lld", str, str, &N);

        for(i = 0; i < N*N; i++)
            scanf("%lld", arr+i);

        for(i = 0; i < N*N; i++)
            if(arr[i] != arr[(N*N-1) - i] || arr[i] < 0) /* Any element in symmetric matrix must be non-negative. */
                flag = Non_sym;

        printf("Test #%lld: %s.\n", test++, flag == Sym ? "Symmetric" : "Non-symmetric");
    }
    return 0;
}
 

----2017-06-24----

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package uva11349;

import java.util.Scanner;

/**
 *
 * @author awesq
 */
public class UVa11349 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Every element is non-negative.
        // Consider the two-dimensional square matrix as a one-dimensional linear array.
        int cases, N;
        Scanner sc = new Scanner(System.in);
        cases = sc.nextInt();

        for (int loop = 1; loop <= cases; loop++) {
            sc.nextLine(); // For the remaining line separator.
            String str = sc.nextLine();
            str = str.substring(4, str.length()); // "N = 33"
            str = str.replaceAll(" ", ""); // Sometimes there are some " " right after the number and will result in a exception.
            N = Integer.parseInt(str);

            // input
            int[] arr = new int[N * N];
            for (int i = 0; i < N * N; i++) {
                arr[i] = sc.nextInt();
            }

            // comparison
            int i, j;
            for (i = 0, j = (N * N) - 1; i < j; i++, j--) {
                if (arr[i] != arr[j] || arr[i] < 0 || arr[j] < 0) {
                    break;
                }
            }
            if (i >= j) {
                System.out.println("Test #" + loop + ": Symmetric.");
            } else {
                System.out.println("Test #" + loop + ": Non-symmetric.");
            }
        }
    }
}
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()