200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 斗鱼校园招聘提前批笔试题之统计乘法次数

斗鱼校园招聘提前批笔试题之统计乘法次数

时间:2023-12-25 01:57:32

相关推荐

斗鱼校园招聘提前批笔试题之统计乘法次数

#include<iostream>

#include<string>

#include<stack>

#define MAXN 1024

struct MAP {

int col; //行

int row; //列

int count;

char flag;

}MAP1[MAXN],map;

void compute_mutiplx(std::string &str1,MAP MAP1[MAXN],MAP &map,int len){

int current = str1.size();

std::cout << current << std::endl;

std::stack<MAP> stack2;

//将所有待计算的元素入栈

for (int i=0;i<current;i++){

for (int j = 0;j<len;j++) {

if (str1[i]== MAP1[j].flag){

stack2.push(MAP1[j]);

}

if (str1[i] == '#'){

stack2.push(map);

break;

}

}

}

MAP map1=stack2.top();//保存若干次相乘后的矩阵结果

stack2.pop();

while (!stack2.empty()){

MAP temp1= stack2.top();

stack2.pop();

map.count += map1.col*map1.row*temp1.row;//计算相乘次数

map1.row = temp1.row;

}

map.col = map1.col;

map.row = map1.row;

std::cout << map.col << map.row << std::endl;

str1.clear();

}

int main() {

int n;//表示矩阵的个数

std::cin >> n;

for (int i = 0; i < n; i++) {

std::cin >> MAP1[i].col;

std::cin >> MAP1[i].row;

}

char matrix[1024];

map.count = 0;//初始化

std::string str1;

std::cin >> matrix;//保存输入的矩阵运算

int len = strlen(matrix);

std::stack<char>stack1;

int index = 0;

for (int i=0;i<len;i++){

std::cout << "--------" << std::endl;

//如果是'('或者是字母的话,将该字符串入栈,当是字母的时候要将其MAP中存储的值与其对应

if(matrix[i]=='('||(matrix[i]<='Z'&&matrix[i]>='A')){

if (matrix[i]<= 'Z'&&matrix[i]>= 'A') {

MAP1[index].flag =matrix[i];

index++;

}

stack1.push(matrix[i]);

}

if ((matrix[i] ==')')){

while(stack1.top()!='('){

str1+=stack1.top();

stack1.pop();

}

stack1.pop();//将左括号弹出栈

std::cout << str1 << std::endl;

compute_mutiplx(str1,MAP1,map,n);//计算弹出栈的元素的乘法计算次数和计算的结果,保存在map里,用#来标记。

if (stack1.empty()){

std::cout <<map.count << std::endl;

system("pause");

return 1;

}

stack1.push('#');

}

}

system("pause");

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。