2019寒假训练day3-基础数据结构Ⅰ

时间:2019-01-23
本文章向大家介绍2019寒假训练day3-基础数据结构Ⅰ,主要包括2019寒假训练day3-基础数据结构Ⅰ使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

A.hdu2031简单进制转换

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<sstream>
#include<map>
using namespace std;
int main(){
	int n, r;
	while (scanf("%d%d", &n, &r) != EOF) {
		vector<char>V;
		bool flag = false;
		if (n < 0) {
			n = -n;
			flag = true;
		}
		while (n) {
			int v = n % r;
			if (v >= 10) {
				V.push_back('A' + v - 10);
			}
			else {
				V.push_back(v + '0');
			}
			n /= r;
		}
		if (flag) putchar('-');
		for (int i = V.size() - 1; i >= 0; i--) {
			printf("%c", V[i]);
		}
		putchar('\n');
	}
	return 0;
}

B.uva11995栈、队列、优先队列模拟

题意:这道题的题目是“猜猜数据结构”,就是给你一些输入输出数据,让你根据这些数据判断是什么数据结构。要猜的数据结构只有三种,栈(stack)、队列(queue)、优先队列(priority_queue)。输出有5种情况,前三种分别是确定了一种数据结构,第四种是三种数据结构都不符合,第五种是有2种或2种以上符合。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
using namespace std;
int main(){
	int n;
	while (scanf("%d", &n) != EOF) {
		priority_queue<int, vector<int>, less<int> > PQ;
		stack<int> S;
		queue<int> Q;
		bool flag1 = true, flag2 = true, flag3 = true;
		for (int i = 0; i < n; i++) {
			int a, b;
			scanf("%d%d", &a, &b);
			if (a == 1) {
				PQ.push(b);
				S.push(b);
				Q.push(b);
			}
			else {
				if (PQ.empty() || (PQ.top() != b)) {
					flag1 = false;
				}
				if(!PQ.empty()) PQ.pop();
				if (S.empty() || S.top() != b) {
					flag2 = false;
				}
				if(!S.empty()) S.pop();
				if (Q.empty() || Q.front() != b) {
					flag3 = false;
				}
				if(!Q.empty()) Q.pop();
			}

		}
		if (flag1 || flag2 || flag3) {
			if (flag1 && flag2 || flag1 && flag3 || flag2 && flag3) {
				printf("not sure\n");
			}
			else {
				if (flag1) {
					printf("priority queue\n");
				}
				else if (flag2) {
					printf("stack\n");
				}
				else if (flag3) {
					printf("queue\n");
				}
			}
		}
		else {
			printf("impossible\n");
		}
	}
	return 0;
}

C.hdu1005 矩阵乘法或者循环节为49

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
typedef long long ll;
using namespace std;
int main(){
	int a, b, n;
	while (scanf("%d%d%d", &a, &b, &n) != EOF) {
		if (a == 0 && b == 0 && n == 0) break;
		vector<int> V;
		V.push_back(1);
		V.push_back(1);
		n %= 49;
		for (int i = 0; i < n - 2; i++) {
			int vv = (V[V.size() - 1] * a + V[V.size() - 2] * b) % 7;
			V.push_back(vv);
		}
		printf("%d\n", V[V.size() - 1]);
	}
	return 0;
}

 D.hdu1022火车进出站模拟栈

题意:输入火车的进站次序与火车的出站次序,看是否能按照这种次序进站出站,如果可以输出Yes,并输出in和out的次序,如果不能则输出No

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
typedef long long ll;
using namespace std;
char s1[1005], s2[1005];
bool vis[1005];
int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		memset(vis, false, sizeof(vis));
		stack<char> S;
		scanf("%s%s", s1, s2);
		int p = 0, count = 0;
		for (int i = 0; i < n; i++) {
			S.push(s1[i]);
			vis[count++] = true;
			while(!S.empty() && S.top() == s2[p]) {
				p++;
				S.pop();
				vis[count++] = false;
			}
		}
		if (count == 2 * n) {
			printf("Yes.\n");
			for (int i = 0; i < count; i++) {
				if (vis[i]) {
					printf("in\n");
				}
				else {
					printf("out\n");
				}
			}
		}
		else {
			printf("No.\n");
		}
		printf("FINISH\n");
	}
	return 0;
}

E.hdu1237简单计算器实现 

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
typedef long long ll;
using namespace std;
int main() {
	string str;
	while (getline(cin, str)) {
		if (str == "0") return 0;
		//cout << str << endl;
		stack<double> S;
		int len = str.length();
		for (int i = 0; i < len; i++) {
			if (str[i] == ' ') continue;
			int v = 0;
			bool sign = false;
			while (isalnum(str[i]) && i < len) {
				v = v * 10 + str[i] - '0';
				i++;
				sign = true;
			}
			if(sign) S.push(v);
			if (i < len && str[i] == ' ') i++;
			if (str[i] == '*') {
				i += 2;
				double p = S.top();
				S.pop();
				v = 0;
				while (isalnum(str[i]) && i < len) {
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(p * (double)v);
			}
			else if (str[i] == '/') {
				i += 2;
				double p = S.top();
				S.pop();
				v = 0;
				while (str[i] >= '0' && str[i] <= '9' && i < len) {
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(p / v);
			}
			else if (str[i] == '-') {
				i += 2;
				v = 0;
				while (str[i] >= '0' && str[i] <= '9' && i < len) {
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(-v);
			}
			else if (str[i] == '+') {
				i += 2;
				v = 0;
				while (str[i] >= '0' && str[i] <= '9' && i < len) {
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(v);
			}
		}
		double res = 0;
		while (!S.empty()) {
			//printf("%f\n", S.top());
			res += S.top();
			S.pop();
		}
		printf("%.2f\n", res);
	}
	return 0;
}

F.hdu1896结构体定义优先级用于优先队列

题意:现在来扔石头,如果是奇数次就扔,偶数次就不扔,现在告诉我们石头的初始坐标和能扔的距离,问最远的石头离初始(0)有多远。(如果在一个位置遇见多个石头,则先扔扔出距离小的)

方法:直接模拟扔石头的过程,把每个石头加入到优先队列中(位置的优先级最高,其次是能扔的距离),奇数次则把该石头扔出去(出队,pos+=dis再次入队),偶数次则直接出队,直到队空为止。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
typedef long long ll;
using namespace std;
struct Stone{
	int p;
	int d;
	bool operator < (const Stone &a) const {
		return p > a.p || (p == a.p && d > a.d);
	}
};
int main(){
	int T;
	scanf("%d", &T);
	while (T--) {
		int n;
		scanf("%d", &n);
		priority_queue<Stone, vector<Stone> > pq;
		Stone vv;
		for (int i = 0; i < n; i++) {
			int p, d;
			scanf("%d%d", &p, &d);
			vv.p = p;
			vv.d = d;
			pq.push(vv);
		}
		/*while (!pq.empty()) {
			printf("%d %d\n", pq.top().p, pq.top().d);
			pq.pop();
		}*/
		bool flag = true;
		while (!pq.empty()) {
			vv = pq.top();
			pq.pop();
			if (flag) {
				vv.p += vv.d;
				pq.push(vv);
			}
			flag = !flag;
		}
		printf("%d\n", vv.p);
	}
	return 0;
}

G.uva127超长英文题

题意:

题意:

模拟一种叫Accordian的纸牌游戏:

一副扑克牌有52张牌,首先把纸牌一张一张从左到右排好(不能有重叠,所以共有52堆牌,每堆一张),当某一张牌与它左边那张牌或者左边的第三张牌有Match的时候,就把这张牌移到那张牌上面去。在这里两张牌Match指的是这两张牌的花色(suit)或者点数(rank)一样。当你做了一个移动之后,要观察是否还可以做其他的移动。在任何时间,只有最上面那张牌可以移动。如果因为移动一张牌使得产生一个空格(也就是被移动的那堆牌只有一张牌),你必须把右边所有的牌堆往左移一格。如此不断的寻找可移动的牌,直到没有一张牌可以移动,结束游戏。

当有两张牌可以移动,你应该选择最左边那张牌。当一张牌可以移动到左边一格或者左边三格的时候,你必须移动到左边三格。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
typedef long long ll;
using namespace std;
struct Accordian{
	char num;
	char shape;
};
vector< vector<Accordian> > Lists;
bool IsSame(Accordian a, Accordian b) {
	if (a.num == b.num || a.shape == b.shape) {
		return true;
	}
	return false;
}
int main() {
	Accordian p;
	vector<Accordian> V;
	while (cin >> p.num && p.num != '#') {
		cin >> p.shape;
		V.clear();
		Lists.clear();
		V.push_back(p);
		Lists.push_back(V);
		for (int i = 0; i < 51; i++) {
			cin >> p.num >> p.shape;
			V.pop_back();
			V.push_back(p);
			Lists.push_back(V);
		}
		bool sign = true;
		while (sign) {
			sign = false;
			int len = Lists.size();
			int Left , space = 0;
			for (int i = len - 1; i >= 1; i--) {
				if (i >= 3 && IsSame(Lists[i].back(), Lists[i - 3].back())) {
					Left = i;
					space = 3;
					sign = true;
				}
				else if (i >= 1 && IsSame(Lists[i].back(), Lists[i - 1].back())) {
					Left = i;
					space = 1;
					sign = true;
				}
			}
			if (sign) {
				Lists[Left - space].push_back(Lists[Left].back());
				Lists[Left].pop_back();
				if (Lists[Left].empty()) {
					Lists.erase(Lists.begin() + Left);
				}
			}
			
		}
		printf("%d", Lists.size());
		if (Lists.size() == 1) {
			printf(" pile remaining: %d\n", Lists[0].size());
		}
		else if (Lists.size() > 1) {
			printf(" piles remaining:");
			for (int i = 0; i < Lists.size(); i++) {
				printf(" %d", Lists[i].size());
			}
			putchar('\n');
		}
	}
	return 0;
}