蓝桥杯研究生组C/C++程序设计例题分析

第十届蓝桥杯大赛C/C++程序设计省赛

1 试题A-立方和

1.1 问题描述

在这里插入图片描述

1.2 代码实现

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>//常用的算法函数库,eg:sort()等 
#define LL long long 
using namespace std;

bool CheckNum(int num)
{
	//这个循环是为了遍历num数字的每一位数字,
	//并对每一位数字进行判断,从最低为开始判断 
	while (num > 0)
	{
		int m = num % 10;
		if (m == 0 || m == 1 || m == 2 || m == 9)
			return true;
		num /= 10;
	}
}

int main()
{
	LL ans = 0;
	int n;
	
	cin >> n;
	for (int i = 1; i <= n; i++){
		//判断当前数字i是否为符合要求的数字,
		//如果不是就跳过当前这一次循环,直接进行下一轮循环,判断新的i 
		if (!CheckNum(i))
			continue;
		ans += (LL)(i * i) * i;
	}
	
	cout << ans << endl;
	
	
	return 0;
}

1.3 运行结果

在这里插入图片描述
在这里插入图片描述

2 试题B-字符串数字

2.1 试题描述

在这里插入图片描述

2.2 代码实现

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

int main(){
	char str[100];//定义字符串数组
	int str_len = 0;
	
	gets(str); //可以读入一行当中的空格,遇到换行符就停止读入 
	//或者scanf("%s", str); //遇到空格和换行符就停止读入 
	str_len = strlen(str);//调用的cstring库中的函数
	
	long long ans = 0;
	for (int i = 0; i < str_len; i++){
		ans = ans * 26 + int(str[i] - 'A' + 1);
	}
	
	cout << ans << endl;
	
	return 0;
}

2.4 运行结果

在这里插入图片描述

3 试题C-质数

3.1 试题描述

在这里插入图片描述

3.2 代码实现

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

bool JudgePrime(int num)
{
	/*
	判断num是否为一个素数(质数) 
	*/ 
	if (num == 1) return false;
	//这里是运用了一个判断素数的一个便捷的方法 ,
	//即判断2-sqrt(num)之间的整数有没有可以被num整除的,如果有num就不是素数 
	int m = int(sqrt(num) + 1);
	for (int i = 2; i <= m; i++)
	{
		if (num % i == 0)
			return false;
	}
	return true;
}

int main()
{
	int arr[2200];//用来存储素数,第一个素数,第二个素数,....
	int index = 0;//arr数组的索引
	int i;
	
	i = 2;
	//定义一个无线循环,当得到了所有的2019个素数就跳出循环 
	while (true){
		//判断当前的i是不是质数,如果是就添加到arr数组当中,然后index+1 
		if (JudgePrime(i)){
			arr[index] = i;
			index++;
		}
		//判断是否已经找到2019个素数 
		if (index >= 2019)
			break;//跳出当前的循环语句(while) 
		
		i++;
	}
	
	cout << arr[2018] << endl;
	 
	
	return 0;
}

3.3 运行结果

在这里插入图片描述

其他试题分析

1 数的分解

1.1 试题描述

在这里插入图片描述

1.2 代码实现

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

bool CheckNum(int num)
{
	while (num > 0)
	{
		int m = num % 10;
		if (m == 2 || m == 4)
			return false;
		num /= 10;
	}
	return true;
	
}

int main()
{
	int ans = 0;
	
	//因为是三个整数,所以用三个for循环分别遍历三个整数的值,
	//因为三个整数各不相同,而且和整数的顺序无关,所以我们采用i < j < k的方式,去除因为顺序不同而重复的结果 
	for (int i = 1; i <= 2019; i++)
	{
		if (!CheckNum(i)) continue;
		for (int j = i+1; j <= 2019; j++)
		{
			if (!CheckNum(j)) continue;
			for (int k = j+1; k <= 2019; k++)
			{
				if (!CheckNum(k)) continue;
				//判断是否满足条件 
				if (i + j + k == 2019)
					ans++;
			}
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

1.3 运行结果

在这里插入图片描述

2 快速排序

实现快速排序的几种方式

2.1 基本的一级排序(只对一个变量进行比较排序)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

bool cmp1(const int a, const int b)
{
	return a < b; //从小到大排序 
}

bool cmp2(const int a, const int b)
{
	return a > b; //从大到小排序 
}

int main()
{
	int arr[15] = {10,1,9,2,8,3,7,4,6,5};//初始化一个样例数组
	int n = 10;//数组元素的个数 
	
	sort(arr, arr + n, cmp1);
	
	for (int i = 0; i < n; i++)
		cout << arr[i] << ' ';
	cout << endl;
	
	sort(arr, arr + n, cmp2);
	for (int i = 0; i < n; i++)
		cout << arr[i] << ' ';
	cout << endl;
	
	return 0;
}

运行结果
在这里插入图片描述

2.2 多级排序

3 小知识

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <bits/stdc++.h> //万能头文件 
using namespace std;

int main(){
	
	//iostream中常用函数示例
	int value1;
	
	cout << "请输入一个整数:" << endl;
	cin >> value1;
	cout << "value1 = " << value1 << endl;
	
	//cstdio文件中常用函数
	int value2;
	double value3;
	
	cout << "请输入一个整数和浮点数(空格隔开):" << endl;
	scanf("%d%lf", &value2, &value3);
	printf("value2 = %d, value3(保留两位小数) = %.2f, value3(默认保留六位小数) = %f\n", value2, value3, value3);
	
	char str[100];
	
	cout << "请输入一个字符串(不包含空格):" << endl;
	scanf("%s", str);
	printf("str = %s\n", str);
		
	//cstring库中的常用函数
	//gets()和puts() 
	char str1[100];
	
	cout << "请输入一个字符串(可以包含空格):" << endl;
	//这个函数的作用是读取一个字符,下面会具体介绍,当你把这条语句注释的话,
	//gets(str1)函数将无法读取字符串,具体原因先不解释,记住如果用gets函数遇到了这种情况,
	//就在gets函数前加一条语句:getchar(); 
	getchar();  
	gets(str1);//输入字符串 
	puts(str1);//输出字符串 
	
	//getchar()
	char ch;
	
	cout << "请输入一个字符:" << endl;
	ch = getchar();
	cout << "ch = " << ch << endl;
	
	//cmath常用函数:sqrt(),abs(),fabs()
	cout << sqrt(123) << endl;
	cout << abs(-123) << endl;
	cout << fabs(-123.342) << endl; 
	
	
	return 0;
}

4 队列和栈的操作

在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值