北邮2015计算机机试

题目链接:https://wenku.baidu.com/view/eb52ca661a37f111f0855b1b.html

注:没有找到完整版的题目,也没有找到oj来评测,我的代码自行写了一些数据经过测试无误,但还是没有oj的ac来的舒服,不确定性很大,大家仅作参考,另外哪里有错误也欢迎大家留言指正。

A.求导数

思路:水题,直接求就好

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,x0;
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&x0);
		int result=3*a*x0*x0+2*b*x0+c;
		printf("%d\n",result);
	}
	return 0;
}

B.LIST

思路:使用一个vector就可以了,水题。。。

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int m;
		scanf("%d",&m);
		vector<int> vt;
		while(m--){
			char buf[100];
			scanf("%s",buf);
			string ord(buf);
			if(ord=="append"){
				int x;
				scanf("%d",&x);
				vt.push_back(x);
			}else if(ord=="pop"){
				vt.erase(vt.end()-1);
			}else if(ord=="find"){
				int index;
				scanf("%d",&index);
				if(index>0){
					printf("%d\n",vt[index-1]);
				}else if(index<0){
					printf("%d\n",vt[vt.size()+index]);
				}
			}
		}
	} 
	return 0;
}

C.图像压缩存储

思路:其实我一开始根本没有思路,前两道题那么水,这道题没有思路还是很烦的,看了大神的博客才明白,首先两个矩阵求异或,然后利用动态规划的思想求最大的方阵,一定要多练动态规划方面的题,不熟悉啊。

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
const int MAXV=1010;
int a[MAXV][MAXV];
int dp1[MAXV][MAXV];	//dp1[i][j]代表a[i][j]向左最大的连续区域数量 
int dp2[MAXV][MAXV];	//向上
int dp3[MAXV][MAXV];	//向左上 
int n;
int main(){
	while(scanf("%d",&n)!=EOF){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%d",&a[i][j]);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				int temp;
				scanf("%d",&temp);
				a[i][j]=!(a[i][j]^temp);
			}
		}
		for(int i=0;i<n;i++){
			dp1[i][0]=a[i][0]; 
			for(int j=1;j<n;j++){
				if(a[i][j]){
					dp1[i][j]=dp1[i][j-1]+1;
				}else{
					dp1[i][j]=0;
				}
			}
		}
		for(int i=0;i<n;i++){
			dp2[0][i]=a[0][i]; 
			for(int j=1;j<n;j++){
				if(a[j][i]){
					dp2[j][i]=dp2[j-1][i]+1;
				}else{
					dp2[j][i]=0;
				}
			}
		}
		memset(dp3,0,sizeof(dp3));
		for(int i=1;i<n;i++){
			for(int j=1;j<n;j++){
				int t[3];
				t[0]=dp3[i-1][j-1];        //判断左上的连续需要判断周围的三个点
				t[1]=dp3[i-1][j];
				t[2]=dp3[i][j-1];
				sort(t,t+3); 
				if(a[i][j]){
					dp3[i][j]=t[0]+1;
				}else{
					dp3[i][j]=0;
				}
			}
		}
		int result=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				int temp[3]={dp1[i][j],dp2[i][j],dp3[i][j]};
				sort(temp,temp+3);
				if(temp[0]>result){
					result=temp[0];
				}
			}
		}
		printf("%d\n",result);
	}
	return 0;
}

D.解析表达式

思路:相信大家学习数据结构时都写过类似的,这里加入了三角函数和对数函数,其实就是加入了一些字符串截取方面的操作,题不难,但感觉很繁琐,如果我是2015机试的同学,感觉这道题会pass掉,不过平常还是多用些时间敲出来了。

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
map<char,int> pro;		//进栈优先级 
double cal(double a,double b,char c){
	if(c=='+'){
		return a+b;
	}else if(c=='-'){
		return a-b;
	}else if(c=='*'){
		return a*b;
	}else if(c=='/'){
		return a/b;
	}
	return 0.0;
}
double calString(string s){
	stack<double> ns;		//数栈 
	stack<char> ss;			//符号栈 
	for(int i=0;i<s.length();i++){
		if(s[i]-'0'>=0 && s[i]-'0'<=9){
			ns.push((double)(s[i]-'0'));
		}else if(s[i]=='s' || s[i]=='c' || s[i]=='t' || s[i]=='l'){
			int index0=s.find(s[i]);
			string str=s.substr(i);
			int index1=str.find('(');
			int index2=index1;
			stack<char> ts;
			ts.push('(');
			while(!ts.empty()){
				index2++;
				if(str[index2]=='('){
					ts.push('(');
				}else if(str[index2]==')'){
					ts.pop();
				}
			}
			string nstr=str.substr(index1+1,index2-index1-1);
			double num=calString(nstr);
			if(s[i]=='s'){
				ns.push(sin(num));
			}else if(s[i]=='c'){
				ns.push(cos(num));
			}else if(s[i]=='t'){
				ns.push(tan(num));
			}else if(s[i+1]=='g'){
				ns.push(log10(num));
			}else if(s[i+1]=='n'){
				ns.push(log(num));
			}
			i=index0+index2;	
			continue;
		}else if(s[i]==')'){
			while(ss.top()!='('){
				double b=ns.top();
				ns.pop();
				double a=ns.top();
				ns.pop();
				char c=ss.top();
				ss.pop();
				ns.push(cal(a,b,c));
			}
			ss.pop();
		}else if(s[i]=='('){
			ss.push(s[i]);
		}else if(!ss.empty() && pro[s[i]]<=pro[ss.top()]){
			while(!ss.empty() && pro[s[i]]<=pro[ss.top()]){
				double b=ns.top();
				ns.pop();
				double a=ns.top();
				ns.pop();
				char c=ss.top();
				ss.pop();
				ns.push(cal(a,b,c));
			}
			ss.push(s[i]);
		}else{
			ss.push(s[i]);
		}
	}
	while(!ss.empty()){
		double b=ns.top();
		ns.pop();
		double a=ns.top();
		ns.pop();
		char c=ss.top();
		ss.pop();
		ns.push(cal(a,b,c));
	}
	return ns.top();
}
int main(){
	pro['(']=0;
	pro['+']=1;pro['-']=1;
	pro['*']=2;pro['/']=2;
	char buf[1000]; 
	while(scanf("%s",buf)!=EOF){
		string s(buf);
		printf("%lf\n",calString(s));
	}
	return 0;
}
/*
输入:7+lg(1+2*4+(3-2))+sin((((2-1)*5-5)*2)/2)
输出:8 
*/

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值