AtCoder Beginner Contest 295 A~C

A - Probably English

模拟

#include 

using namespace std;

const int N = 110;
string a[N];

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++) cin >> a[i];
	bool f = false;
	for(int i = 0; i < n; i++) {
		if(a[i] == "and" || a[i] == "not" || a[i] == "that" || a[i] == "the" || a[i] == "you") {
			f = true;
			break;
		}
	}
	if(f) cout << "Yes";
	else cout << "No";
	return 0;
}

B - Bombs

数字1~9为炸弹,威力为它的数值,只要距离小于等于到炸弹的曼哈顿距离,就会被炸到。

#include 

using namespace std;

const int N = 30;
char a[N][N];

int main() {
	int r, c;
	cin >> r >> c;
	for(int i = 0; i < r; i++) {
		for(int j = 0; j < c; j++) {
			cin >> a[i][j];
		}
	}

	for(int i = 0; i < r; i++) {
		for(int j = 0; j < c; j++) {
			if(a[i][j] - '0'>= 1 && a[i][j] -'0' <= 9) {
				// a[i][j] = '.';
				for(int k = 0; k < r; k++) {
					for(int b = 0; b < c; b++) {
						if(abs(k-i)+abs(b-j) <= a[i][j] - '0') {
							if(a[k][b] >= '0' && a[k][b] <= '9') continue;
							a[k][b] = '.';
						}
					}
				}
				a[i][j] = '.';
			}
		}
	}

	for(int i = 0; i < r; i++) {
		for(int j = 0; j < c; j++) {
			cout << a[i][j];
		}
		cout << endl;
	}
	return 0;
}

C - Socks

贪心问题先排个序,每次操作取两个数值相等的。取完后将他俩赋为不可能的值,以防下次比较取到重复的数。

#include 

using namespace std;

const int N = 5e5 + 10;
int a[N];
int n;

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> a[i];
	sort(a, a + n);

	int cnt = 0;
	for(int i = 1; i <= n; i++) {
		if(a[i-1] == a[i]) {
			a[i-1] = -1;
			a[i] = -1;
			cnt++;
		}
	}
	cout << cnt;

	return 0;
}

你可能感兴趣的:(AtCoder,c语言,c++,算法)