zoukankan      html  css  js  c++  java
  • 计算矩阵运算的乘法次数

    //描述:
    //矩阵乘法的运算量与矩阵乘法的顺序强相关。
    //
    //例如:
    //A是一个50×10的矩阵,B是10×20的矩阵,C是20×5的矩阵
    // 
    //计算A*B*C有两种顺序:((AB)C)或者(A(BC)),前者需要计算15000次乘法,后者只需要3500次。
    // 
    //编写程序计算不同的计算顺序需要进行的乘法次数
      
    //知识点: 字符串 
    //题目来源: 内部整理 
    //练习阶段: 中级 
    //运行时间限制: 10Sec
    //内存限制: 128MByte
    //输入:  
    //输入多行,先输入要计算乘法的矩阵个数n,每个矩阵的行数,列数,总共2n的数,最后输入要计算的法则
    //3       //矩阵个数n 
    //50 10   //矩阵A的行数50,列数10
    //10 20   //矩阵B的行数10,列数20
    //20 5    //矩阵C的行数20,列数5
    //(A(BC)) //矩阵从A开始命名,A、B、C、D...以此类推,通过括号表示运算顺序
    // 
    //输出:  
    //输出需要进行的乘法次数
    // 
    //样例输入:
    //3
    //50 10
    //10 20
    //20 5

    //(A(BC))

    //样例输出:

    //3500

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int cishu=0;
    
    struct juzhen_info{
    	int row;
    	int column;
    };
    static int index=0;
    struct juzhen_info chengfa_cishu(int n, struct juzhen_info *juzhen, char* exep){
    	int flag=0;
    	struct juzhen_info cur_juzhen;
    	struct juzhen_info temp;
    	while(*(exep+index)!=''){
    		if(*(exep+index)=='('){
    			index++;
    			if(flag==0){
    				//保存cur_juzhen
    				cur_juzhen = chengfa_cishu(n, juzhen, exep);
    				flag = 1;
    			}
    			else{
    				//计算次数
    				temp = chengfa_cishu(n, juzhen, exep);
    				cishu += cur_juzhen.row * cur_juzhen.column * temp.column;
    				cur_juzhen.column = temp.column;
    			}
    			//chengfa_cishu(n, juzhen, exep);
    		}
    		else if(*(exep+index)==')'){
    			index++;
    			return cur_juzhen;
    		}
    		else if(*(exep+index)>64&&*(exep+index)<65+n){
    			if(flag==0){
    				cur_juzhen.row = juzhen[*(exep+index)-65].row;
    				cur_juzhen.column = juzhen[*(exep+index)-65].column;
    				flag = 1;
    				index++;
    			}
    			else{
    				cishu += cur_juzhen.row * cur_juzhen.column * juzhen[*(exep+index)-65].column;
    				cur_juzhen.column = juzhen[*(exep+index)-65].column;
    				index++;
    			}
    		}
    	}
    }
    
    int main(void){
    	int n;
    	int k;
    	char exep[100]; char *ptr;
    
    	struct juzhen_info *juzhen, *juzhen_temp;
    
    	scanf("%d", &n);
    	juzhen_temp = juzhen = (struct juzhen_info*)malloc(n*sizeof(struct juzhen_info));
    	
    	for(k=0; k<n; k++){
    		scanf("%d %d", &juzhen->row, &juzhen->column);
    		juzhen++;
    	}
    
    	scanf("%s", exep);
    	ptr = strupr(exep);
    
    	chengfa_cishu(n, juzhen_temp, ptr);
    	printf("%d",cishu);
    
    	system("pause");
    	return 0;
    }

  • 相关阅读:
    修改msn密碼的地址
    Global.asax.cs中的方法的含义 Application_AcquireRequestState验证Session[轉]
    工作筆記DMIS項目
    给创业者的忠告
    Windows 2008 / Windows 7 x64: The ‘Microsoft.Jet.OLEDB.4.0′ provider is not registered on the local machine.
    Case Study: Nick Leeson and The Barings Debacle
    如何减小MS SQL Server的Log文件尺寸
    如何使windows7的默认共享可以被访问
    Android 开发人员必须掌握的 10 个开发工具
    在Windows Server 2008 R2上设置FTP 服务
  • 原文地址:https://www.cnblogs.com/xhyzjiji/p/6159396.html
Copyright © 2011-2022 走看看