ACM题解Day9| 2019 ,钱币找零,砍树
🔥博客介绍`: 27dCnc
🎥系列专栏: <<数据结构与算法>> << 算法入门>> << C++项目>>
🎥 当前专栏: << 算法入门>>
专题 : 数据结构帮助小白快速入门算法
👍👍👍👍👍👍👍👍👍👍👍👍
☆*: .。. o(≧▽≦)o .。.:*☆
❤️感谢大家点赞👍收藏⭐评论✍️
学习目标:
今日学习打卡
- ACM题解
学习时间:
- 周一至周五晚上 7 点—晚上9点
- 周六上午 9 点-上午 11 点
- 周日下午 3 点-下午 6 点
学习内容:
- 2019
- 钱币找零
- 砍树
内容详细:
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 最新版接入服务他来了 点击链接即可查看详细
🔥如果此文对你有帮助的话,欢迎💗关注、👍点赞、⭐收藏、✍️评论,支持一下博主~
本文作者:2c237c6
本文链接:https://www.cnblogs.com/27dCnc/p/18568627
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步