ACM题解Day9| 2019 ,钱币找零,砍树

在这里插入图片描述

🔥博客介绍`: 27dCnc

🎥系列专栏: <<数据结构与算法>> << 算法入门>> << C++项目>>

🎥 当前专栏: << 算法入门>>

专题 : 数据结构帮助小白快速入门算法
👍👍👍👍👍👍👍👍👍👍👍👍
☆*: .。. o(≧▽≦)o .。.:*☆

❤️感谢大家点赞👍收藏⭐评论✍️

在这里插入图片描述

学习目标:

今日学习打卡
在这里插入图片描述

  • ACM题解

学习时间:

  • 周一至周五晚上 7 点—晚上9点
  • 周六上午 9 点-上午 11 点
  • 周日下午 3 点-下午 6 点

学习内容:

  1. 2019
  2. 钱币找零
  3. 砍树

内容详细:

2019

题目考点: 未知数 数学
在这里插入图片描述
思路
不断循环去找出所有结果,一重循环。
从2020开始遍历X的值,对于每一个X的值,利用等差数列的条件求出Y2的值,只需要判断开方得到的Y是不是整数即可。(可以将Y再次平方,看数值是否发生变化)

代码

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
using unl = __int128_t;
using ll = long long;
using namespace  std;

class Solution {
public: 
     void solve() {
        ll N; 
        while(cin >> N ) {
            ll x = N,y;
            while(1) {
                x++;
                y = sqrt(2 * x * x - N * N);
                if ((x*x - N*N) == (y * y - x * x) && y * y - x * x != 0) break;
            }
            cout << x + y << endl;
        }
    }
};


signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

    #if Run
        int _;cin>>_;while(_--) Solution().solve();
    #else
        Solution().solve();
    #endif

    return 0;
}

钱币找零

题目考点: 贪心

在这里插入图片描述

贪心算法的定义:

贪心算法是指在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,只做出在某种意义上的局部最优解。贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关。

解题的一般步骤是:

1.建立数学模型来描述问题;
2.把求解的问题分成若干个子问题;
3.对每一子问题求解,得到子问题的局部最优解;
4.把子问题的局部最优解合成原来问题的一个解。

详细代码

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100005
using unl = __int128_t;
using ll = long long;
using namespace  std;
int a[6]={1,5,10,20,50,100};
int cnt[6] = {N,N,N,N,N,N};//对应上面的张数

class Solution {
public: // 转换为完全背包问题
     void solve() {
        int money; cin >> money; // 背包容量
        for (int i = 5; i >= 0; i --) { 
            int zhanshu = 0;
            if (money > 0) {
                zhanshu = min(money / a[i],cnt[i]);
                cnt[i] = zhanshu;
            }
            money -= zhanshu * a[i];
        }
        for (int i = 5; i >= 0; i--) {
            if ( cnt[i] > 0&&cnt[i] < N) cout << "需要" << cnt[i] << "张"<< a[i] << "块的" << endl;
        }
    }
};

signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);

#if Run
    int _;cin>>_;while(_--) Solution().solve();
#else
    Solution().solve();
#endif

    return 0;
}

砍树

题目考点: 二分答案
在这里插入图片描述
思路
排序,然后二分在中间找不断的去遍历,然后设置小函数,去判断当前值是否符合条件, 这道题我一开始是暴力,因为当时还没有用二分法,然后直接就超时,后来看算法书发现这道题原来是用二分的。这道题就是找不断通过二分法找k值,然后从存树的数组头到尾计算每棵树能砍多少长度为k的木头。但是要注意的是不能找到一个num == k 就直接 break,因为像样例在找到2.47的时候也符合 num == k,所以一定要把条件设置为 (right-left)>0.002。0.002其实题中已经给你提示了。

详细代码

#include<bits/stdc++.h>
#define Run 0
#define endl "\n"
#define N 100000005
using unl = __int128_t;
using ll = long long;
using namespace  std;
ll a[N + 5];

class Solution {
public: //运用二分算法
    void slove() {
        ll n,m; cin >> n >> m;
        ll r = LLONG_MIN,l = 0,mid;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            r = max(r,a[i]);
        } //输入木材并找到木材最大值
        while(l < r) {
            mid = (l + r + 1) / 2;
            if (ltm(mid,n,m)) l = mid;
            else r = mid - 1;
        } //二分锯片高度
        cout << l << endl;
    }
    bool ltm(ll x,ll n,ll m) {
        ll sum = 0;
        for (int i = 1; i <= n; i++) if (a[i] > x) sum = sum + a[i] - x; //计算木材
        if(sum >= m) return true;
        return false;
    }
};

signed main() {

    cin.tie(0) -> ios::sync_with_stdio(0);
    cout.tie(0) -> ios::sync_with_stdio(0);
#if Run
    int _;cin>>_;while(_--) Solution().slove();
#else
    Solution().slove();
#endif
    return 0;
}

学习产出:

  • 技术笔记 2 遍
  • CSDN 技术博客 3 篇
  • 习的 vlog 视频 1 个

在这里插入图片描述

重磅消息:

GTP - 4 最新版接入服务他来了 点击链接即可查看详细

GTP - 4 搭建教程

🔥如果此文对你有帮助的话,欢迎💗关注、👍点赞、⭐收藏、✍️评论,支持一下博主~

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值