总结小知识点(蓝桥杯)

文章介绍了如何运用编程中的取整取模策略和函数来优化饮料换购的问题,提到了C++中的暴力算法和优先队列在解决问题中的应用,并探讨了回文判断和字符串转换的技巧。通过将复杂问题分解为多个函数,简化了代码理解和执行效率。
摘要由CSDN通过智能技术生成

饮料换购,合理运用取整取模,设置条件直接解出答案。

优化:

每次喝完三瓶我就多喝一瓶,思路简洁

对于题目非常复杂时,可以分成很多个函数,把每一部分解决就会容易很多,简化一时间的思考负担

同时重复使用的也可以用函数代替

含 2 天数

暴力算法

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int days[] = {
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
//直接将日期插入在数组中,舍去了大量循环和if的判断
//开始的0是同步下标

bool is_leap(int x)
{
    return x % 400 == 0 || x % 4 == 0 && x % 100 != 0;
//返回真值就相当于返回1
}

int daysOfMonth(int y, int m)
{
    if (m == 2)
        return days[m] + is_leap(y);
        //二月特判闰年
    return days[m];
}

bool check(int x, int y, int z)
{
    int a[3] = {x, y, z};
    for (int i = 0; i < 3; ++ i )
        while (a[i])//当a[i]还存在的时候就继续分割判断
        {
           if (a[i] % 10 == 2)
                return true;
            else
                a[i] /= 10;
        }
           
    return false;
}

int main()
{
    int res = 0;
    for (int i = 1900; i <= 9999; ++ i )
        for (int j = 1; j <= 12; ++ j )
            for (int k = 1; k <= daysOfMonth(i, j); ++ k )
            //日期直接放在数组里,更简洁
            //i是看是否闰年,准确就是传入月份,对应的月份就对应数组中的日期即可
                res += check(i, j, k);//符合条件的就加一
                //上传年月日,逐个切割看有无2
    cout << res << endl;
    return 0;
}

使用函数会使得程序更清晰和简洁

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

bool is_leap(int y)
{
    return y % 400 == 0 || y % 4 == 0 && y % 100 != 0;
}

bool check(int x)
{
    while (x)
        if (x % 10 == 2)
            return true;
        else
            x /= 10;
    return false;
}

int main()
{
    int res = 0;
    for (int i = 1900; i <= 9999; ++ i )
        if (check(i))
            res += 365 + is_leap(i);
        else
            res += 179 + is_leap(i);
    cout << res << endl;
    return 0;
}

谈判

C语言不好解,用c++对列好解决

必须从小的开始叠加,才是用钱最少的方法

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    
    priority_queue<int, vector<int>, greater<int>> heap;
//优先对列名字      类型    多个变量    最后的大小
//如less就是倒序,也就是最后是最小的,而greater是更大的,最后最大
//这是最小值优先队列
    while (n -- )
    {
        int x;
        cin >> x;
        heap.push(x);
    }
    
    int res = 0;
    while (heap.size() > 1)
    {
        auto a = heap.top();
        //auto为自动判断类型
        heap.pop();
        auto b = heap.top();
        heap.pop();
        res += a + b;
        heap.push(a + b);
    }
    
    cout << res << endl;
    
    return 0;
} 

补充

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{int n;
  cin>>n;
  priority_queue<int,vector<int>,greater<int>> heap;

while(n--)
{
  int x;
  cin>>x;
  heap.push(x);
}
  int res=0;
  while((heap.size())>1)
  //前面要有()!
  //()中是给你定位的,不写就默认从第一个开始
  {
    auto a=heap.top();
    heap.pop();
    auto b=heap.top();
    heap.pop();//默认从首开始出队
    res+=a+b;
//记得插入a+b合在一起入队的结果!
heap.push(a+b);
//默认插入也是插在队尾
  }

  cout<<res<<endl;



  return 0;
}

取模不受影响是因为数只会向前进位,不会变后面的4位,因此只用保留后4位让前面的一直更新就行了

,我们只需要最新的后四位

to_string可以将整型数字转化为字符串

reverse翻转函数,两个位置是框定开始的字符位置和结束的位置

return s==str如果翻转后相同说明是回文返回该值,如果不是就不返回.

c++直接可以将整数转化为字符串,根本就不需要自己取整取模拆,十分便捷

判断ab回文

填空题只要让系统输入,就会自己跳出答案,试试吧不会做试试看能不能用能用就赚了

  int a;
  cin>>a;
  cout<<a<<endl;

这种方法只有填空题才会有,大题是没有的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值