【算法1-1】模拟与高精度

这篇博客主要介绍了多个编程竞赛中的模拟和高精度计算问题,包括乒乓球比赛、扫雷游戏、玩具谜题等,通过实例解析了如何运用乘法原理进行高精度计算,并分享了在解决这些问题时的经验和技巧,如模拟、分类讨论和细心处理特殊情况。
摘要由CSDN通过智能技术生成

P1042 [NOIP2003 普及组] 乒乓球

题目链接:P1042 [NOIP2003 普及组] 乒乓球 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
#include <cmath>
using namespace std;
int res[100000];

int main() {
	int cnt, a = 0, b = 0;
	char c;
	for (cnt = 0; cin >> c && c != 'E'; cnt++) {
		if (c == 'W') {
			res[cnt] = 1;
		}
	}
	if (cnt == 0) {
		cout << "0:0" << endl;
		cout << endl;
		cout << "0:0";
		return 0;
	}
	//11分制
	for (int i = 0; i < cnt; i++) {
		if (res[i] == 1) {
			a++;
		} else {
			b++;
		}
		if ((a >= 11 || b >= 11) && abs(a - b) >= 2) {
			cout << a << ':' << b << endl;
			a = b = 0;
		}
	}
	cout << a << ':' << b << endl;
	cout << endl;
	a = b = 0;
	//21分制
	for (int i = 0; i < cnt; i++) {
		if (res[i] == 1) {
			a++;
		} else {
			b++;
		}
		if ((a >= 21 || b >= 21) && abs(a - b) >= 2) {
			cout << a << ':' << b << endl;
			a = b = 0;
		}
	}
	cout << a << ':' << b << endl;
	return 0;
}

P2670 [NOIP2015 普及组] 扫雷游戏

题目链接:P2670 [NOIP2015 普及组] 扫雷游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
using namespace std;
int a[105][105];

int main() {
	int n, m;
	cin >> n >> m;
	char c;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> c;
			if (c == '*') {
				a[i][j] = 1;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (a[i][j] == 1) {
				cout << '*';
			} else {
				cout << a[i + 1][j + 1] + a[i][j + 1] + a[i + 1][j] + a[i - 1][j - 1] + a[i - 1][j] + a[i][j - 1] + a[i - 1][j + 1] +
				     a[i + 1][j - 1];
			}
		}
		cout << endl;
	}
	return 0;
}

P1563 [NOIP2016 提高组] 玩具谜题

题目链接:P1563 [NOIP2016 提高组] 玩具谜题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class Person {
	public:
		string name;
		int flag;
};
Person p[100005];

int main() {
	int n, m, a, b, index = 0;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> p[i].flag >> p[i].name ;
	}
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		if (a ^ p[index].flag) {
			index = (index + b) % n;
		} else {
			if (index - b >= 0) {
				index -= b;
			} else {
				index = n - (abs(index - b) % n);
			}
		}
	}
	cout << p[index].name ;
	return 0;
}

P1601 A+B Problem(高精) 

题目链接:P1601 A+B Problem(高精) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
#include <string>
using namespace std;

int main() {
	string a, b;
	cin >> a >> b;
	if (a.size() > b.size()) {
		for (int i = b.size(); i < a.size(); i++) {
			b = '0' + b;
		}
	} else if (a.size() < b.size()) {
		for (int i = a.size(); i < b.size(); i++) {
			a = '0' + a;
		}
	}
	int c, d;
	for (int i = a.size() - 1; i >= 0; i--) {
		c = (a[i] - '0' + b[i] - '0') % 10;
		d = (a[i] - '0' + b[i] - '0') / 10;
		a[i] = c + '0';
		if (i != 0) {
			a[i - 1] += d;
		} else if (d != 0 && i == 0) {
			cout << d;
		}
	}
	cout << a;
	return 0;
}

P1303 A*B Problem 

题目链接:P1303 A*B Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <iostream>
#include <string>
#include <algorithm>
us
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值