王道机试 第二章 暴力求解 2.2 模拟 3、其他模拟

王道机试 第二章 暴力求解

2.2 模拟

3、其他模拟
例题2.9 剩下的树(清华大学复试上机题)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 10005;
bool tree[maxn];

int main()
{
	int L, m, a, b;
	cin >> L >> m;
	fill(tree, tree + maxn, 1);
	while (m--){
		cin >> a >> b;
		for (int i = a; i <= b; i++){
			tree[i] = false;
		}
	}
	int cnt = 0;
	for (int i = 0; i <= L; i++){
		if (tree[i]) cnt++;
	}
	cout << cnt << endl;
	return 0;
}
例题2.10 手机键盘
  • 解题关键
    1、要知道九宫格输入法的键盘布局
    2、判断两个字母是否在同一个按键上的方法:若两个字母的位置之差等于时间之差,则它们在同一个按键上。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int times[] = {
	1, 2, 3, // abc
	1, 2, 3, // def
	1, 2, 3, // ghi
	1, 2, 3,  // jkl
	1, 2, 3, // mno
	1, 2, 3, 4, // pqrs
	1, 2, 3, // tuv
	1, 2, 3, 4 // wxyz
};

int main()
{
	string s;
	while (cin >> s){
		int ans = 0;
		for (int i = 0; i < s.size(); i++){
			ans += times[s[i] - 'a'];
			if (i != 0 && s[i] - s[i - 1] == times[s[i] - 'a'] - times[s[i - 1] - 'a']){
				ans += 2;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
例题2.11 XXX定律(浙江大学复试上机题)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

// 对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。     
// 请计算需要经过几步才能将n变到1,具体可见样例。

int main()
{
	int n;
	while (cin >> n && n){
		int cnt = 0;
		while (n != 1){
			if (n % 2 == 0) {n /= 2; cnt++;}
			else{n = 3 * n + 1; n /= 2; cnt++;}
		}
		cout << cnt << endl;
	}
	return 0;
}
习题2.9 Grading(浙江大学复试上机题)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int maxx(int a, int b, int c){
	return (max(max(a, b), max(a, c)));
}

void solve(int P, int T, int G1, int G2, int G3, int GJ)
{
	float ans = 0.0;
	if (abs(G1 - G2) <= T) ans = (G1 + G2) / 2.0;
	else{
		if ((abs(G1 - G3) <= T && abs(G2 - G3) > T) || (abs(G1 - G3) > T && abs(G2 - G3) <= T)){
			if (abs(G1 - G3) < abs(G1 - G2)) ans = (G1 + G3) / 2.0;
			else ans = (G2 + G3) / 2.0;
		}
		else if (abs(G1 - G3) <= T && abs(G2 - G3) <= T) ans = maxx(G1, G2, G3);
		else ans = GJ;
	}
	printf("%.1f\n", ans);
	return;
}

int main()
{
	int P, T, G1, G2, G3, GJ;
	while (cin >> P >> T >> G1 >> G2 >> G3 >> GJ){
		solve(P, T, G1, G2, G3, GJ);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值