【数据结构】线性表

寄包柜题目描述

#include<iostream> 
#include<vector>
using namespace std;
int n, q, opt, i ,j, k;

int main() {
	cin >> n >> q;
	vector< vector<int> > locker(n + 1);
	while(q--) {
		cin >> opt;
		if(opt == 1) {
			cin >> i >> j >> k;
			if(locker[i].size() < j) locker[i].resize(j + 1);
			locker[i][j] = k;
		}
		else {
			cin >> i >> j;
			cout << locker[i][j] << endl;
		}
	}
	return 0;
}

后缀表达式题目描述

#include<iostream> 
#include<stack>
using namespace std;
stack<int> n;
int s = 0, x, y;

int main() {
	char ch;
	do {
		ch = getchar();
		if(ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
		else if (ch == '.') n.push(s), s = 0;
		else if (ch != '@') {
			x = n.top(); n.pop(); y = n.top(); n.pop();
			switch(ch) {
				case '+': n.push(y + x); break;
				case '-': n.push(y - x); break;
				case '*': n.push(y * x); break;
				case '/': n.push(y / x); break;
			} 
		}
	} while(ch != '@');
	cout << n.top() << endl;
	return 0;
}

约瑟夫问题题目描述 

//队列实现
#include<iostream> 
#include<queue>
using namespace std;
queue <int> q;
int n, m; 

int main() {
	cin >> n >> m;
	for(int i = 1; i <= n; i++) q.push(i);
	while(q.size()) {
		for(int i = 1; i < m; i++) {
			q.push(q.front());
			q.pop();
		} 
		cout << q.front() << ' ';
		q.pop();
	}
	return 0;
}

//链表实现
#include<iostream>
#include<list>
using namespace std;
list <int> a;

int main() {
	int n, m, cnt = 0;
	cin >> n >> m;
	for(int i = 1; i <= n; i++) 
	    a.push_back(i);
	list <int> :: iterator it, now;
	it = a.begin();
	while(!a.empty()) {
		cnt++;
		now = it;
		if(++it == a.end()) it = a.begin();
		if(cnt == m) {
			cout << *now << ' ';
			a.erase(now);
			cnt = 0;
		}
	}
    return 0;
}

机器翻译题目描述 

//可变数组vector方法
#include<iostream> 
#include<vector> 
#include<algorithm>
using namespace std;
vector <int> v;

int main() {
	int m, n, t, ans = 0;
	cin >> m >> n;
	while (cin >> t) {
		if (find(v.begin(), v.end(), t) == v.end()) { 
			v.push_back(t); 
			ans++;
		}
		if (v.size() > m) 
			v.erase(v.begin()); 
	}
	cout << ans << endl;
}

海港题目描述 

#include<iostream>
#include<queue>
using namespace std;
const int MAXN = 1e5 + 5;

struct node {
    int t, x;
} tx;
queue <node> ship;
int n, t, k, x, ans;
int temp[MAXN];

int main() {
	cin >> n;
	while(n--) {
		cin >> t >> k;
		while(!ship.empty()) {
			tx = ship.front();
			if(tx.t + 86400 <= t) {
				ship.pop();
				temp[tx.x]--;
				if(!temp[tx.x]) ans--;
				continue;
			}
			break;
		}
		while(k--) {
			cin >> x;
			tx.t = t, tx.x = x;
			ship.push(tx);
			temp[tx.x]++;
			if(temp[tx.x] == 1) ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

 验证栈序列题目描述

#include<iostream>
#include<stack>
using namespace std;
stack <int> q;
int p, n;
int main() {
	cin >> p; 
	while(p--) {
		cin >> n;
		int a[n+1], b[n+1], sum = 1;
		for(int i = 1; i <= n; i++) cin >> a[i];
		for(int i = 1; i <= n; i++) cin >> b[i];
		for(int i = 1; i <= n; i++) {
			q.push(a[i]);
			while(q.top() == b[sum]) {
				q.pop(), sum++;
				if(q.empty()) break; 
			} 
		} 
		if(q.empty()) cout << "Yes" << endl;
		else cout << "No" << endl;
		while(!q.empty()) q.pop();
	}
	return 0;
}

营业额统计题目描述 

#include<iostream>
#include<list>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = 32767;
int b[MAXN];
list <int> s;
list <int> :: iterator now, it, index[MAXN]; 
struct volume {
	int ai, l;
} a[MAXN]; 

bool cmp(volume x, volume y) {
	return x.ai < y.ai; 
}

int main() {
	int n, sum = 0;
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i].ai;
		a[i].l = i;
	}
	sort(a, a + n, cmp);
	it = s.begin();
	for(int i = 0; i < n; i++) {
		s.push_back(a[i].ai);
		index[i] = ++it;
		b[a[i].l] = i;
	} 
	while(n--) {
		int min = 1e6;
		now = index[b[n]];
		if(s.size() == 1) min = *now;
		else {
			it = now;
			if(it-- != s.begin()) if(fabs(*it - *now) < min) min = fabs(*it - *now);
			it = now;
			if(++it != s.end()) if(fabs(*it - *now) < min) min = fabs(*it - *now);			
		}
		s.erase(now);
		sum += min;
	}
	cout << sum << endl;
	return 0;
}
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级码立

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值