题解 | #矩阵乘法计算量估算#

矩阵乘法计算量估算

https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sum = 0, len;
char* p;

int* rr_cc(int r_c[][2], int stack) {
    int* cul_rc = (int*)malloc(sizeof(int) * 2);
    cul_rc[0] = 1;cul_rc[1] = 1;

    int* next_rc;
    while (*p != '\0') {

        if ((*p >= 'A' && *p <= 'Z') && stack == 0) {
            cul_rc[0] = r_c[*p - 'A'][0];
            cul_rc[1] = r_c[*p - 'A'][1];
            p++;
            stack++;
        } else if ((*p >= 'A' && *p <= 'Z') && stack != 0) {
            sum += r_c[*p - 'A'][1] * cul_rc[0] * cul_rc[1];
            cul_rc[1] = r_c[*p - 'A'][1];
            p++;
            stack++;
        } else if (*p == '(' ) {
            p++;
            next_rc = rr_cc(r_c, 0);
            if(*p >= 'A' && *p <= 'Z' )
            {
                cul_rc[0] = next_rc[0];
                cul_rc[1] = next_rc[1];
                next_rc[0] = r_c[*p - 'A'][0];
                next_rc[1] = r_c[*p - 'A'][1];
                p++;
            }else if(*p == '(')
            {
                p++;
                cul_rc[0] = next_rc[0];
                cul_rc[1] = next_rc[1];
                next_rc = rr_cc(r_c, 0);
            }
            sum += cul_rc[0] * cul_rc[1] * next_rc[1];
            cul_rc[1] = next_rc[1];
        } else if (*p == ')') {
            p++;
            break;
        }
    }
    return cul_rc;
}

int main() {
    int num;



    while (scanf("%d", &num) != EOF) {
        int r_c[num][2];

        for (int i = 0; i < num; i++) {
            scanf("%d %d", &r_c[i][0], &r_c[i][1]);
        }
        char str[100];
        scanf("%s", str);




        len = strlen(str);
        p = str+1;

        int* sizee;
        sizee = rr_cc(r_c, 0);
        //printf("%d %d\n",sizee[0],sizee[1]);

        printf("%d", sum);
    }
}

全部评论

相关推荐

10-05 23:02
东北大学 Java
我说句实话啊:那时候看三个月培训班视频,随便做个项目背点八股,都能说3 40w是侮辱价
点赞 评论 收藏
分享
可可可可可_:nb啊,看样子是专科玩了几年随便专升本了个民办,又玩了两年。你这能找到我吃
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务