SRM 219 Div II Level One: WaiterTipping,小心约分
2013-07-10 10:51
429 查看
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503
这题目看上去so easy, 但写的时候要特别小心,如果直接按照公式算,没有加下面这一句的话:
那么因为公式涉及向下约分的运算,那么所得到的tip的值可能是比最大值小1的值。一定要加上这一句进行判定。
当然还有一种解法就是用循环,tip值从money 到1,当满足公式时,则此时tip的值就是解,这种方法效率低,
但不容易出错!
代码如下:
这题目看上去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 219 Div II Level Two: HealthFood
- SRM 212 Div II Level One: YahtzeeScore
- SRM 222 Div II Level One: TextCompressor,求最大重复子串问题
- SRM 583 Div II Level One:SwappingDigits
- SRM 582 Div II Level One: SemiPerfectSquare
- SRM 207 Div II Level One: TransportCounting
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序( 比较函数 )
- SRM 222 Div II Level Two: GroceryBagger,STL map 用法
- SRM 207 Div II Level Three: CaptureThemAll,BFS算法
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 212 Div II Level Three: LargestCircle,Brute Force
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 578 Div II Level Two: GooseInZooDivTwo, DFS
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- SRM 585 Div II Level Two: TrafficCongestionDivTwo