蓝桥杯第八届c++大学B组详解

2024-03-31 16:52

本文主要是介绍蓝桥杯第八届c++大学B组详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1.购物单

2.等差素数列

3.承压计算

4.方格分割

5.日期问题

6.包子凑数

7.全球变暖

8.k倍区间


1.购物单

 题目解析:就是将折扣字符串转化为数字,进行相加求和。

 

#include<iostream>
#include<string>
#include<cmath>
using namespace std;int main()
{double sum = 0.0;while(1){double price;string ignore;string discount;double dis = 0.0;cin >> price >> ignore >> discount;if(ignore == "0") break;if(discount == "半价") discount = "5折";//截取字符discount = discount.substr(0, discount.size() - 2);for(int i = 0; i < discount.size(); i++){dis += (discount[i] - '0') * pow(10, -i - 1);}sum += dis * price;}//面额刚好100整除if((int)sum % 100 == 0)cout << (int)sum << endl;else{cout << (int)sum / 100 * 100 + 100 << endl;}return 0;
}

 这题目数据太长了,输入很麻烦。那么就不讲武德。因为上面代码没错但是运行超级麻烦。这种简单题目直接不讲武德。

#include <iostream>
using namespace std;
int main()
{// 请在此输入您的代码double sum = 180.90*0.88+10.25*0.65+56.14*0.9+104.65*0.9+100.30*0.88+297.15*0.5+26.75*0.65+130.62*0.5+240.28*0.58+270.62*0.8+115.87*0.88+247.34*0.95+73.21*0.9+101.00*0.5+79.54*0.5+278.44*0.7+199.26*0.5+12.97*0.9+166.30*0.78+125.50*0.58+84.98*0.9+113.35*0.68+166.57*0.5+42.56*0.9+81.90*0.95+131.78*0.8+255.89*0.78+109.17*0.9+146.69*0.68+139.33*0.65+141.16*0.78+154.74*0.8+59.42*0.8+85.44*0.68+293.70*0.88+261.79*0.65+11.30*0.88+268.27*0.58+128.29*0.88+251.03*0.8+208.39*0.75+128.88*0.75+62.06*0.9+225.87*0.75+12.89*0.75+34.28*0.75+62.16*0.58+129.12*0.5+218.37*0.5+289.69*0.8;if((int)sum % 100 == 0)cout << (int)sum;else{int a = (int)(sum+100)/100*100;cout << a << endl;}return 0;
}

2.等差素数列

 首先将素数全部放到数组当中,然后再用set进行查看是否是素数,f函数用首先确定首项,在确定公差,最后判断是否每项是否是素数,看在set中可不可以找到。

#include <iostream>
#include<vector>
#include<set>
using namespace std;vector<int> a(5000);
set<int> g;bool isprime(int x)
{for(int i = 2; i < x / 2; i++){if(x % i == 0)return false;}return true;
}int f(vector<int> a, int n)
{for(int i = 0; i < n; i++)//算首项{int first = a[i];for(int d = 1; d < a[n-1] - first; d++){int m = first;for(int j = 1; j < 10; j++){m += d;//求和不是素数if(g.find(m) == g.end())break;if(j == 9)return d;}}}
}int main()
{a[0] = 2, a[1] = 3;g.insert(2);g.insert(3);int index = 2;int t = 5;while(index < 5000){//将2-5000的素数都放到set;if(isprime(t)){a[index++] = t;g.insert(t); }t++;}//f返回最小公差;cout << f(a, 5000) << endl;return 0;
}

3.承压计算

题目解析:这个题目看懂题目意思就已经够烦了,就是将上面的数平均分给下面的数,因为最小重量是2086458231,这个垃圾数字处理。

#include <iostream>
#include<algorithm>
using namespace std;long long a[30][30];int main()
{long long factor = 1;//2的30次方。是为了避免小数。for(int i = 0; i < 30; i++){factor = factor * 2;}for(int i = 0; i < 29; i++){for(int j = 0; j <= i; j++){long long m = 0;cin >> m;a[i][j] = m * factor;} }for(int i = 0; i < 29; i++){for(int j = 0; j <= i; j++){long long half = a[i][j] / 2;a[i + 1][j] += half;a[i + 1][j + 1] += half;}}//最后一行排序sort(a[29], a[29] + 30);cout  << a[29][29] / 2 << endl;return 0;
}

4.方格分割

 题目解析:本质是递归回溯深搜算法,找规律,是不是方格都是中心对称的,那么递归起点就在中心点,进行上下左右的排查,最后还要回溯,还有因为题目要求是中心对称也是一种方法,所以方法数最后要除以4.

#include <iostream>
using namespace std;//上下左右方位
int d[][2]  = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int vis[7][7];
int ret = 0;void dfs(int x, int y)
{//递归出口if(x == 0 || x == 6 || y == 0 || y == 6){ret++;return;}vis[x][y] = 1;vis[6 - x][6 - y] = 1;//上下左右for(int i = 0; i < 4; i++){int nx = x + d[i][0];int ny = y + d[i][1];//边界处理if(nx > 6 || nx < 0 || ny > 6 || ny < 0)continue;if(!vis[nx][ny]){dfs(nx, ny);}}//回溯vis[x][y] = 0;vis[6 - x][6 - y] = 0;
}int main()
{dfs(3, 3);//因为旋转对称是同一种分隔方式cout << ret / 4 << endl;return 0;
}

5.日期问题

 题目解析: 细节很多,逻辑很缜密,自己代码走读一下,就可以找到细节。

#include <iostream>
#include <string>
#include<sstream>
#include<set>
using namespace std;bool isleap(int a)
{if((a % 4 == 0 || a % 100 != 0) && a % 400 == 0)return true;elsereturn false;
}void is(int n, string& s)
{stringstream ss;ss << n;ss >> s;
}string f(int year, int month, int day)
{if(year >= 0 && year <= 59) year += 2000;if(year >= 60 && year <= 99) year += 1900;if(month < 1 || month > 13) return " ";if(day < 1 || day > 31) return " ";//计算闰年bool _isleap = isleap(year);switch(month){//考虑闰月case 2:if(_isleap && day > 29) return " ";if(!_isleap && day > 28) return " ";break;case 4:if(day > 30)  return " ";break;case 6:if(day > 30)  return " ";break;case 9:if(day > 30)  return " ";break;case 11:if(day > 30)  return " ";break;default:break;}//将数字变会字符串string _a, _b, _c;is(year, _a);is(month, _b);is(day, _c);//处理前导0;if(_b.size() == 1)  _b = '0' + _b;if(_c.size() == 1)  _c = '0' + _c;return _a + "-" + _b + "-" + _c;
}int main()
{string in;cin >> in;//将字符串转化为数字;int a, b, c;a = (in[0] - '0') * 10 + (in[1] - '0') ;b = (in[3] - '0') * 10 + (in[4] - '0') ;c = (in[6] - '0') * 10 + (in[7] - '0') ;//年月日string case1 = f(a, b, c);//月日年string case2 = f(b, c, a);//日月年string case3 = f(c, b, a);set<string> ans;if(case1 != " ") ans.insert(case1);if(case2 != " ") ans.insert(case2);if(case3 != " ") ans.insert(case3);for(set<string>::iterator it = ans.begin(); it != ans.end(); it++){cout << *it << endl;}return 0;
}

 6.包子凑数

 题目解析:就是找钱的变形,使用动态规划。

#include <iostream>
using namespace std;//dp[i]表示凑不到i种蒸笼凑不齐的方案数
int main()
{int n;cin >> n;int dp[20000] = {0};int a[101];for(int i = 0; i < n; i++){cin >> a[i];}dp[0] = 1;for(int i = 0; i < n; i++){for(int j = 0; j < 10001; j++)//10001是最大凑包子数目;{if(dp[j]){dp[j + a[i]] = 1; //dp[4] = 1; dp[5] = 1}}}int flag = 0, num = 0;for(int i = a[n-1]; i < 10001; i++){if(dp[i] == 1)num++;if(dp[i] == 0)num = 0;if(num == a[0]){flag = 1;break;}}if(flag == 0)cout << "INF" << endl;else{num = 0;for(int i = 0; i < 10001; i++){if(dp[i] == 0)num++;}cout << num << endl;}return 0;
}

7.全球变暖

 题目解析:递归深搜fullfill问题,

#include <iostream>
using namespace std;#define N 1000
int ret = 0;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char IslandSea[N][N];void dfs(char IslandSea[N][N], int x, int y)
{for(int k = 0; k < 4; k++){int xx = dx[k] + x;int yy = dy[k] + y;if(xx >= 0 && xx < 7 && yy >= 0 && yy < 7){if(IslandSea[xx][yy] == '.')return;    }}ret++;
}int main()
{int n;cin >> n;for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){cin >> IslandSea[i][j];}}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){if(IslandSea[i][j] == '#'){dfs(IslandSea, i, j);}}}cout << ret << endl;return 0;
}

8.k倍区间

 题目解析:读清楚题目,本质就是前缀和。cnt用来记录余数个数,将任意两个相同的余数进行组合一定会得到一种区间。

 

#include <iostream>
using namespace std;
#include<map>map<int, int> cnt;//存放余数
int a[100000];
int dp[100000];//前缀和int main()
{int n, k;long long count = 0;cin >> n >> k;dp[0] = 0;cnt[0] = 1;for(int i = 1; i <= n; i++){cin >> a[i];dp[i] = (dp[i - 1] + a[i] ) % k;//k的倍数cnt[dp[i]]++;//记录一下。cnt[0]为刚好被k整除的个数。}for(int i = 0; i < k; i++){count += (long long) cnt[i] * (cnt[i] - 1) / 2;}cout << count << endl;return 0;
}

这篇关于蓝桥杯第八届c++大学B组详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/864829

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Java中ArrayList与顺序表示例详解

《Java中ArrayList与顺序表示例详解》顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构,:本文主要介绍Java中ArrayList与... 目录前言一、Java集合框架核心接口与分类ArrayList二、顺序表数据结构中的顺序表三、常用代码手动

JAVA线程的周期及调度机制详解

《JAVA线程的周期及调度机制详解》Java线程的生命周期包括NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING和TERMINATED,线程调度依赖操作系统,采用抢占... 目录Java线程的生命周期线程状态转换示例代码JAVA线程调度机制优先级设置示例注意事项JAVA线程

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param