洛谷的--运算似乎有问题
  • 板块工单反馈版
  • 楼主zjyqwq
  • 当前回复4
  • 已保存回复4
  • 发布时间2021/11/5 23:02
  • 上次更新2023/11/4 01:20:16
查看原帖
洛谷的--运算似乎有问题
303025
zjyqwq楼主2021/11/5 23:02

RT。 在https://www.luogu.com.cn/record/61824603https://www.luogu.com.cn/record/61824725的对比中,我们可以发现,两者本意相同的代码却一个10分,一个100分。

十分代码:

#include<bits/stdc++.h>
using namespace std;
string s;
char ch[100005];
int sh[100005],t,t1;
int main(){
//	freopen("cpp.in","r",stdin);
//	freopen("cpp.out","w",stdout);
	cin>>s;
	for(int i=0;i<s.size();i++){
		switch(s[i]){
			case '+':{
				while(ch[t1]=='*'){
//                    t--;这里
					sh[--t]=sh[t]*sh[t+1];
					sh[t]%=10000;
//					cout<<'*'<<'2';
					t1--;
				}
				ch[++t1]='+';
//                cout << t << " " << sh[t] << endl;
				break;
			}
			case '*':{
				ch[++t1]='*';
				break;
			}
			default :{
				sh[++t]=0;
				while(s[i]>='0'&&s[i]<='9'){
					sh[t]=sh[t]*10+s[i]-'0';
					sh[t]%=10000;
					i++;
				}
//                cout << t << " " << sh[t] << endl;
				i--;
				break;
			}
		}
	}
	while(t1){
		if(ch[t1]=='*'){
//            cout << 1 << " " << sh[t - 1] << " " << sh[t] << endl;
//            t--;这里
			sh[--t]=sh[t]*sh[t+1];
			sh[t]=sh[t] % 10000;
//			cout<<'*'<<'2';
		}
		else{
//            cout << 2 << " " << sh[t - 1] << " " << sh[t] << endl;
//            t--;这里
			sh[--t]=sh[t]+sh[t+1];
			sh[t]=sh[t] % 10000;
//			cout<<'+'<<'2';
		}
		t1--;
	}
//	cout<<endl;
	cout<<sh[1]%10000;
}

100分:

#include<bits/stdc++.h>
using namespace std;
string s;
char ch[100005];
int sh[100005],t,t1;
int main(){
//	freopen("cpp.in","r",stdin);
//	freopen("cpp.out","w",stdout);
	cin>>s;
	for(int i=0;i<s.size();i++){
		switch(s[i]){
			case '+':{
				while(ch[t1]=='*'){
                    t--;
					sh[t]=sh[t]*sh[t+1];
					sh[t]%=10000;
//					cout<<'*'<<'2';
					t1--;
				}
				ch[++t1]='+';
//                cout << t << " " << sh[t] << endl;
				break;
			}
			case '*':{
				ch[++t1]='*';
				break;
			}
			default :{
				sh[++t]=0;
				while(s[i]>='0'&&s[i]<='9'){
					sh[t]=sh[t]*10+s[i]-'0';
					sh[t]%=10000;
					i++;
				}
//                cout << t << " " << sh[t] << endl;
				i--;
				break;
			}
		}
	}
	while(t1){
		if(ch[t1]=='*'){
//            cout << 1 << " " << sh[t - 1] << " " << sh[t] << endl;
            t--;
			sh[t]=sh[t]*sh[t+1];
			sh[t]=sh[t] % 10000;
//			cout<<'*'<<'2';
		}
		else{
//            cout << 2 << " " << sh[t - 1] << " " << sh[t] << endl;
            t--;
			sh[t]=sh[t]+sh[t+1];
			sh[t]=sh[t] % 10000;
//			cout<<'+'<<'2';
		}
		t1--;
	}
//	cout<<endl;
	cout<<sh[1]%10000;
}
2021/11/5 23:02
加载中...