跳到正文

题解

题解:P14305 【MX-J27-T2】转换

MobiusInversion约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年10月25日
收录
2025年12月12日
更新
2026年8月26日
篇幅
约 7 分钟
题目
P14305

版本

  1. v2当前
  2. v1

题解:P14305 【MX-J27-T2】转换

赛时被 T1 硬控 2 小时,迫不得已用 15 分钟爆切 T2

1. 我会骗分

注意到特殊性质 C 不存在运算符 ,,所以我们只需模拟剩下的几个类型即可。

得分:30 分。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
int c,t;
int main(){
	//freopen("conversion5.in","r",stdin);
	//freopen("conversion5.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>c>>t;
	while(t--){
		string str;
		cin>>str;
		if(str.find(",")==string::npos){
			if(str.find("double")!=string::npos){//根据题意,浮点型高于整型
				cout<<"double"<<endl;
			}else if(str.find("float")!=string::npos){
				cout<<"float"<<endl;
			}else if(str.find("longlong")!=string::npos){//longlong高于int
				cout<<"longlong"<<endl;
			}else{//int高于剩下的一切!
				cout<<"int"<<endl;
			}
		}
	}
    return 0;
}

2. 我会正解

这题很容易使我们想起表达式求值,用两个栈来模拟类型与符号,即可完成该题!

先看主函数:

C++
int main(){
	freopen("conversion2.in","r",stdin);
	freopen("conversion2.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	cin>>c>>t;
	while(t--){
		string str;
		cin>>str;qwq;
		for(int i=0;i<str.size();i++){
			if('a'<=str[i]&&str[i]<='z'){//是变量
				int j=i;
				string x="";
				while('a'<=str[j]&&str[j]<='z'&&j<str.size()){
					x+=str[j];
					j++;
				}
				i=j-1;
				num.push(x);//加入变量栈
			}else{                       //p是优先级
				while(op.size()&&op.top()>p[str[i]]){
					eval();//计算
				}
				op.push(str[i]);//入栈
			}
		}
		while(op.size()){
			eval();//剩下的再计算
		}
		cout<<num.top()<<endl;//output
		while(op.size()){
			op.pop();
		}//多测清空栈
		while(num.size()){
			num.pop();
		}
	}
    return 0;
}

再看 eval 函数:

C++
//和骗分的代码及其类似
void eval(){
	string y=num.top();num.pop();
	string x=num.top();num.pop();
	char c=op.top();op.pop();
	string ans="";
	if(c=='+'||c=='*'){//上方的骗分代码思路
		if(x=="double"||y=="double"){
			ans="double";
		}else if(x=="float"||y=="float"){
			ans="float";
		}else if(x=="longlong"||y=="longlong"){
			ans="longlong";
		}else{
			ans="int";
		}
	}else{//运算符为","
		ans=y;//直接等于后者
	}
	num.push(ans);
}

code:

C++
#include<bits/stdc++.h>
#define qwq while(1)
using namespace std;
const int N=1e5+1;
int c,t;
unordered_map<char,int> p{{'+',1},{'*',2},{',',0}};//优先级
stack<char> op;//运算符
stack<string> num;//变量类型
void eval(){
	string y=num.top();num.pop();
	string x=num.top();num.pop();
	char c=op.top();op.pop();
	string ans="";
	if(c=='+'||c=='*'){
		if(x=="double"||y=="double"){
			ans="double";
		}else if(x=="float"||y=="float"){
			ans="float";
		}else if(x=="longlong"||y=="longlong"){
			ans="longlong";
		}else{
			ans="int";
		}
	}else{
		ans=y;
	}
	num.push(ans);
}
int main(){
	freopen("conversion.in","r",stdin);
	freopen("conversion.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>c>>t;
	while(t--){
		string str;
		cin>>str;qwq;
		for(int i=0;i<str.size();i++){
			if('a'<=str[i]&&str[i]<='z'){
				int j=i;
				string x="";
				while('a'<=str[j]&&str[j]<='z'&&j<str.size()){
					x+=str[j];
					j++;
				}
				i=j-1;
				num.push(x);
			}else{
				while(op.size()&&op.top()!='('&&op.top()>p[str[i]]){
					eval();
				}
				op.push(str[i]);
			}
		}
		while(op.size()){
			eval();
		}
		cout<<num.top()<<endl;
		while(op.size()){
			op.pop();
		}
		while(num.size()){
			num.pop();
		}
	}
    return 0;
}
原文
存档评论4 条抓取于