UVA 442-matrix Chain Multiplication (use of stacks)

Source: Internet
Author: User

The main idea: to give a matrix multiplication formula, wherein the ranks of each matrix is known, to seek the final result.


The operation of the expression, about the use of the stack, the process of encountering two mismatched matrices, the matrix sequence error.


#include <stdio.h> #include <stdlib.h> #include <string.h>int a[60][3];char b[300];int c[300][2];int Top=0;int Main (void) {int i,n,arow,acol,brow,bcol,ok,count,j,le;scanf ("%d", &n); GetChar (); for (i=0;i<n;i++) { A[i][0]=getchar (); scanf ("%d%d", &a[i][1],&a[i][2]); GetChar ();} while (scanf ("%s", b) ==1) {Top=0;count=0;le=strlen (b); if (le==1) {b[0]= ' + ';p rintf ("0\n");} Else{ok=1;for (i=0;i<le;i++) {if (b[i]== ' (') {;} else if (b[i]== ') ') {brow=c[top][0];bcol=c[top][1];top--;arow=c[top][0];acol=c[top][1];top--;if (acol!=brow) {OK=0; break;} else{count=count+ (brow*arow*bcol); top++;c[top][0]=arow;c[top][1]=bcol;}} Else{for (j=0;j<n;j++) {if (b[i]==a[j][0]) {top++;c[top][0]=a[j][1];c[top][1]=a[j][2];break;}}}} for (j=0;j<le;j++) {b[j]=0;} if (ok==0) {printf ("error\n");} else{printf ("%d\n", Count);}}} return 0;}


UVA 442-matrix Chain multiplication (use of stacks)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.