求助!为什么我用字符串读入就不行
查看原帖
求助!为什么我用字符串读入就不行
405732
VecDouble楼主2022/4/28 12:54

字符串读入,4040 分:

#include <bits/stdc++.h>

#define print(x) printf("%d\n", x)
#define FOR(i, l, r) for(int i = l; i <= r; ++i)

using namespace std;

const int N = 1e6 + 10;

inline int op(const char *);
inline int read(void);

int n, I = 1;
char str[1000];
deque <int> q;

int main(void) {
	n = read();
	
	FOR(i, 1, n) {
		cin.get(str, 100).get();
		
		switch (op(str)) {
			case 1 : {
				q.push_front(I++);
				break;
			}
			
			case 2 : {
				q.push_back(I++);
				break;
			}
			
			case 3 : {
				FOR(j, 1, str[4] - '0') {
					q.pop_front();
				}
				break;
			}
			
			case 4 : {
				FOR(j, 1, str[4] - '0') {
					q.pop_back();
				}
				break;
			}
		}
	}
	
	while (!q.empty()) {
		printf("%d\n", q.front());
		q.pop_front();
	}
	
	return 0;
}

inline int op(const char * s) {
	if (s[0] == 'A') {
		if (s[2] == 'L') {
			return 1;
		}
		else return 2;
	}
	
	else {
		if (s[2] == 'L') {
			return 3;
		}
		else return 4;
	}
}

inline int read(void) {
	int res = 0;
	char c[1000] = {};
	
	cin.get(c, 100).get();
	
	for(int i = 0; c[i]; ++i) {
		if (isdigit(c[i])) {
			res = res * 10 + c[i] - '0';
		}
	}
	
	return res;
}

用单个字符读入就能 ACAC

#include <bits/stdc++.h> 

using namespace std;

int n, c, I = 1;
deque <int> q;
char a, b;

int main() {
	scanf("%d", &n);
	
	for(int i = 1; i <= n; ++i) {
		cin >> a >> b;
		if (a == 'A') {
			if (b == 'L') q.push_front(I++);
			else q.push_back(I++);
		}
		else {
			scanf("%d", &c);
			if (b == 'L') while (c--) q.pop_front();
			else while (c--) q.pop_back();
		}
	}
	
	while (!q.empty()) {
		cout << q.front() << '\n';
		q.pop_front();
	}
	
	return 0;
}

求助!!!

2022/4/28 12:54
加载中...