算法练习4

* 题目*:

A man has a rather old car being worth 2000. He saw a secondhand car being worth 8000. He wants to keep his old car until he can buy the secondhand one.
He thinks he can save $1000 each month but the prices of his old car and of the new one decrease of 1.5 percent per month. Furthermore the percent of loss increases by a fixed 0.5 percent at the end of every two months.
Example of percents lost per month:
If, for example, at the end of first month the percent of loss is 1, end of second month percent of loss is 1.5, end of third month still 1.5, end of 4th month 2 and so on …
Can you help him? Our man finds it difficult to make all these calculations.
How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?

typedef struct 和struct :

typedef struct Student{int a;}Stu;

在c++里面, typedef是别名的意思,Stu是Student的别名,定义 Stu test 即可

struct   Student  {  int   a;  }stu1;//stu1是一个变量  typedef   struct   Student2  {  int   a;  }stu2;//stu2是一个结构体类型=struct Student 

vector应用:

(1)头文件#include.

(2)创建vector对象,vector vec;

(3)尾部插入数字:vec.push_back(a);

(4)使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的。
(5)使用迭代器访问元素.

vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)cout<<*it<<endl;

(6)插入元素: vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;
(7)删除元素: vec.erase(vec.begin()+2);删除第3个元素vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
(8)向量大小:vec.size();
(9)清空:vec.clear();

取整函数:

需要头文件#include<cmath>
1. ceil() 向上取整
2. floor()向下取整
3. round()四舍五入

代码:

#include<vector>
#include<iostream>using namespace std;class BuyCar
{public:static std::vector<int> nbMonths(int startPriceOld, int startPriceNew, int savingperMonth, double percentLossByMonth){int waiteMonth = 1;int savingSum = 0;double nowPriceNew = startPriceNew;double nowPriceOld=startPriceOld;while (1)//run of time{if (waiteMonth%2==0)//increasing 0.5 percent per two months {percentLossByMonth += 0.5;//cout << "percentLossByMonth=" << percentLossByMonth << endl;}if ((savingSum + nowPriceOld) >= nowPriceNew){int leaveOut =round( savingSum + nowPriceOld - nowPriceNew);return   {waiteMonth-1, leaveOut };}waiteMonth++;// a month is gonenowPriceOld -= nowPriceOld*percentLossByMonth*0.01;nowPriceNew -= nowPriceNew*percentLossByMonth*0.01;savingSum += savingperMonth;}};
};
int main()
{vector<int> test = BuyCar::nbMonths(12000, 8000, 1000, 1.5);cout << test[0]<<"  "<<test[1] << endl;}

算法练习4:Buying a car相关推荐

  1. 朴素贝叶斯算法的python实现

    朴素贝叶斯 算法优缺点 优点:在数据较少的情况下依然有效,可以处理多类别问题 缺点:对输入数据的准备方式敏感 适用数据类型:标称型数据 算法思想: 朴素贝叶斯 比如我们想判断一个邮件是不是垃圾邮件,那 ...

  2. 【数据科学系统学习】机器学习算法 # 西瓜书学习记录 [6] 朴素贝叶斯实践

    本篇内容为<机器学习实战>第 4 章 基于概率论的分类方法:朴素贝叶斯程序清单.所用代码为 python3. 朴素贝叶斯 优点:在数据较少的情况下仍然有效,可以处理多类别问题. 缺点:对于 ...

  3. Python实现朴素贝叶斯算法

    Python实现朴素贝叶斯算法 1.算法介绍 朴素贝叶斯假设特征相互独立,于是有下面的公式: x表示特征向量,c表示类别. 原理: 通过数据集计算出公式中右边分子中的各个概率.预测时根据公式计算出该样 ...

  4. 五大经典算法之动态规划

    一.概念起源   动态规划,又名DP算法(取自其Dynamic Programming的缩写),最初是运筹学的一个分支,是用来求解决策过程最优化的数学方法. 二.基本思想   把 多阶段过程 转化为一 ...

  5. 机器学习 多变量回归算法_如何为机器学习监督算法识别正确的自变量?

    机器学习 多变量回归算法 There is a very famous acronym GIGO in the field of computer science which I have learn ...

  6. 基础算法 —— 模拟思维

    [概述] 模拟,是根据实际问题建立模型,模拟实际按程序走一遍,最终求出答案. 思维,则是与逻辑思维有关,其需要针对题意.数据范围等抽丝剥茧抽离出有用的信息,从而得出一个结果. 对于一般的模拟,直接根据 ...

  7. [机器学习-实践篇]贝叶斯算法

    一.贝叶斯公式 贝叶斯定理由英国数学家贝叶斯 ( Thomas Bayes 1702-1761 ) 发展,用来描述两个条件概率之间的关系,比如 P(A|B) 和 P(B|A).主要用于文本分类. 1. ...

  8. 用python实现朴素贝叶斯算法_朴素贝叶斯算法 python 实现

    应用贝叶斯准则: 使用上面这些定义,可以定义贝叶斯分类准则为: 如果 P(c1|x, y) > P(c2|x, y), 那么属于类别 c1; 如果 P(c2|x, y) > P(c1|x, ...

  9. TF-IDF算法-golang实现

    1.TF-IDF算法介绍 TF-IDF(term frequency–inverse document frequency,词频-逆向文件频率)是一种用于信息检索(information retrie ...

  10. 机器学习_贝叶斯算法

    朴素贝叶斯 一.概述 1. 条件概率公式 2. 贝叶斯推断 3. 嫁?还是不嫁?这是一个问题-- 二.朴素贝叶斯种类 1. GaussianNB 2. MultinomialNB 3. Bernoul ...

最新文章

  1. React多页面应用脚手架-v1.3.0
  2. python读取excel表格-python读写excel文件
  3. ACM模板--邻接矩阵 无向图 Prim Kruskal Dijkstra
  4. Jquery通过ajax请求NodeJS返回json数据
  5. Bootstrap(自助法),Bagging,Boosting(提升)
  6. [C#.NET 拾遗补漏]06:单例模式最佳实践
  7. linux中fdisk的参数,Linux fdisk命令参数及用法详解--Linux磁盘分区管理命令fdisk
  8. scala 去除重复元素_Scala程序从列表中删除重复项
  9. Android图片编码机制深度解析(Bitmap,Skia,libJpeg)
  10. Python学习笔记之列表(一)
  11. Wine 4.4 发布,Windows 应用的兼容层
  12. mysql单机热备份_mysql数据库热备份
  13. 数据库设计需要注意什么
  14. 潜安 | 分享最新最前沿的IT教程
  15. 如何用键盘快捷键打开 macOS 控制中心?
  16. win7发现不了无线网络_Win7笔记本怎么设置wifi热点 Win7笔记本设置wifi热点方法【详解】...
  17. 我是如何从大三开始实现财务自由的
  18. PHPExcel 插件使用详解
  19. 腾讯笔试题编程题——纸牌游戏
  20. 英语报纸计算机类,报纸和电脑英语作文

热门文章

  1. 汇编指令初步(ATT格式)
  2. Groovy学习(二):GDK初探
  3. uniapp 打印小票
  4. 无向有权图的邻接矩阵实现(C++)
  5. WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
  6. notepad拼心形_Excel心形函数v1.0 免费版
  7. pvs-stdio ue4_PlatformIO中的PVS-Studio集成
  8. 星空粒子登录页面 jsp
  9. 面向对象系列(三)-抽象类的特点
  10. (四)git储藏与清理