题目来源: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. OpenCV编程-&gt;Windows7下调用iPhnoe摄像头

    //////////////////////////////////////////////////////////////  指尖热度原创,转载请注明来自http://blog.csdn.net/s ...

  2. JQuery+AJax - 无刷新使用验证码

    最终效果: 项目目录: Default.aspx前端代码: <%@ Page Language="C#" AutoEventWireup="true" C ...

  3. C语言sendto()函数-经socket传送数据以及recvfrom函数《转》

    相关函数:send, sendmsg, recv, recvfrom, socket 头文件:#include <sys/types.h>   #include <sys/socke ...

  4. S3C6410 纯粹的裸机启动,自己写的SD BOOT启动

    这几天晚上一直折腾S3C6410的裸机SD卡启动,不大想使用UBOOT,我是搞硬件的,对底层非常感兴趣,不喜欢已经写好的,因此自己一直在尝试,其实很早之前就试过SD卡启动,也就是ARM11上电后会把S ...

  5. PHP之操作数据库

    数据库,顾名思义,是一个存放数据的容器.然后在使用过程中对数据库里面的数据增删改查,具体是怎么实现的呢? 这儿不得不提一下一个神奇的东西:SQL语句:结构化查询语言(Structured Query ...

  6. HTML5开发桌面应用:选择node-webkit还是有道heX

    近几年,移动应用和web2.0大行其道,相比之下.传统桌面应用程序开发显得相对冷清(包含该领域技术人才的后继力量),但在一些场景下,它依旧有其不可替代的优势. 将HTML5和Node.JS的技术优势. ...

  7. Eclipse中导入第三方源码的问题和备用解决方案

    在前篇<配置BeanUtils包,同时也是对导入第三包的步骤说明>中,我已经将[commons-beanutils-1.9.2.jar]包导入,但是在使用BeanUtils进行日期转换的过 ...

  8. mysql5.6 主从配置

    参考网址:http://www.cnblogs.com/zhoujie/p/mysql1.html http://kerry.blog.51cto.com/172631/277414/ 1.配置主库: ...

  9. VC添加背景图片 的一种方法

    .如果程序是新建的对话框,要给其添加背景图片的步骤: 1)加入消息函数:afx_msg void OnPaint(); 2)BEGIN_MESSAGE_MAP(QueryDlg, CDialog) O ...

  10. [Android学习笔记]扩展application

    扩展Application对象 每一个应用程序启动之后,都会分配一个linux用户,并且运行在一个独立的进程中.默认情况下,一个应用程序只会运行在一个进程中(可以通过配置android:process ...