给你们来点看不懂的

伯仲叔季  •  15天前


#include<iostream>
using namespace std; 
int nums[1000];
int main( )
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}
	for (int i = 0; i < n - 2; i++) {
		for (int j = i + 1; j < n - 1; j++) {
			for (int k = j + 1; k < n; k++) {
				if (nums[i] > nums[k] && nums[k] > nums[j])
				{
					cout << "NO" ;
					return 0;

				}
			}
		}
	}
	cout << "YES";
	return 0;
}

评论:

#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<int>num;
stack<char>op;

int check(char c) { //运算优先级
	int f = 0;
	if (c == '+' || c == '-')
		f = 1;
	else if (c == '*' || c == '/')
		f = 2;
	return f;
}

void deal() { //四则运算
	int a, b;
	char c;
	b = num.top();//右操作数
	num.pop();
	a = num.top();//左操作数
	num.pop();
	c = op.top();//运算符
	op.pop();
	int g = 0;
	//四则运算
	if (c == '+')
		g = a + b;
	else if (c == '-')
		g = a - b;
	else if (c == '*')
		g = a * b;
	else if (c == '/')
		g = a / b;
	num.push(g);
}

void calc() { //计算
	string s;
	cin >> s;
	int a, b, data = 0;
	char c;
	for (int i = 0; i < s.size(); i++) {//遍历字符串
		if (s[i] >= '0' && s[i] <= '9') {//数字则转化为整型数据
			while (s[i] >= '0' && s[i] <= '9') {
				data = s[i] - '0' + data * 10;
				i++;
			}
			i--;//判断数字后加一位,需要减一后到符号位
			num.push(data);
			data = 0;
		} else {
			if (s[i] == '(') {
				op.push(s[i]);//操作符栈
			} else if (s[i] == ')') {//处理括号的优先级
				while (op.top() != '(') {
					deal();
				}
				op.pop();
			} else {//处理运算符
				while (!op.empty() && check(op.top()) >= check(s[i])) {
					deal();
				}
				op.push(s[i]);
			}
		}
	}
	//最后一个运算符还没有运算
	while (!op.empty()) {
		deal();
	}
	cout << num.top();
}

int main() {
	freopen("expr10.in", "r", stdin);
	freopen("expr10.ans", "w", stdout);
	calc();
	return 0;
}

Gooooogle  •  15天前

请先登录,才能进行评论