bupt复试机试---2015

求导数

题目描述

描述:求函数f(x) = ax^3 + bx^2 + c*x + d在x = x0处的一阶导数。

输入
数据第一行是数据的组数m
接下来m行的每一行分别是 a b c d x0

输出
输出x0处的导数
样例输入
1
1 1 1 1 1
样例输出
6
来源

2015机考题A题
解析
很简单求导,直接利用公式求导

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int m;
    cin>>m;
    while(m--){
        int a,b,c,d,x;
        cin>>a>>b>>c>>d>>x;
        long long sum=0;
        sum=3*a*pow(x,3)+2*b*pow(x,2)+c*x;
        cout<<sum<<endl;
    }
    return 0;
}

List

题目描述
在该LIST上实现3种操作
1、append x在该LIST末尾添加x,x是32位整数
2、pop删除该LIST末尾的数
3、find i寻找第i个数,若i为负数表示寻找倒数第i个数,例如i = -1表示寻找倒数第一个

输入
首先一个数t表示以下有t个m
第一行输入一个m,表示有m条操作,接下来每行输入一条操作

输出
当输入find i时输出找到的数
样例输入
2
5
append 1
append 2
find 1
find -1
pop
6
append 1
append 2
append 3
append 4
find -2
find 2

样例输出
1
2
3
2
来源
2015机考B题
解析
利用vector实现

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
        int m;
        cin>>m;
        vector<int>a;
        while(m--){
            string op;
            cin>>op;
            if(op=="append"){
                int x;
                cin>>x;
                a.push_back(x);
            }else if(op=="pop"){
                int len =a.size();
                a.pop_back();
            }else{
                int i;
                cin>>i;
                if(i>0)cout<<a[i-1]<<endl;
                else if(i<0)cout<<a[a.size()+i]<<endl;
            }
        }
    }
    return 0;
}

图像压缩存储

题目描述

以二维数组表示图像,其值只有0、1两种,寻找两幅图像中最大的相同方阵

输入
第一行输入一个n,接下来的2n行输入两个nn数组,寻找一个最大的mm子区域,使得两个数组在该子区域完全相同
输出
输出m
样例输入
4
1 1 1 1
1 1 1 0
1 1 1 0
1 1 1 1
0 1 1 1
0 1 1 1
0 1 1 1
0 1 1 0
样例输出
2
来源
2015机考C题
解析
解题思路:
可以将两个矩阵直接异或(^)出来。
0异或0等于0
1异或1等于0
0异或1等于1
(相同异或为0,不同异或为1)
两个矩阵异或之后取非。
然后题目就变成了求第三个矩阵里的最大的为1的方阵。
我们可以采用动态规划的思想。
首先从左到右遍历一次寻找每个点可以向左延伸到最远的距离。
然后从上到下遍历一次寻找每个点可以向上延伸到最远的距离。
最后直接从左上角到右下角。看直接能到最左上角的距离。
就是每个点可以延伸最大的方阵的边长。

#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;
}

解析表达式

题目描述
输入一个字符串形式的表达式,该表达式中包括整数,四则运算符(+、-、*、/),括号,三角函数(sin(x)、cos(x)、tan(x)),底数函数(lg(x)、ln(x)),计算该表达式的值
输入
输入一个字符串形式的表达式,保证中间及最终结果不超出double的范围

输出
表达式的值,保留6位小数
样例输入
3
3+5
((2-1)*5-1)*6
1+cos(0)
样例输出
3.000000
8.000000
24.000000
2.000000
来源
2015机考D题

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<stack>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        stack<double>num;
        stack<char>chr;
        for(int i=0;i<str.size();i++)
        {
            //cout<<str[i]<<" ";
            double number=0;
            if(str[i]>=48&&str[i]<=57)
            {
                number=str[i]-48;
                while(str[i+1]>=48&&str[i+1]<=57)
                {
                    number=number*10+(str[i+1]-48);
                    i++;
                }
                num.push(number);
            }
            else if(str[i]=='(')
                chr.push(str[i]);
            else if(str[i]=='+'||str[i]=='-')
                chr.push(str[i]);
            else if(str[i]=='*'||str[i]=='/')
            {
                char t=str[i];
                i++;
                number=str[i]-48;
                while(str[i+1]>=48&&str[i+1]<=57)
                {
                    number=number*10+(str[i+1]-48);
                    i++;
                }
                num.push(number);
                double x=num.top();num.pop();
                double y=num.top();num.pop();
                double n=0;
                if(t=='*')
                    n=x*y;
                else n=x/y;
                num.push(n);
            }
            else if(str[i]==')')
            {
                char temp=chr.top();chr.pop();
                double x=num.top();num.pop();
                double y=num.top();num.pop();
                double n=0;
                if(temp=='+')
                    n=x+y;
                else if(temp=='-')
                    n=y-x;
                num.push(n);
                chr.pop();
                chr.pop();
            }
            else if(str[i]=='s'||str[i]=='c'||str[i]=='t')
            {
                char t=str[i];
                i=i+4;
                number=str[i]-48;
                while(str[i+1]>=48&&str[i+1]<=57)
                {
                    number=number*10+(str[i+1]-48);
                    i++;
                }
                if(t=='s')
                {
                    number=sin(number);
                    num.push(number);
                }
                else if(t=='t')
                {
                    number=tan(number);
                    num.push(number);
                }
                else if(t=='c')
                {
                    number=cos(number);
                    num.push(number);
                }
                i++;
            }
            else if(str[i]=='l')
            {
                char t=str[i+1];
                i=i+3;
                number=str[i]-48;
                while(str[i+1]>=48&&str[i+1]<=57)
                {
                    number=number*10+(str[i+1]-48);
                    i++;
                }
                if(t=='g')
                {
                    number=log10(number);
                    num.push(number);
                }
                else if(t=='n')
                {
                    number=log(number);
                    num.push(number);
                }
                i++;
            }
        }
        while(chr.size()>0&&chr.size()<100&&num.size()!=1)
        {
            char temp=chr.top();chr.pop();
            double x=num.top();num.pop();
            double y=num.top();num.pop();
            double n=0;
            if(temp=='+')
                n=x+y;
            else if(temp=='-')
                n=y-x;
            else if(temp=='(')
                continue;
            num.push(n);
        }
        printf("%lf",num.top());cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值