9.3 Exercise 4 analytical solution to a problem statement

Written for: Luo Gu P1597

Title Description

String PASCAL language code length does not exceed 255, only a, b, c 3 variables, and only assignment statements, an assignment can only be a number or a variable, the format of each assignment statement is [变量]:=[变量或一位整数];. Unassigned variable value of zero. An output value of a, b, c of.

Input Format

String (<255) PASCAL language, only a, b, c 3 variables, and only assignment statements, an assignment can only be a number or a variable, the variable is 0 unassigned.

Output Format

It outputs a, b, c of the final value.

Sample input

a:=3;b:=4;c:=5;

Sample Output

3 4 5

problem analysis

Basic simulation questions.
We begin with a string s to hold our input.
Then we can find a sentence comprising five characters, the form "X: = Y;", where Xcertainly the character 'a', 'b', 'c' one, and Ymay be 'a', 'b' , 'c' or a digital '0' to a '9' is.
Then with different situations, it is possible to obtain the value of a, b, c of.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
string s;
int a, b, c;
int main() {
    cin >> s;
    int len = s.length();
    for (int i = 0; i+4 < len; i += 5) {
        char d = s[i];
        char e = s[i+3];
        int f;
        if (isdigit(e)) {
            f = e - '0';
        }
        else {
            if (e == 'a') f = a;
            else if (e == 'b') f = b;
            else f = c;
        }
        if (d == 'a') a = f;
        else if (d == 'b') b = f;
        else c = f;
    }
    printf("%d %d %d\n", a, b, c);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zifeiynoip/p/11570743.html