SRM 219 Div II Level One: WaiterTipping,小心约分
题目来源: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 223 Div II Level Two: BlackAndRed,O(N)复杂度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=2866&rd=5853 主要是要对字符串的操作要熟悉,熟 ...
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...
- SRM 582 Div II Level One: SemiPerfectSquare
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...
- SRM 582 Div II Level Two SpaceWarDiv2
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意 ...
- SRM 583 Div II Level One:SwappingDigits
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...
- SRM 212 Div II Level One: YahtzeeScore
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...
随机推荐
- DLP底座(威创定制)
品牌:威创 型号:BC06730-1000 生产商:广东威创视讯科技股份有限公司 1.DLP底座说明 DLP底座由威创统一定制,确保了整套系统的完整性和可靠性.材质为钢结构,根据淄川地下管线中心的现场 ...
- 14.9 InnoDB Row Storage and Row Formats InnoDB 行存储和行格式:
14.9 InnoDB Row Storage and Row Formats InnoDB 行存储和行格式: 14.9.1 Overview of InnoDB Row Storage 14.9.2 ...
- TCP/IP笔记 二.网络层(1)
1. IP 1.1 配套协议 IP 是 TCP/IP 体系中两个最主要的协议之一 . 与 IP 协议配套使用的还有四个协议: (1)ARP (Address Resolution Protocol ...
- mysql基础:mysql列类型--时间和日期
mysql列类型--整型 http://blog.csdn.net/jk110333/article/details/9342283 mysql列类型--字符串http://blog.csdn.net ...
- 单片机C语言实现的采用DS18B20的温度检测装置
这几天老师布置了一个课程设计题目:采用51单片机控制的DS18B20温度检测系统.大概花了我一个礼拜的时间,幸好我的C语言学得还可以,最后还是让我搞出来了,真是高兴,我是采用STC-52单片机和DS1 ...
- T-SQL 操作文件 具体解释
/******* 导出到excel EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S" ...
- MongoDB学习笔记(一)
MongoDB的介绍我就不说了.直接开始环境的搭建和连接.在这个之前,向大家介绍几个关于MongoDB的网站. 1. https://www.mongodb.com/ MongoDB的官网. 2. ...
- Delphi的组件读写机制
Delphi的组件读写机制(一) 一.流式对象(Stream)和读写对象(Filer)的介绍在面向对象程序设计中,对象式数据管理占有很重要的地位.在Delphi中,对对象式数据管理的支持方式是其一大特 ...
- 利用WinDbg找出程序崩溃的代码行号
之前碰到论坛里有几个好友,说程序不时的崩溃,什么xxoo不能read的! 如果光要是这个内存地址,估计你会疯掉~~ 所以分享一下基本的调试技巧,需要准备的工具有WinDbg + VC6.0, 下面是自 ...
- android端向服务器提交请求的几种方式
1.GET方式 其实GET方式说白了,就是拼接字符串..最后拼成的字符串的格式是: path ? username= ....& password= ...... public boolea ...