2020蓝桥杯模拟赛

2020蓝桥杯校内模拟赛C/C++

前四题基础填空比较水为 计算器算就行

程序代码题

#include <string>
#include <iostream>
using namespace std;
char c[5] = { 'a','e','i','o','u' };
int check(char s) {
	int tag = 0;
	for (int i = 0; i < 5; i++) {
		if (s == c[i]) {
			tag = 1;
		}
	}
	if (tag == 1) return 1;
	return 0;
}
int main() {
	string s;
	cin >> s;
	int i = 0, j;
	int tag = 0, cnt = 0;//0为辅音
	
	while (i < s.size()) {
		while (check(s[i]) == tag && i < s.size()) {
			i++;
		}
		if (tag) tag = 0;//状态翻转
		else
			tag = 1;
		cnt++;

	}
	if (cnt == 4) {
		printf("Yes\n");
	}
	else printf("No\n");
	return 0;
}

#include <string>
#include <iostream>
using namespace std;
int Sum(int a) {
	int sum = 0;
	while (a) {
		sum += (a % 10) * (a % 10);
		a /= 10;
	}

	return sum;
}
int main() {
	int n, m;
	int ans = 0;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		if (Sum(i) == m) ans++;
	}
	cout << ans << endl;
	return 0;
}

#include <string>
#include <iostream>
using namespace std;
int main() {
	int n, a, b, c;
	cin >> n;
	cin >> a >> b >> c;
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		if (i % a != 0 && i % b != 0 && i % c != 0) {
			cnt++;
		}
	}
	cout << cnt << endl;
	return 0;
}

O(m * n)最坏时间复杂度

#include <string>
#include <iostream>
using namespace std;
int n, m;
int r, c;

bool in(int x, int y) {
	if (x + 1 == c && y + 1 == r) {
		return true;
	}
	else
		return false;
}

int main() {
	
	cin >> n >> m;
	cin >> c >> r;
	int x = 0, y = 0;//初始位置

	int a = 0 , b = 0;
	int s = 0;
	while (1) {//模拟过程
		
		for (int i = 0; i < m - 2 * a; i++) { // 右走
			s++;
			if (in(x, y)) {
				cout << s << endl;
				return 0;
			}
			y++;
			
		}
		y--;
		x++;
		for (int i = 0; i < n - 2 * a - 1; i++) {// 下走
			s++;
			if (in(x, y)) {
				cout << s << endl;
				return 0;
			}
			x++;

		}
		x--;

		y--;
		for (int i = 0; i < m - 2 * a - 1; i++) { // 左走
			s++;
			if (in(x, y)) {
				cout << s << endl;
				return 0;
			}
			y--;

		}
		y++;
		x--;
		for (int i = 0; i < n - 2 * a - 2; i++) { // 上走
			s++;
			if (in(x, y)) {
				cout << s << endl;
				return 0;
			}
			x--;

		}
		x++;
		y++;

		a++; //进入下一个模拟

	}
	
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/DengSchoo/p/12560688.html