描述 | |
---|---|
知识点 | 字符串 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入多行,先输入要计算乘法的矩阵个数n,每个矩阵的行数,列数,总共2n的数,最后输入要计算的法则 3 //矩阵个数n |
输出 | 输出需要进行的乘法次数 |
样例输入 | 3 50 10 10 20 20 5 (A(BC)) |
样例输出 | 3500 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cnt=0;
struct matrix
{
int row;
int column;
};
static int index=0;
struct matrix matrix_cnt(int n, struct matrix *matrix, char* exep)
{
int flag=0;
struct matrix cur_matrix;
struct matrix temp;
while(*(exep+index)!='\0')
{
if(*(exep+index)=='(')
{
index++;
if(flag==0)
{
//保存cur_matrix
cur_matrix = matrix_cnt(n, matrix, exep);
flag = 1;
}
else
{
//计算次数
temp = matrix_cnt(n, matrix, exep);
cnt += cur_matrix.row * cur_matrix.column * temp.column;
cur_matrix.column = temp.column;
}
}
else if(*(exep+index)==')')
{
index++;
return cur_matrix;
}
else if(*(exep+index)>='A'&&*(exep+index)<'A'+n)
{
if(flag==0)
{
cur_matrix.row = matrix[*(exep+index)-'A'].row;
cur_matrix.column = matrix[*(exep+index)-'A'].column;
flag = 1;
index++;
}
else
{
cnt+= cur_matrix.row * cur_matrix.column * matrix[*(exep+index)-'A'].column;
cur_matrix.column = matrix[*(exep+index)-'A'].column;
index++;
}
}
}
}
int main(void)
{
int n;
int k;
char exep[100]; char *ptr;
struct matrix *matrix, *matrix_temp;
scanf("%d", &n);
matrix_temp = matrix = (struct matrix*)malloc(n*sizeof(struct matrix));
for(k=0; k<n; k++)
{
scanf("%d %d", &matrix->row, &matrix->column);
matrix++;
}
scanf("%s", exep);
ptr = strupr(exep);
matrix_cnt(n, matrix_temp, ptr);
printf("%d",cnt);
//system("pause");
return 0;
}