字节跳动C++二面

1、设计模式以及适用的场景:工厂模式和单件模式

http://blog.csdn.net/liang19890820/article/details/70652858

https://blog.csdn.net/lbqbraveheart/article/details/7084509

2、虚函数:看代码输出应该是什么 ,析构适用虚函数和不适用虚函数分别为什么。

3、小模拟:带有括号的四则运算:例:1+2*(3+4*(5+6))-7。

#include <iostream>
#include <stack>
using namespace std;
int firstL(char c){
	if(c=='-'||c=='+')return 1;
	else if(c=='*'||c=='/')return 2;
	else return 0;
}
int c(int x,int y,char c){
	if(c=='+')return x+y;
	if(c=='-')return x-y;
	if(c=='*')return x*y;
	if(c=='/')return x/y;
	return -1;
}
int calculate(string s) {
	stack<char>op;
	stack<int>num;
	s+='#';
	int len=s.length();
	int sum=0;
	int re=0;
	int f=0;
	for(int i=0;i<len;i++){
		if(s[i]>='0'&&s[i]<='9'){
			sum=sum*10+(s[i]-'0');
			f=1;
		}
		else{
			if(f){
				num.push(sum);
				//cout<<"num: "<<sum<<endl;
				sum=0;
				f=0;
			}
			if(op.size()==0||s[i]=='('){
				op.push(s[i]);
				continue;
			}
			char tmpOp=op.top();
			while(firstL(s[i])<=firstL(tmpOp)){
				//cout<<"nowop: "<<s[i]<<endl;
				//cout<<"lastop: "<<op.top()<<endl;
				if(tmpOp=='('){
					op.pop();
					break;
				}
				op.pop();
				int x=num.top();
				num.pop();
				int y=num.top();
				num.pop();
				re=c(y,x,tmpOp);
				//cout<<re<<endl;
				num.push(re);
				if(op.size()==0)break;
				tmpOp=op.top();
			}
			if(s[i]!=')')
				op.push(s[i]);
		}
	}
	op.pop();
	int result=num.top();
	num.pop();
	return result;
}
string s;
int main(){
	cin>>s;
	cout<<calculate(s)<<endl;
}

 

4、一个圆上三个点处于同一个半圆的概率是多少?

求锐角三角形的概率,3/4。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值