题解 | #对称矩阵#

对称矩阵

https://www.nowcoder.com/practice/ad11ebc8d44842c78bb0bbfb6d07ad7a

#include <stdio.h>
#include <string.h>
char a[1000][1000];

//i是矩阵的维度
//i+1到i+dimension是矩阵的数据
//对矩阵数据进行判断,最后输出结果
void judge(int i, int dimension) {
    //先将数据读入到array数组中
    //printf("\n%d\n", dimension);
    int array[dimension][dimension];
    int m, n, value = 0, start;
    for (start = 0; start < dimension; start++) {
        for (m = 0, n = 0; m <= strlen(a[i + start]); m++) {
            if (a[i + start][m] == ' ' || m == strlen(a[i + start])) {
                //printf("%d ", value);
                array[start][n++] = value;
                value = 0;
                continue;
            }
            value = value * 10 + a[i + start][m] - '0';
        }
        //printf("--%d\n", strlen(a[i + start]));
    }

    //输出数组
    for (m = 0; m < dimension; m++) {
        for (n = 0; n < dimension; n++) {
            //printf("%d ", array[m][n]);
        }
        //printf("\n");
    }

    //判断数组是否是对称矩阵
    for (m = 0; m < dimension; m++) {
        for (n = m; n < dimension; n++) {
            if(array[m][n]!=array[n][m]){
                printf("No!\n");
                return ;
            }
        }
    }
    printf("Yes!\n");
    return ;
}

int main() {

    int number = 0;
    while (gets(a[number])) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to
        //printf("%s\n", a[number]);
        number++;
    }

    //printf("%s\n",a[0]);
    int i = 0, j, len, dimension = 0;
    while (i < number) {
        dimension = 0;
        len = strlen(a[i]);
        for (j = 0; j < len; j++) {
            dimension = 10 * dimension + a[i][j] - '0';
        }
        //printf("%d\n",dimension);
        judge(i + 1, dimension);
        i = i + dimension + 1;
    }
    return 0;
}

全部评论

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
整顿职场的柯基很威猛:这种不可怕,最可怕的是夹在一帮名校里的二本选手,人家才是最稳的。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务