2.25 做题

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;
#define LL long long;
int main()
{
	int row, col; cin >> row >> col;
	vector<vector<int>>arr(row + 5, vector<int>(col + 5));
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			cin >> arr[i][j];
		}
	}
	int top = 0;
	int bottom = row-1;
	int left = 0;
	int right = col - 1;
	while (top <= bottom && left <= right)
	{
		for (int i = left; i <= right; i++)
		{
			cout << arr[top][i] << endl;
		}
		top++;
		for (int i = top; i <= bottom; i++)
		{
			cout << arr[i][right] << endl;
		}
		right--;
		if (top <= bottom)
		{
			for (int i = right; i >= left; i--)
			{
				cout << arr[bottom][i] << endl;
			}
		}
		bottom--;
		if (left <= right)
		{
			for (int i = bottom; i >= top; i--)
			{
				cout << arr[i][left] << endl;
			}
		}
		left++;
	}
	return 0;
}

思路:环形输出,四个for循环,第一个for是上面的从左到右,第二个是右边从上到下,第三个是底下从右到左,第四个是左边的从下到上,一个圈循环完毕,while里面的条件是保证至少有一列和一行,但是前两个for之后,行和列都减少了一个,不能保证还有,所以后两个循环要加if条件

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int MOD = 10000;
int main() {
    string s;
    cin >> s;
    vector<long long> st;
    long long num = 0;
    char last_op = '+';
    for (int i = 0; i <= s.size(); ++i) {
        if (i < s.size() && isdigit(s[i])) {
            num = num * 10 + (s[i] - '0');
            num %= MOD;
        } else {
            if (last_op == '+') {
                st.push_back(num);
            } else if (last_op == '*') {
                long long top = st.back();
                st.pop_back();
                st.push_back((top * num) % MOD);
            }
            num = 0;
            if (i < s.size()) {
                last_op = s[i];
            }
        }
    }
    long long ans = 0;
    for (int i = 0; i < st.size(); ++i) {
        ans = (ans + st[i]) % MOD;
    }
    cout << ans << endl;
    return 0;
}

就是先把所有数字放进栈中,如果有乘法,先乘后入栈,入栈后全部遍历相加,最后取模

相关推荐
sali-tec19 分钟前
C# 基于OpenCv的视觉工作流-章108-区域筛选
图像处理·人工智能·opencv·算法·计算机视觉
Gigavision6 小时前
基于BUAA-MIHR数据集的噪声解耦对比学习算法
人工智能·python·深度学习·算法
鹿角片ljp6 小时前
LeetCode 64:最小路径和复盘|二维 DP 与 ACM 模式完整写法
java·数据结构·算法
彧azz7 小时前
算法设计与分析:贪心与动态规划
数据结构·学习·算法·贪心算法·动态规划
Beyond_System|系统之外8 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙55238 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
汉克老师9 小时前
GESP2026年9月认证C++六级( 第三部分编程题(1、数组划分))精讲
c++·gesp·小学生·学c++编程
千里码aicood9 小时前
基于PLC与机器视觉的智能分拣控制系统设计
c++·自动化·plc
漂流瓶jz10 小时前
UVA-1442 洞穴 题解答案代码 算法竞赛入门经典第二版
c++·算法·题解·aoapc·算法竞赛入门经典·uva
朽棘不雕10 小时前
进一步了解模板
c++