C++学习日记---第11天(蓝桥杯超级基础题)

精选好题

1.最省钱的购买方案

学习到的知识点

1.利用min,max函数比较两数的大小

前面我们说输出一个列表的最大值或者最小值可以使用for循环搭配if语句来实现,这里我们学习一个新的关键字min和max来简化我们的代码

注:这两个函数在使用时需要先引入 algorithm

a.min函数

作用:比较两个数(整数或者浮点数)的大小,返回较小的数

语法:min(数1,数2)

例:

#include <algorithm>
min(10,20);

b.max函数

作用:比较两个数(整数或者浮点数)的大小,返回较大的数

语法:max(数1,数2)

例:

#include <algorithm>
max(10,20);

 那么对于一个数组,我们输出它的最大值或者最小值时可以延续之前的思路用for循环把每个数都拿出来比一比

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

int main() {
	int arr[] = { 2,3,6,1,0,9 };
	int max_arr = 0;
	int len = sizeof(arr) / sizeof(arr[0]);
	for (int i = 0; i < len; i++) {
		max_arr = max(arr[i], max_arr);
	}
	cout << max_arr;
	return 0;
}
答案
#include<iostream>
#include <algorithm>
using namespace std;

const int N = 100;//这里定义N的目的是确定数组的长度,又数组的长度必须是常量,因此前面要加const
int d[N];

int main() {
	int n, p, q;
	cin >> n >> p >> q;
	int min_d = 2e5;
	for (int i = 0; i < n; i++) {
		cin >> d[i];
		min_d = min(min_d, d[i]);
	}
	cout << min(min_d + q,p);
	return 0;
}

2.检查字符串

 学习到的知识点

1.检查触发器

当我么需要判断一个字符串或者一个数组是否有元素时,我们可以遍历这个数组或字符串,当出现特定元素时就做个记号,这样后面我们看到这个记号时就可以确定这个字符串或者这个数组出现了特定元素。那么如何做记号呢?下面是两个常用的方法:

a.bool记号

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

const int N = 100;
string s;

int main() {
	int n;
	cin >> n;
	cin >> s;
	bool found1 = false;
	for (int i = 0; i < n; i++) {
		if (s[i] == 'A') {
			found1 = true;
		}
	}
	if (found1) {
		cout << "找到了" << endl;
	}
	else {
		cout << "没找到" << endl;
	}
	return 0;
}

b. 数组记号

在这个方法中,我们定义一个数组,如果出现A,那么我们把第0个元素设置为1,最后检查这个数组的第0个元素是否为1即可

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

const int N = 100;
string s;

int main() {
	int n;
	cin >> n;
	cin >> s;
	int arr[1];
	for (int i = 0; i < n; i++) {
		if (s[i] == 'A') {
			arr[0] = 1;
		}
	}
	if (arr[0]==1) {
		cout << "找到了" << endl;
	}
	else {
		cout << "没找到" << endl;
	}
	return 0;
}
答案
法一:bool记号
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 100;
string s;

int main() {
	int n;
	cin >> n;
	cin >> s;
	bool found1 = false;
	bool found2 = false;
	bool found3 = false;
	for (int i = 0; i < n; i++) {
		if (s[i] == 'A') {
			found1 = true;
		}
		if (s[i] == 'B') {
			found2 = true;
		}
		if (s[i] == 'C') {
			found3 = true;
		}
		if (found1 && found2 && found3) {
			cout << i+1;
			break;
		}
	}
	return 0;
}
法二:数组记号
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 100;
string s;

int main() {
	int n;
	cin >> n;
	cin >> s;
	int flag[3] = { 0 };
//定义一个长度为3的数组,数组的初始值为0
	for (int i = 0; i < n; i++) {
		flag[s[i] - 'A'] = 1;
//s[i]-'A'实际上计算的是ASCII码,当s[i]为A时,会将数组flag的第0个元素设置为1,以此类推。
		int sum = 0;
//每一次循环都更新sum,确保sum在等于3的时候表示的是A,B,C已经全部出现
		for (int j = 0; j < 3; j++) {
			sum += flag[j];
//将数组的三个元素相加
		}
		if (sum == 3) {
			cout << i + 1 << endl;
			break;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值