[360 Autumn Recruitment Written Exam] Programming Question 2: Modifying the Web (AC Solution of C++)

topic

Let’s look at the example first:

6
16=1+2*3
7*8*9=54
1+1=1+22
4*6=22+2
15+7=1+2
11+1=1+5

n means inputting n rows of data, and each row of data below represents an equation. If it is possible to add any number to the equation to make both sides of the equation true, then output Yes, otherwise output No. If the equations are inherently equal, Yes is also output.

The only symbols are +, * and =. All are positive integers.

If you want to test whether your splitting or calculation is correct, you can just use an example:

1
16=1+2*3

Ideas

A slightly more complicated simulation.

To perform calculations, the equation (string) needs to be split into numbers and characters. The calculation process is similar to the reverse Polish expression , implemented using a stack. As for whether adding a number makes the equation true, we can enumerate .

This question is solved in C++. ( This kind of data input feels more convenient with C++. If you need to use JS asynchronously?? There is no template for input in 360’s programming questions, just ACM mode )

Input a string and get the function of numbers and characters: the middle cntNum、cntChis a pointer, used to record the total number of splits.

void stringTo(string a){
    
    
    int temp=0;
    for(int i=0;a[i];i++){
    
    
        if(a[i]>='0'&&a[i]<='9'){
    
    
            temp*=10;
            temp+=a[i]-'0';
        }else{
    
    
            ch[cntCh]=a[i];
            cntCh++;
            num[cntNum]=temp;
            cntNum++;temp=0;
        }
    }
    num[cntNum]=temp;
    cntNum++;temp=0;
}

To calculate the numbers on the left and right sides of the equal sign =, the idea is the reverse Polish expression .

The parameters left and right here represent the calculation [left,right]of the answer in the array. In this way, you only need to change the parameters to calculate the left side of the equal sign and the right side of the equal sign respectively.

Note that the number of numbers will be 1 more than the number of symbols, so the first number can be pushed onto the stack first.

//计算左右两边的大小 
//左 1,indexx
//右 indexx+1,end 
int cal(int left,int right){
    
    
	if(left==right) return num[left];
	
	stack<int>numm;stack<char>chh;
	numm.push(num[left]);
//	数字 
	for(int i=left+1;i<=right;i++){
    
    
		//对应的操作符是i-1的下标 
		char op=ch[i-1];
		//优先级高 
		if(op=='*'){
    
    
			int qian=numm.top();numm.pop();
			int hou=num[i];
			int temp=qian*hou;
			numm.push(temp);
		}
		else{
    
    
			numm.push(num[i]);
			chh.push(ch[i-1]);
		}
	}
	
//	计算加减
	
	while(chh.size()){
    
    
		char op=chh.top();chh.pop();
		int hou=numm.top();numm.pop();
		int qian=numm.top();numm.pop();
		int temp=qian+hou;
		numm.push(temp);
	} 
	return numm.top();
}

The enumeration inserts a number at each position in the string and calculates:

for(int i=0;a[i];i++){
    
    
	c.clear();
	b+=a[i];
	c+=b;
	for(int j=0;j<=9;j++){
    
    
		c+=char('0'+j);
		c+=a.substr(i+1);
		if(solve(c)) {
    
    
			flag=1;break;
		}
		c.clear();c+=b;
	}
	if(flag) break;
}

The overall process: converting strings into numbers and symbols, calculations. This process can be encapsulated:

bool solve(string a){
    
    
	clearr();
	stringTo(a);
	findEqual();
	l=cal(0,indexx);
	r=cal(indexx+1,cntNum-1);
	if(l==r) return true;
	else return false;
}

Overall Code (AC)

#include<bits/stdc++.h>
using namespace std;
int t;string a;
int num[5000];
char ch[5000];
int cntNum=0,cntCh=0;
int indexx;//=的下标 
int l,r;
void clearr(){
    
    
	cntNum=0,cntCh=0;
}
void stringTo(string a){
    
    
    int temp=0;
    for(int i=0;a[i];i++){
    
    
        if(a[i]>='0'&&a[i]<='9'){
    
    
            temp*=10;
            temp+=a[i]-'0';
        }else{
    
    
            ch[cntCh]=a[i];
            cntCh++;
            num[cntNum]=temp;
            cntNum++;temp=0;
        }
    }
    num[cntNum]=temp;
    cntNum++;temp=0;
}
//找到=的下标index 对应num的下标,index及其之前的都是=号左边的 
void findEqual(){
    
    
	for(int i=0;i<cntCh;i++){
    
    
		if(ch[i]=='='){
    
    
			indexx=i;break;
		}
	} 
}
//计算左右两边的大小 
//左 1,indexx
//右 indexx+1,end 
int cal(int left,int right){
    
    
	if(left==right) return num[left];
	
	stack<int>numm;stack<char>chh;
	numm.push(num[left]);
//	数字 
	for(int i=left+1;i<=right;i++){
    
    
		//对应的操作符是i-1的下标 
		char op=ch[i-1];
		//优先级高 
		if(op=='*'){
    
    
			int qian=numm.top();numm.pop();
			int hou=num[i];
			int temp=qian*hou;
			numm.push(temp);
		}
		else{
    
    
			numm.push(num[i]);
			chh.push(ch[i-1]);
		}
	}
	
//	计算加减
	
	while(chh.size()){
    
    
		char op=chh.top();chh.pop();
		int hou=numm.top();numm.pop();
		int qian=numm.top();numm.pop();
		int temp=qian+hou;
		numm.push(temp);
	} 
	return numm.top();
}

bool solve(string a){
    
    
	clearr();
	stringTo(a);
	findEqual();
	l=cal(0,indexx);
	r=cal(indexx+1,cntNum-1);
	if(l==r) return true;
	else return false;
}

int main(){
    
    
    cin>>t;
    while(t--){
    
    
    	clearr();l=0,r=0;
        cin>>a;
        stringTo(a);
        
//        找= index
		findEqual();
		l=cal(0,indexx);
		r=cal(indexx+1,cntNum-1);
		
		if(l==r) {
    
    
			cout<<"Yes"<<endl;continue;
		}
		
		string b,c;int flag=0;
		//在最前面加
		for(int i=0;i<=9;i++){
    
    
			c.clear();
			c+=char('0'+i);
			c+=a;
			if(solve(c)) {
    
    
				flag=1;break;
			}
		}
		if(flag) {
    
    
			cout<<"Yes"<<endl;continue;
		}
		
		b.clear();c.clear();
		 
		for(int i=0;a[i];i++){
    
    
			c.clear();
			b+=a[i];
			c+=b;
			for(int j=0;j<=9;j++){
    
    
				c+=char('0'+j);
				c+=a.substr(i+1);
				if(solve(c)) {
    
    
					flag=1;break;
				}
				c.clear();c+=b;
			}
			if(flag) break;
		}
		if(flag) {
    
    
			cout<<"Yes"<<endl;continue;
		}
		else cout<<"No"<<endl;    
    }
    return 0;
}

Not important experience

A very typical simulation question with many elements, string calculation + reverse Polish expression + enumeration. It took more than 70 minutes to write!
The input data range is not large, so enumeration can be implemented. I don't know if there is a better way to write it.
The code is written in a messy manner and is not streamlined. I will rewrite it when I have time.

In addition: I am a front-end! But I was dumbfounded when I saw that the programming question did not provide a template for asynchronous data input. . How to do this. . I was forced to pick up the memory of writing questions in C++, otherwise I would have sent it.

Guess you like

Origin blog.csdn.net/karshey/article/details/133216144