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

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

相关推荐
qeen877 小时前
【数据结构】自平衡二叉搜索树各种旋转算法原理解析及AVL树的C++实现
数据结构·c++·算法
lucas_AI7 小时前
1.2B 小模型赢过 235B 大模型:NaviDC-OCR 把文档解析卷明白了
人工智能·深度学习·算法
Augustzero7 小时前
为什么线程不能说睡就睡?看懂等待与唤醒机制
c++·后端
程序员与背包客_CoderZ7 小时前
Linux D-Bus通信协议详解:从入门到C/C++编码实战
linux·服务器·c语言·c++·嵌入式硬件·嵌入式软件·dbus
冻柠檬飞冰走茶7 小时前
PTA基础编程题目集 7-35有理数均值(C++语言实现)
开发语言·数据结构·c++·算法·均值算法
wangchen_08 小时前
C++正则表达式
开发语言·c++·正则表达式
民乐团扒谱机8 小时前
【微实验】倒谱算法(Cepstrum)深度解析:原理、数学推导与代码实现
人工智能·算法·语音识别
Nil2089 小时前
leetcode 189轮转数组
数据结构·算法·leetcode
-dzk-9 小时前
【动态规划】LC 118.杨辉三角
算法·动态规划
恣逍信点9 小时前
《凌微经》静态自悖——变化之自因
人工智能·科技·算法·机器学习·业界资讯·拓扑学·哲学