uva442 Matrix Chain Multiplication

简介: uva442 Matrix Chain Multiplication
#include <stdio.h>#define LOCAL#define MAXN 200typedefstructelem{
charmatrix;
introw;
intcol;
}elem;
typedefstructstackelem{
introw;
intcol;
}elemstack;
elemdata[26];
elemstackstack[MAXN];
intmain()
{
intn;
inti;
charstr[2];
charc;
inttop=-1;
elemstacktemp, a, b;
intmulcount;
interror;
#ifdef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifscanf("%d", &n);
for (i=0; i<n; i++)
    {
scanf("%s%d%d", str, &(data[i].row), &(data[i].col));
data[i].matrix=str[0];
    }
fgetc(stdin);
mulcount=0;
error=0;
while((c=fgetc(stdin)) !=EOF)
    {
if (c=='/n')
        {
if (error)
printf("error/n");
elseprintf("%d/n", mulcount);
top=-1;
mulcount=0;
error=0;
        }
elseif (c!='('&&c!=')')
        {
for (i=0; i<n; i++)
            {
if (data[i].matrix==c)
                {
temp.row=data[i].row;
temp.col=data[i].col;
break;
                }
            }
stack[++top] =temp;
        } elseif (c==')')
        {
b=stack[top--];
a=stack[top--];
if (a.col==b.row)
            {
temp.row=a.row;
temp.col=b.col;
stack[++top] =temp;
mulcount+=a.row*a.col*b.col;
            } elseerror=1;
        }
        }
return0;
}
目录
相关文章
UVa11565 - Simple Equations
UVa11565 - Simple Equations
56 0
UVA442 矩阵链乘 Matrix Chain Multiplication
UVA442 矩阵链乘 Matrix Chain Multiplication
[LeetCode] Sparse Matrix Multiplication
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is equal to B's row number.
1036 0
Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.
888 0
|
人工智能 Java BI
矩阵乘法 --- hdu 4920 : Matrix multiplication
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 820    Accepted Submi...
955 0

热门文章

最新文章