课堂总结

· · 个人记录

课堂总结

1.T703669 二阶矩阵4

题意: 给定一个 n×m 的矩阵,问所有 2×2 的子矩阵中,输出元素总和最大值,并输出该矩阵左上角的行列。

如果有多个最大值相同的,输出最靠近左上角的那个。

思路: 1.枚举2×2子矩阵的左上角的坐标 2.将2×2矩阵四个点值的和比擂主

下面是我的代码:

#include<bits/stdc++.h>
using namespace std;
int a[1005][1005];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    int maxx=INT_MIN,h,l;
    for(int i=1;i<n;i++){
        for(int j=1;j<m;j++){
            if(a[i][j]+a[i][j+1]+a[i+1][j]+a[i+1][j+1]>maxx){
                maxx=a[i][j]+a[i][j+1]+a[i+1][j]+a[i+1][j+1];
                h=i;
                l=j;
            }
        }
    }
    cout<<maxx<<endl<<h<<" "<<l;
    return 0;
}

(有些地方会有点长,所以换到下一行了)

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int a[1005][1005];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    int maxx=0;//最大值擂主 
    int x,y;//最大2*2矩阵左上角的行、列坐标 
    //枚举2*2子矩阵的左上角的坐标
    for(int i=1;i<=n-1;i++)//该点的行坐标从1到n-1
    {
        for(int j=1;j<=m-1;j++)//该点的列坐标从1到m-1 
        {
            //当前i j
            //右边i j+1
            //下边i+1 j
            //右下i+i j+1 
            if(a[i][j]+ a[i][j+1]+ a[i+1][j]+ a[i+1][j+1]>maxx)//2*2矩阵四个点值的
和比擂主
            {
                maxx=a[i][j]+ a[i][j+1]+ a[i+1][j]+ a[i+1][j+1];
                x=i;
                y=j;
            }
        }
    }
    cout<<maxx<<endl<<x<<" "<<y;

    return 0;
}

2.T692546 后缀表达式求值1

题意: 从键盘读入一个后缀表达式(字符串),只含有 0-9 组成的运算数及加(+)、减(—)、乘(*)、除(/)四种运算符。每个运算数和运算符之间用一个空格隔开,不需要判断给你的表达式是否合法。

提示:输入字符串长度小于 250,保证参与运算的整数只会是一位正整数,除法计算规则按照 C++ 中的 / 计算,计算结果不超过 int 的范围。

思路: 1.拿到字符串,然后如果发现是数字就将对应字符转为数字。 2.遇到符号,栈顶为表达式右边,第二个栈顶为表达式左边。 3.运算结果入栈 。

下面是我的代码:

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
stack<int>s;
int main()
{

    //拿到字符串,然后如果发现是数字就将对应字符转为数字
    //一位数,直接-'0' 
    string a;
    getline(cin,a);//有空格 
    for(int i=0;i<a.size();i++)
    {
        //遇到数字直接入栈 
        if(a[i]>='0'&&a[i]<='9')//发现是数字字符 
        {
            //将对应的数字字符变成整数存入栈中
            s.push(a[i]-'0'); 
        }
        //遇到符号,栈顶为表达式右边,第二个栈顶为表达式左边
        //运算结果入栈 
        else if(a[i]=='+')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1+num2);
        }
        else if(a[i]=='-')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1-num2);
        }
        else if(a[i]=='*')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1*num2);
        }
        else if(a[i]=='/')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1/num2);
        }
    }
    cout<<s.top();
    return 0;
}

下面是AC代码:

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
stack<int>s;
int main()
{

T3
    //拿到字符串,然后如果发现是数字就将对应字符转为数字
    //一位数,直接-'0' 
    string a;
    getline(cin,a);//有空格 
    for(int i=0;i<a.size();i++)
    {
        //遇到数字直接入栈 
        if(a[i]>='0'&&a[i]<='9')//发现是数字字符 
        {
            //将对应的数字字符变成整数存入栈中
            s.push(a[i]-'0'); 
        }
        //遇到符号,栈顶为表达式右边,第二个栈顶为表达式左边
        //运算结果入栈 
        else if(a[i]=='+')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1+num2);
        }
        else if(a[i]=='-')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1-num2);
        }
        else if(a[i]=='*')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1*num2);
        }
        else if(a[i]=='/')
        {
            int num2=s.top();
            s.pop();
            int num1=s.top();
            s.pop();
            s.push(num1/num2);
        }
    }
    cout<<s.top();
    return 0;
}

3.T692232 后缀表达式求值2

题意: 与上题相差不多,只是可能是多位数。 思路: 是多位数时,用累计数字×10+当前数字,算出来这个多位数入栈后即可,其他与上一题一样。

下面是我的代码:

#include<bits/stdc++.h>
using namespace std;
stack<int>q;
int main(){
    string s;
    getline(cin,s);
    cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]>='0'&&s[i]<='9'){
            int num=0;
            while(s[i]!=' '){
                num=num*10+(s[i]-'0');
                i++;    
            } 
            q.push(num);
        }else if(s[i]=='+'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s1+s2);
        }
        else if(s[i]=='-'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s2-s1);
        }
        else if(s[i]=='*'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s2*s1);
        }
        else if(s[i]=='/'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s2/s1);
        }

    }
    cout<<q.top();
    return 0;
}

下面是AC代码:

#include<bits/stdc++.h>
using namespace std;
stack<int>q;
int main(){
    string s;
    getline(cin,s);
    cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]>='0'&&s[i]<='9'){
            int num=0;
            while(s[i]!=' '){
                num=num*10+(s[i]-'0');
                i++;    
            } 
            q.push(num);
        }else if(s[i]=='+'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s1+s2);
        }
        else if(s[i]=='-'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s2-s1);
        }
        else if(s[i]=='*'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s2*s1);
        }
        else if(s[i]=='/'){
            int s1,s2;
            s1=q.top();
            q.pop();
            s2=q.top();
            q.pop();
            q.push(s2/s1);
        }

    }
    cout<<q.top();
    return 0;
}

课堂总结:

比上次有进步,继续加油!