数据结构作业(1)

A

#include<iostream>
#include<map>
using namespace std;
map<string, int> mp;
int n, s;
string name;

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> name >> s;
		mp[name] = s;
	}
	cin >> name;
	cout << mp[name] << endl;

	return 0;
}

B

#include<iostream>
#include<vector>
using namespace std;
const int N = 2010;
int a[N], n, m, x, k;
int tag[N];
vector<int> insert[N];


int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    cin >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> k >> x;
        tag[k] = 1;
        insert[k].push_back(x);
    }
    for (int i = 1; i <= n; i++)
    {
        if (tag[i]) for (int j = 0; j < insert[i].size(); j++) cout << insert[i][j] << " ";
        cout << a[i] << " ";
    }

    return 0;
}

C

#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
const int N = 1000;
int a[N], cnt = 0;
string op1,op2;


int main()
{
	getline(cin, op1);
	getline(cin, op2);

	istringstream ss1(op1), ss2(op2);
	while (ss1 >> a[++cnt]);
	while (ss2 >> a[++cnt]);
	sort(a + 1, a + cnt);
	for (int i = 2; i < cnt; i++) cout << a[i] << " ";

	return 0;
}

D

#include<iostream>
#include<sstream>
#include<climits>
using namespace std;
const int N = 1000;
int a[N], maxn = INT_MIN, n = 1, ans;
string op;



int main()
{
	getline(cin, op);
	istringstream ss(op);
	while (ss >> a[n]) n++;
	n--;

	for (int i = 1; i <= n; i++)
	{
		if (a[i] > maxn)
		{
			maxn = a[i];
			ans = 1;
		}
		else if (a[i] == maxn)
		{
			ans++;
		}
		else continue;
	}

	cout << ans << endl;

	return 0;
}

E

#include<iostream>
using namespace std;
const int N = 105;
struct Node {
	int val;
	int next;
	int pre;
}Nodes[N];

int main()
{
	int n, m;
	cin >> n >> m;
	Nodes[0].next = 1;
	for (int i = 1; i <= n; i++)
	{
		Nodes[i].val = i;
		Nodes[i].next = i + 1;
	}
	Nodes[n].next = 1;
	int now = 1, preN = 1;
	while ((n--) > 1)
	{
		for (int i = 1; i < m; i++)
		{
			preN = now;
			now = Nodes[now].next;
		}
		cout << Nodes[now].val << ' ';
		Nodes[preN].next = Nodes[now].next;
		now = Nodes[preN].next;
	}
	cout << Nodes[now].val;
	return 0;
}

F

#include<iostream>
#include<stack>
using namespace std;
string s;
stack<char> st;
bool flag = true;

string err(char s)
{
    if (s == '(' || s == ')') return "NO1";
    else if (s == '[' || s == ']') return "NO2";
    else return "NO3";
}

void solve()
{
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == '(' || s[i] == '[' || s[i] == '{') st.push(s[i]);
        else if (s[i] == ')')
        {
            if (st.empty()) cout << err(')'), flag = false;
            else if (st.top() != '(') cout << err(st.top()), flag = false;
            else st.pop();
        }
        else if (s[i] == ']')
        {
            if (st.empty()) cout << err(']'), flag = false;
            else if (st.top() != '[') cout << err(st.top()), flag = false;
            else st.pop();
        }
        else if (s[i] == '}') 
        {
            if (st.empty()) cout << err('}'), flag = false;
            else if (st.top() != '{') cout << err(st.top()), flag = false;
            else st.pop();
        }
        else continue;
        if (!flag) return;
    }

    
    if (!st.empty()) {
        cout << err(st.top());
        flag = false;
    }

    
    if (flag) cout << "YES";
}

int main()
{
    cin >> s;
    solve();
    return 0;
}

G

#include<iostream>
#include<stack>
using namespace std;
string s, ans;
stack<char> st;

int pri(char op)
{
	switch (op)
	{
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	}
	return 0;
}

bool isNum(char op)
{
	return '0' <= op && op <= '9';
}


int main()
{
	cin >> s;

	for (int i = 0; i < s.size(); i++)
	{
		if (isNum(s[i])) {
			// 处理多位数字
			while (i < s.size() && isNum(s[i])) {
				ans += s[i];
				i++;
			}
			ans += ' ';  
			i--;  // 回退一步,因为外层循环会再次i++
		}
		else
		{
			switch (s[i])
			{
			case '(':
				st.push(s[i]);
				break;
			case ')':
				while (!st.empty() && st.top() != '(')
				{
					ans += st.top();
					ans += ' ';  
					st.pop();
				}
				if (!st.empty()) st.pop();  
				break;
			case '+':
			case '-':
			case '*':
			case '/':
				while (!st.empty() && pri(st.top()) >= pri(s[i]))
				{
					ans += st.top();
					ans += ' ';  
					st.pop();
				}
				st.push(s[i]);
				break;
			}
		}
	}

	while (!st.empty())
	{
		if (st.top() != '(' && st.top() != ')') {
			ans += st.top();
			ans += ' ';  
		}
		st.pop();
	}

	cout << ans << endl;

	return 0;
}

H

字符间有多个空格存在(简直逆天)

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int t;
string s;

int main()
{
    cin >> t;
    cin.ignore();
    while (t--)
    {
        getline(cin, s);
        int start = 0;
        int end = 0;

        while (start < s.size())
        {
            while (start < s.size() && s[start] == ' ')
                start++;
            if (start >= s.size())
                break;
            end = start;
            while (end < s.size() && s[end] != ' ')
                end++;
            reverse(s.begin() + start, s.begin() + end);
            start = end;
        }
        cout << s << endl;
    }

    return 0;
}
posted @ 2025-06-05 15:41  _P_D_X  阅读(74)  评论(1)    收藏  举报