SRM 219 Div II Level One: WaiterTipping,小心约分
来源:互联网 发布:淘宝差评处理技巧 编辑:程序博客网 时间:2024/04/29 02:20
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503
这题目看上去so easy, 但写的时候要特别小心,如果直接按照公式算,没有加下面这一句的话:
if (total + total * taxPercent / 100 + (tip + 1) * total / 100 <= money) {++tip;}
那么因为公式涉及向下约分的运算,那么所得到的tip的值可能是比最大值小1的值。一定要加上这一句进行判定。
当然还有一种解法就是用循环,tip值从money 到1,当满足公式时,则此时tip的值就是解,这种方法效率低,
但不容易出错!
代码如下:
#include <iostream>using namespace std;class WaiterTipping{public:int maxPercent(int total, int taxPercent, int money);};int WaiterTipping::maxPercent(int total, int taxPercent, int money){int res = money - ( total + total * taxPercent / 100 );int tip;if (res >= 0) {tip = res * 100 / total;/* 关键不能少,上面得到的tip可能不是最大值,而是比最大值小1的值 */if (total + total * taxPercent / 100 + (tip + 1) * total / 100 <= money) {++tip;}return tip;}return -1;}
- SRM 219 Div II Level One: WaiterTipping,小心约分
- SRM 207 Div II Level One: TransportCounting
- SRM 219 Div II Level Two: HealthFood
- SRM 208 DIV 1 Level One
- SRM 222 Div II Level One: TextCompressor,求最大重复子串问题
- SRM 582 Div II Level Two SpaceWarDiv2
- SRM 233 Div II Level Two: PipeCuts
- SRM 299 Div II Level Two: Projections
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 585 Div II Level Two: TrafficCongestionDivTwo
- SRM 585 Div II Level Three: EnclosingTriangleColorful
- SRM 579 Div II Level Two: UndoHistory
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 212 Div II Level Three: LargestCircle,Brute Force
- SRM 207 Div II Level Three: CaptureThemAll,BFS算法
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 584 Div Ii Level Three: Excavations2, Dynamic Programming
- List去除重复
- MySQL中SQL调优三语句
- VMware vSphere 服务器虚拟化之二十七 桌面虚拟化之View中使用Thinapp软件虚拟化
- Ubuntu12.04TLS安装全过程
- struts结合uploadify.js--------------补:单文件,表单数据量少的时候上传
- SRM 219 Div II Level One: WaiterTipping,小心约分
- jdbc详述
- linux-C编程-进程间通信
- 在Windows下学习chromium
- string::size_type
- 策略模式总结
- OpenCV形态学相关函数的使用测试。
- MFC 如何显示时间星期月份
- 测试解析class常量池