2025.12.3

· · 个人记录

T703669 二阶矩阵4

题意

给定一个 n \cdot m 的矩阵,问所有 2\ \cdot \ 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[j][i];
        }
    }
    int maxx = 0, maxi = n, maxj = m;
    for(int i = 1; i < n; i++)
        for(int j = 1; j < m; j++)
        {
            int t = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
            if(t >= maxx)
            {
                maxx = t;
                maxi = i;
                maxj = j;
            }
        }
    cout << maxx << endl << maxj << ' ' << maxi;
    return 0;
}

AC 代码

#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, maxi, maxj;
    for(int i = 1; i < n; i++)
        for(int j = 1; j < m; j++)
        {
            int t = a[i][j] + a[i + 1][j] + a[i][j + 1] + a[i + 1][j + 1];
            if(t >= maxx)
            {
                maxx = t;
                maxi = i;
                maxj = j;
            }
        }
    cout << maxx << endl << maxi << ' ' << maxj;
    return 0;
}

错因

# T692546 后缀表达式求值1 ## 题意 从键盘读入一个后缀表达式(字符串),只含有 0-9 组成的一位正整数作为运算数及加(+)、减(—)、乘(*)、除(/)(向下取整)四种运算符。每个运算数和运算符之间用一个空格隔开,保证给你的表达式合法,并保证原始后缀表达式中不存在形如 -1 这样的数字。 ## 目的 计算该表达式的结果。 ## 思路 照题意模拟即可。 ## 代码 ```cpp #include <bits/stdc++.h> using namespace std; stack <int> st; int main() { string s; cin >> s; int num = 0; bool flag = 1; for(int i = 0; i < s.size(); i++) { char c = s[i]; if(c >= '0' && c <= '9') { flag = 1; num = num * 10 + (c - '0'); } else if(c == ' ' && flag) { st.push(num); num = 0; } else if(!flag) { break; } else { if(c == '+') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b + a); flag = 0; } if(c == '-') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b - a); flag = 0; } if(c == '*') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b * a); flag = 0; } if(c == '/') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b / a); flag = 0; } } } cout << st.top(); return 0; } ``` ## $AC$ 代码 ```cpp #include <bits/stdc++.h> using namespace std; stack <int> st; int main() { string s; getline(cin, s); for(int i = 0; i < s.size(); i++) { char c = s[i]; if(c >= '0' && c <= '9') { st.push(c - '0'); } else { if(c == '+') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b + a); } if(c == '-') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b - a); } if(c == '*') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b * a); } if(c == '/') { int a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b / a); } } } cout << st.top(); return 0; } ``` ## 错因 未注意“**每个运算数和运算符之间用一个空格隔开**”及“**一位正整数**”。 # T692232 后缀表达式求值2 ## 题意 从键盘读入一个后缀表达式(字符串),只含有 0-9 组成的一位正整数作为运算数及加(+)、减(—)、乘(*)、除(/)(向下取整)四种运算符。每个运算数和运算符之间用一个空格隔开并以 `@` 结尾,保证给你的表达式合法,并保证原始后缀表达式中不存在形如 -1 这样的数字。 ## 思路 照题意模拟即可。 ## 代码 无^_^... ## $AC$ 代码 ```cpp #include <bits/stdc++.h> #define ll long long using namespace std; stack <ll> st;//开long long!!! int main() { string s; getline(cin, s); ll num; for(int i = 0; i < s.size(); i++) { char c = s[i]; if(c >= '0' && c <= '9') { num = num * 10 + (c - '0'); } else if(c == ' ') { st.push(num); num = 0; } else { if(c == '+') { ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b + a); } if(c == '-') { ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b - a); } if(c == '*') { ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b * a); } if(c == '/') { ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b / a); } } } cout << st.top(); return 0; } ``` **开long long!!!** # T692401 后缀表达式求值3 ## 题意 从键盘读入一个后缀表达式(字符串),只含有 0-9 组成的一位正整数作为运算数及加(+)、减(—)、乘(*)、除(/)(向下取整)四种运算符。每个运算数和运算符之间用一个空格隔开并以 `@` 结尾,保证给你的表达式合法,**并且原始后缀表达式中可能存在形如 -1 这样的数字**。 ## 思路 照题意模拟即可。 ## 代码 无^_^... ## $AC$ 代码 ```cpp #include <bits/stdc++.h> #define ll long long using namespace std; stack <ll> st;//开long long!!! int main() { string s; getline(cin, s); ll num = 0; int n = 1; bool flag = 0; for(int i = 0; i < s.size(); i++) { char c = s[i]; if(c >= '0' && c <= '9') { num = num * 10 + (c - '0'); flag = 1; } else if(c == ' ' && flag) { st.push(num * n); num = 0; n = 1; flag = 0; } else { if(c == '+') { ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b + a); } if(c == '-') { if(s[i + 1] >= '0' && s[i + 1] <= '9') { n = -1; continue; } ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b - a); } if(c == '*') { ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b * a); } if(c == '/') { ll a = st.top(), b; st.pop(); b = st.top(); st.pop(); st.push(b / a); } } } cout << st.top(); return 0; } ``` **开long long!!!** # T705513 最长的括号 ## 题意 有一排括号序列,其中包含小括号 `()`、中括号 `[]` 和大括号 `{}`,总长度为 $N$。定义以下括号序列是美观的: 1. 空的括号序列是美观的; 2. 若括号序列 $A$ 是美观的,则括号序列 `(A)`、`[A]`、`{A}` 也是美观的; 3. 若括号序列 $A、B$ 都是美观的,则括号序列 `AB` 也是美观的; ## 目的 在括号序列中,找出其中连续的一段,满足这段子序列是美观的,并且长度尽量大。 ## 思路 将所有美规的子括号序列删除后通过括号和括号的位置之差求出其长度,随后求出最大值。 ## 代码 无^_^... ## $AC$ 代码 ```cpp #include <bits/stdc++.h> using namespace std; struct St { char data; int position; }; stack <St> st; int main() { string s; cin >> s; st.push({'x', -1}); for(int i = 0; i < s.size(); i++) { char c = s[i]; if(st.empty()) { st.push({c, i}); } else if(c == ')' && st.top().data == '(') { st.pop(); } else if(c == ']' && st.top().data == '[') { st.pop(); } else if(c == '}' && st.top().data == '{') { st.pop(); } else { st.push({c, i}); } } st.push({'x', s.size()}); int maxx = INT_MIN; while(st.size() > 1) { int a = st.top().position; st.pop(); maxx = max(maxx, a - st.top().position - 1); } cout << maxx; return 0; } ```