题目来源: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,小心约分的更多相关文章

  1. SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...

  2. SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...

  3. SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2

    题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...

  4. SRM 582 Div II Level One: SemiPerfectSquare

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...

  5. SRM 582 Div II Level Two SpaceWarDiv2

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...

  6. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  7. SRM 583 Div II Level One:SwappingDigits

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...

  8. SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...

  9. SRM 212 Div II Level One: YahtzeeScore

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...

随机推荐

  1. HDU2795 Billboard 【线段树】+【单点更新】

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. Maven 实现Struts2注解配置步骤详解

    1,pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  3. 使用JDK自带的工具将中文转换为ascii码

    有时候在MyEclipse中,文件只能保存为“ISO-8859-1”的类型,而这种类型的文件时无法保存中文数据的,那么我们只能将中文数据经过Unicode编码才能往文件中保存,这里可以使用JDK自带的 ...

  4. [SVN] 分支同步、合入主干操作分享

    冲突的解决原则 不是自己修改的地方就使用主干的. 需要特别注意的是: 分支同步主干时,远端(theirs)是主干,本地(mine/working)的是分支: 分支合入主干时,本地(mine/worki ...

  5. MFC消息响应机制分析

    ---- 摘要: ---- MFC是Windows下程序设计的最流行的一个类库,但是该类库比较庞杂,尤其是它的消息映射机制,更是涉及到很多低层的东西,我们在这里,对它的整个消息映射机制进行了系统的分析 ...

  6. BCB/Delphi中常用的VCL函数说明(字符串函数)

    本文档是ccrun(老妖)根据网上资料整理而成. --------------------内存分配--------------------函数名称:AllocMem函数说明:在队中分配指定字节的内存块 ...

  7. Swift - 各种手势检测大全(UIGestureRecognizer及其子类)

    UIGestureRecognizer有许多子类,用于监听一些常见的手势事件,这些子类主要有: 1,UISwipeGestureRecognizer:滑动(快速移动) 1 2 3 4 5 6 7 8 ...

  8. JVM查找类文件的顺序(转)

    配置classpath 根据path环境变量的原理,可以定义一个名为classpath环境变量,将要运行的class文件所在目录定义在该变量中. 例:set classpath=c:\ classpa ...

  9. 深度学习系列之CNN核心内容

    导读 怎么样来理解近期异常火热的深度学习网络?深度学习有什么亮点呢?答案事实上非常简答.今年十月份有幸參加了深圳高交会的中科院院士论坛.IEEE fellow汤晓欧做了一场精彩的报告,这个问题被汤大神 ...

  10. DataReader和DataSet的区别以及使用

    DataReader和DataSet这两个对象都可以将检索的关系数据存储在内存中.它们在功能使用方面非常相似,但是它们不可以相互替换. 主要区别如表所示:   DataReader DataSet 数 ...