#include<bits/stdc++.h>

using namespace std;
int a,b,c;
int main(){
	string s;
	getline(cin,s);
	int n=s.size();
	string w;
	cout << s << endl;
	for(int i=0;i<n;i++){
		if(s[i]==';'){
			w+=s[i-4]+s[i-2]+s[i-1];
			cout << w<<endl;
			if(w[0]=='a'){
				a=w[2]-'0';
			}
			if(w[0]=='b'){
				if(w[2]=='a'){
					b=a;
				}
				else{
					b=w[2]-'0';//a:=1;b:=a;c:=3;
				}
			}
			if(w[0]=='c'){
				if(w[2]=='a'){
					c=a;
				}
				if(w[2]=='b'){
					c=b;
				}
				else{
					c=w[2]-'0';
				}
			}
		}
	}
	cout << a<<' '<<b<<' '<<c;
}

题目

1 comments

  • @ 2025-5-27 19:11:20

    明显错误:

    string w;
    w+=s[i-4]+s[i-2]+s[i-1];
    

    string 作为字符串不能用 + 连接单个字符,这道题也没必要存储每一个等式,直接分情况判断赋值即可,参考代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    
    int x, y, z;
    
    int main()
    {
    	string a;
    	getline(cin, a);
    	
    	int len = a.size();
    	for (int i = 0; i < len; i ++ )
    	{
    		if (a[i] == 'a')
    		{
    			if (a[i + 3] >= '0' && a[i + 3] <= '9') x = a[i + 3] - '0';
    			if (a[i + 3] == 'b') x = y;
    			if (a[i + 3] == 'c') x = z;
    		}
    		if (a[i] == 'b')
    		{
    			if (a[i + 3] >= '0' && a[i + 3] <= '9') y = a[i + 3] - '0';
    			if (a[i + 3] == 'a') y = x;
    			if (a[i + 3] == 'c') y = z;
    		}
    		if (a[i] == 'c')
    		{
    			if (a[i + 3] >= '0' && a[i + 3] <= '9') z = a[i + 3] - '0';
    			if (a[i + 3] == 'a') z = x;
    			if (a[i + 3] == 'b') z = y;
    		}
    //		cout << x << ' ' << y << ' ' << z << '\n';
    	}
    	
    	cout << x << ' ' << y << ' ' << z << '\n';
    	return 0;
    }
    
    • 1