2017第八届蓝桥杯C/C++ B组省赛题解

前言:

毕业前最后一次蹭一次公费旅游了。以前都是在成都,这次在绵阳,至少不用早起了。应该是最后一次玩蓝桥杯了。



尊重原创,转载请注明出处:http://blog.csdn.net/y1196645376/article/details/69718192


第一题

标题: 购物单

小明刚刚找到工作,老板人很好,只是老板夫人很爱购物。老板忙的时候经常让小明帮忙到商场代为购物。小明很厌烦,但又不好推辞。

这不,XX大促销又来了!老板夫人开出了长长的购物单,都是有打折优惠的。
小明也有个怪癖,不到万不得已,从不刷卡,直接现金搞定。
现在小明很心烦,请你帮他计算一下,需要从取款机上取多少现金,才能搞定这次购物。

取款机只能提供100元面额的纸币。小明想尽可能少取些现金,够用就行了。
你的任务是计算出,小明最少需要取多少现金。
以下是让人头疼的购物单,为了保护隐私,物品名称被隐藏了。
--------------------
****     180.90       88折
****      10.25       65折
****      56.14        9折
****     104.65        9折
****     100.30       88折
****     297.15       半价
****      26.75       65折
****     130.62       半价
****     240.28       58折
****     270.62        8折
****     115.87       88折
****     247.34       95折
****      73.21        9折
****     101.00       半价
****      79.54       半价
****     278.44        7折
****     199.26       半价
****      12.97        9折
****     166.30       78折
****     125.50       58折
****      84.98        9折
****     113.35       68折
****     166.57       半价
****      42.56        9折
****      81.90       95折
****     131.78        8折
****     255.89       78折
****     109.17        9折
****     146.69       68折
****     139.33       65折
****     141.16       78折
****     154.74        8折
****      59.42        8折
****      85.44       68折
****     293.70       88折
****     261.79       65折
****      11.30       88折
****     268.27       58折
****     128.29       88折
****     251.03        8折
****     208.39       75折
****     128.88       75折
****      62.06        9折
****     225.87       75折
****      12.89       75折
****      34.28       75折
****      62.16       58折
****     129.12       半价
****     218.37       半价
****     289.69       8折
--------------------

需要说明的是,88折指的是按标价的88%计算,而8折是按80%计算,余者类推。
特别地,半价是按50%计算。

请提交小明要从取款机上提取的金额,单位是元。
答案是一个整数,类似4300的样子,结尾必然是00,不要填写任何多余的内容。


特别提醒:不许携带计算器入场,也不能打开手机。

讲真,一来就看到这种题目,这种蓝桥杯我内心是拒绝的。

做法:将清单复制到txt文本里面,利用Ctrl+H替换掉**这些字符和折扣。预处理好数据之后用代码计算即可!

答案:5200

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;

int main()
{
    freopen("DATA.txt","r",stdin);
    double ans = 0,a,b;
    char buf[1110];
    while(scanf("%s%lf%lf",buf,&a,&b)!=EOF){
        ans += a*b/100;
    }
    printf("%lf\n",ans);
    return 0;
}
//5136.859500
//5200


第二题

标题:等差素数列

2,3,5,7,11,13,....是素数序列。
类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列。
上边的数列公差为30,长度为6。

2004年,格林与华人陶哲轩合作证明了:存在任意长度的素数等差数列。
这是数论领域一项惊人的成果!

有这一理论为基础,请你借助手中的计算机,满怀信心地搜索:

长度为10的等差素数列,其公差最小值是多少?

注意:需要提交的是一个整数,不要填写任何多余的内容和说明文字。

做法:用线性素数筛把10^6内的素数筛出来,然后从小到大枚举公差然后去验证。

答案:210

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
const long long  N = 1000010;
int dp[N]={
  1,1,0};
int prim[N],tot = 0;
void init()
{
    for(long long i = 2 ; i < N ; i ++)
    {
        if(dp[i])continue;
        prim[tot++]=i;
        for(long long  j = i ; j * i < N ; j ++){
            dp[i*j] = 1;
        }
    }
}
int main()
{
    init();
    printf("%d\n",tot);
    for(int i = 1 ; i*10 < N ; i ++){
        for(int j = 0 ; j < tot ; j ++){
            int flag = 1,temp = prim[j]; 
            for(int k = 1 ; k < 10 ; k ++)
            {
                if(temp + i >= N || dp[temp + i] == 1){
                    flag = 0;break;
                }else{
                    temp = temp + i;
                }
            }
            if(flag == 1
### 第十三届蓝桥杯 C++ B 题目及题解 #### 试题 A: 空间 对于空间问题,通常涉及计算几何或简单的数学运算。具体到此题,可能涉及到三维坐标系中的距离计算或其他基本的空间关系处理。 ```cpp #include <iostream> using namespace std; int main() { double x1, y1, z1; double x2, y2, z2; cin >> x1 >> y1 >> z1; cin >> x2 >> y2 >> z2; double distance = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1)); cout << fixed << setprecision(2) << distance << endl; return 0; } ``` [^1] #### 试题 B: 卡片 卡片问题一般考察的是合数学的知识点或者是字符串操作技巧。这类题目往往需要理解排列合原理以及如何高效地遍历所有可能性来找到最优解法。 ```cpp #include <bits/stdc++.h> using namespace std; string cards[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"}; vector<string> deck(cards, end(cards)); void shuffleDeck(vector<string>& d){ random_shuffle(d.begin(),d.end()); } int main(){ srand(time(NULL)); // 初始化随机数种子 do{ shuffleDeck(deck); for(auto card : deck) cout<<card<<" "; cout<<"\n"; }while(getchar()!=&#39;q&#39;); return 0; } ``` [^2] #### 试题 C: 直线 直线问题是关于解析几何的基础应用之一,比如求两条直线交点、判断两直线平行与否等问题。解决此类问题的关键在于掌握好斜率的概念及其相关性质的应用方法。 ```cpp struct Line { double a,b,c; // ax+by+c=0 形式的系数表示一条直线 }; bool isParallel(Line l1,Line l2){ return abs(l1.a*l2.b-l1.b*l2.a)<EPSILON; } pair<double,double> intersectionPoint(Line l1,Line l2){ double det=l1.a*l2.b-l1.b*l2.a; if(abs(det)>EPSILON){ // 不平行则有唯一交点 double px=(l2.c*l1.b-l1.c*l2.b)/det; double py=(l1.c*l2.a-l2.c*l1.a)/det; return make_pair(px,-py); // 注意这里返回的纵坐标取负号是因为我们定义方程时c项前面带了个减号 } throw runtime_error("Lines are parallel or coincident"); } ``` [^3] #### 试题 D: 货物摆放 货物摆放示例展示了动态规划算法的实际应用场景。通过构建状态转移表并逐步填充表格中的值直到最终得到全局最优解的过程体现了该类问题的核心思想——分治策略下的最优化选择。 ```cpp const int MAXN=1e5+5; long long dp[MAXN],w[MAXN]; for(int i=1;i<=n;++i){ for(int j=m;j>=v[i];--j){ dp[j]=max(dp[j],dp[j-v[i]]+w[i]); } } cout<<dp[m]<<endl; ``` #### 试题 E: 路径 路径寻找属于图论范畴内的经典难题,无论是广度优先搜索还是深度优先搜索都能很好地解决问题;而当引入权重概念之后,则可以考虑采用Dijkstra算法或是Floyd-Warshall等更高级别的解决方案来进行分析解答。 ```cpp // 使用队列实现BFS查找最短路径长度 queue<int> q; memset(dist,INF,sizeof dist); dist[s]=0;q.push(s); while(!q.empty()){ int u=q.front();q.pop(); for(int v:g[u]){ if(dist[v]==INF){ dist[v]=dist[u]+1; pre[v]=u; q.push(v); } } } if(dist[t]!=INF){ vector<int> path; for(int cur=t;cur!=-1;cur=pre[cur]) path.push_back(cur); reverse(path.begin(),path.end()); printf("%d\n%d\n",dist[t],path.size()-1); for(size_t i=0;i<path.size();++i) printf(i==path.size()-1?"%d\n":"%d ",path[i]); }else puts("-1"); // 如果无法到达终点就输出-1 ``` #### 试题 F: 时间显示 时间转换是一个非常基础但也容易出错的小知识点,尤其是在不同单位之间的相互转化上要特别小心精度丢失的情况发生。下面给出了一种较为通用的时间格式化函数模板供参考学习之用。 ```cpp stringstream ss; ss<<setfill(&#39;0&#39;)<<setw(2)<<hour<<&#39;:&#39;<<setw(2)<<minute<<&#39;:&#39;<<setw(2)<<second; return ss.str(); ``` #### 试题 G: 砝码称重 砝码称重问题可以通过贪心算法快速得出结论。每次选取当前可用的最大重量作为本次测量的标准,这样既能保证准确性又能减少不必要的复杂度提升效率。 ```cpp sort(weights.rbegin(),weights.rend()); // 对砝码按降序排序 double total_weight=accumulate(begin(weights),end(weights),(double)0.0); double current_sum=0.0; int count=0; for(double w:weights){ if(current_sum+w<=total_weight/2.0){ ++count; current_sum+=w; }else break; } printf("%.lf%%\n",(current_sum/(total_weight/2))*100); ```
评论 168
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值