2017 ThoughtWorks校招编程作业(C++版)
来源:互联网 发布:地图软件电脑版 编辑:程序博客网 时间:2024/05/17 22:15
题目
小明是一个羽毛球爱好者,考虑到许多人喜欢打羽毛球但是找不大球友,他打算成立一个羽毛球俱乐部,不定期组织羽毛球活动,活动可以是每周的任一时段,每次时间为2小时或3小时。参加活动的人每次付给小明30元。小明向羽毛球馆支付总费用。
小明每次会根据报名人数决定向球馆规定若干球场,通常会按照6个人一个场地预定。但小明发现报名人数多数情况并不是6的整数倍,到底该不该为多出来的人预定场地,让小明很头疼。
经过一段时间的摸索,小明找到一种让大多数人满意的方式:
- 计参加一次活动的总人数为M
- 记T为可以立即确定的场地数,其值为M/6(整除)。例如M为6,则T为1;M为5,则T为0
- 记X为多出来的人,其值为M%6(求模)。比如M为8,则X为2(按求模定义,X的值小于6)
则订场策略如下:
比如只来了三个人,则取消活动;来了五个人,则订一个场;来了7个人,则订2个场;来了29个人订4个场,等等;
同时,小明选择的羽毛球馆每块场地收费标准如下:
- 周一到周五
9:00 ~ 12:00 30/时
12:00 ~ 18:00 50/时
18:00 ~ 20:00 80/时
20:00 ~ 22:00 60/时 - 周六及周日
9:00 ~ 12:00 40/时
12:00 ~ 18:00 50/时
18:00 ~ 22:00 60/时
每过一段时间,小明会根据活动情况计算收入,请编写程序帮助小明计算收入。
要求
实现以下函数或方法,例如:
- Javascript:
//参数input是如下面范例所示的输入(字符串),返回值为生成的汇总信息(字符串)。string generateSummary(input)
- Java:
string generateSummary(String input)
- C#:
string GenerateSummary(string input)
注意:你可以对该函数的实现进行任意的设计,例如划分函数或创建新的类型。
输入为字符串,其格式定义如下
范例:
2016-06-02 20:00~22:00 7
2016-06-03 09:00~12:00 14
2016-06-04 14:00~17:00 22
2016-06-05 19:00~22:00 3
2016-06-06 12:00~15:00 15
2016-06-07 15:00~17:00 12
2016-06-08 10:00~13:00 19
2016-06-09 16:00~18:00 16
2016-06-10 20:00~22:00 5
2016-06-11 13:00~15:00 11
输出为字符串,针对上述范例,应当有如下输出:
[Summary]
2016-06-02 20:00~22:00 +210 -240 -30
2016-06-03 09:00~12:00 +420 -180 +240
2016-06-04 14:00~17:00 +660 -600 +60
2016-06-05 19:00~22:00 +0 -0 0
2016-06-06 12:00~15:00 +450 -300 +150
2016-06-07 15:00~17:00 +360 -200 +160
2016-06-08 10:00~13:00 +570 -330 +240
2016-06-09 16:00~18:00 +480 -300 +180
2016-06-10 20:00~22:00 +150 -120 +30
2016-06-11 13:00~15:00 +330 -200 +130
Total Income: 3630
Total Payment: 2470
Profit: 1160
其中:
- 每一行代表一次活动的收支情况,格式为{活动日期:yyyy-MM-dd} {活动时间段:HH:mm~HH:mm} {活动时间段:HH:mm~HH:mm} {收入:+数字} {支出: -数字} {结余}
- 结余的部分如果大于0 则格式为+数字,如果小于0则格式为 -数字,如果等于0则直接为0
- 活动顺序与输入保持一致
实现:
预定义的几个常量:
const int cActivityCancle = -1; //取消活动const int cPayEach = 30;//每人付给小明30元const int cEveryPlace = 6;//按6个人一个场地预定
因周内和周末价格不一样,要判断输入的某天是周几。蔡勒公式定义
int GetDayOfWeek(int nYear, int nMonth, int nDay){ //2016-06-02 20:00~22:00 int iWeek = 0; unsigned int y = 0, c = 0, m = 0, d = 0; if (nYear == 1 || nMonth == 2) { c = (nYear - 1) / 100; y = (nYear - 1) % 100; m = nMonth + 12; d = nDay; } else { c = nYear / 100; y = nYear % 100; m = nMonth; d = nDay; } //蔡勒公式 iWeek = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; iWeek = iWeek >= 0 ? (iWeek % 7) : (iWeek % 7 + 7); if (iWeek == 0) { iWeek = 7; } return iWeek;}
时间格式的判断,输入格式为:yyyy-MM-dd(活动日期) HH:mm~HH:mm(活动时间段) 人数
//ACTIVITY_INFO结构体定义如下:typedef struct _ACTIVITY_INFO{ int nYear; int nMonth; int nDay; int nStartTime;//时间必为整小时,开始时间 int nEndTime;//结束时间 int nPeoeleNum;//活动人数 std::string strRawTime;//输入时间,格式为:年-月-日}ACTIVITY_INFO;判断输入是否为非法字符,若非法直接返回false。用到了C的格式化输入`sscanf`bool ParseInputTime(const std::string &strTime, ACTIVITY_INFO &structInfo){ //2016-06-02 20:00~22:00 7 //strTime为原始每一行的输入:年-月-日 开始时间~结束时间 人数 int nRet = sscanf(strTime.c_str(), "%d-%d-%d %d:00~%d:00 %d", &structInfo.nYear, &structInfo.nMonth, &structInfo.nDay, &structInfo.nStartTime, &structInfo.nEndTime, &structInfo.nPeoeleNum); if (6 != nRet) return false; int nIntervalTime = structInfo.nEndTime - structInfo.nStartTime; //每场预定时间为2小时或3小时 if( nIntervalTime!= 2 && nIntervalTime != 3) return false; bool bRet = true; auto iterBlank = strTime.rfind(" "); if (iterBlank != std::string::npos) structInfo.strRawTime = strTime.substr(0, iterBlank); else bRet = false; return bRet;}
根据不同的分配策略得到预定的场地数
int GetOrderStrategy(const int nPlaceNum, const int nRedundant){ int nTotalPlace = 0; if (0 == nPlaceNum) { if (nRedundant < 4) nTotalPlace = cActivityCancle; else nTotalPlace = 1; } else if (1 == nPlaceNum) { nTotalPlace = 2; } else if (2 == nPlaceNum || 3 == nPlaceNum) { if (nRedundant >= 4) nTotalPlace = nPlaceNum + 1; else nTotalPlace = nPlaceNum; } else { nTotalPlace = nPlaceNum; } return nTotalPlace;}
计算租用场地周内或周末某天一场应该付的金额:
//枚举类型DAY_OF_WEEK定义enum DAY_OF_WEEK{ EM_DAY_MON = 1, EM_DAY_TUE, EM_DAY_WED, EM_DAY_THU, EM_DAY_FRI, EM_DAY_SAT, EM_DAY_SUN};//周内和周末每个时间段的租金均不同,建立一个哈希Map来存储其映射关系。//map<int, int>---<时间,价格>std::unordered_map<int, int> g_setWeekPay = { { 9, 30 }, { 10, 30 }, { 11, 30 }, { 12, 50 },{ 13, 50 }, { 14, 50 }, { 15, 50 }, { 16, 50 }, { 17, 50 }, { 18, 80 }, { 19, 80 },{ 20, 60 }, { 21, 60 } };std::unordered_map<int, int> g_setWeekendPay = { { 9, 40 }, { 10, 40 }, { 11, 40 }, { 12, 50 },{ 13, 50 }, { 14, 50 }, { 15, 50 }, { 16, 50 }, { 17, 50 }, { 18, 60 },{ 19, 60 }, { 20, 60 }, { 21, 60 } };int GetTotalPay(int nDayOfWeek, int nStartTime, int nEndTime){ //时间必然为整小时 int nTotalPay = 0; int nCurrentHour = nStartTime; switch (nDayOfWeek) { case EM_DAY_MON: case EM_DAY_TUE: case EM_DAY_WED: case EM_DAY_THU: case EM_DAY_FRI: while (nCurrentHour < nEndTime) { auto iter = g_setWeekPay.find(nCurrentHour); if (iter != g_setWeekPay.end()) nTotalPay += iter->second; nCurrentHour++; } break; case EM_DAY_SAT: case EM_DAY_SUN: while (nCurrentHour < nEndTime) { auto iter = g_setWeekendPay.find(nCurrentHour); if (iter != g_setWeekendPay.end()) nTotalPay += iter->second; nCurrentHour++; } break; default: break; } return nTotalPay;}
处理每一行的输入:
//输入:2016-06-03 09:00~12:00 14//输出:2016-06-02 20:00~22:00 +210 -240 -30std::string generateEachLineSummary(const std::string &strEachLine){ std::string eachLineOutPut(""); ACTIVITY_INFO tmActivity; if (!ParseInputTime(strEachLine, tmActivity)) { printf("Input Format Error\n"); exit(0); } int nPlaceNum = tmActivity.nPeoeleNum / cEveryPlace; int nRedundant = tmActivity.nPeoeleNum % cEveryPlace; int nTotalPlace = GetOrderStrategy(nPlaceNum, nRedundant); if (cActivityCancle == nTotalPlace) { //这里注意,直接取消活动的条件 eachLineOutPut += tmActivity.strRawTime + " +0 -0 0";; } else { int nDayOfWeek = GetDayOfWeek(tmActivity.nYear, tmActivity.nMonth, tmActivity.nDay); int nPay = GetTotalPay(nDayOfWeek, tmActivity.nStartTime, tmActivity.nEndTime); nPay *= nTotalPlace; int nIncome = tmActivity.nPeoeleNum * cPayEach; int nProfit = nIncome - nPay; if (nProfit > 0) { eachLineOutPut = tmActivity.strRawTime + " +" + std::to_string(nIncome); eachLineOutPut += " -" + std::to_string(nPay) + " +" + std::to_string(nProfit); } else { eachLineOutPut = tmActivity.strRawTime + " +" + std::to_string(nIncome); eachLineOutPut += " -" + std::to_string(nPay) + " " + std::to_string(nProfit); } } return eachLineOutPut;}
generateSummary函数实现:
std::string generateSummary(const std::string &input){ std::string strSummary(""); std::vector<std::string> strEachLine; auto iter = input.find("\n"); int nStartPos = 0; while (iter != std::string::npos) { strEachLine.push_back(input.substr(nStartPos, iter - nStartPos)); nStartPos = iter + 1; iter = input.find("\n", iter + 1); } if (!input.substr(nStartPos).empty()) strEachLine.push_back(input.substr(nStartPos)); for (size_t i = 0; i < strEachLine.size(); i++) { strSummary += generateEachLineSummary(strEachLine[i]); strSummary += "\n"; } return strSummary;}
汇总最后的收支情况,对应输出的下列需求:
Total Income: 3630
Total Payment: 2470
Profit: 1160
typedef struct _TOTAL_MONEY{ int nTotalIncome; int nTotalPayment; int nTotalProfit;}TOTAL_MONEY;TOTAL_MONEY GetTotalMoney(const std::string &strSummary){ //2016-06-09 16:00~18:00 +480 -300 +180 TOTAL_MONEY totalMoney{ 0, 0, 0 }; auto iter = strSummary.find("\n"); int nStartPos = 0; while (iter != std::string::npos) { std::string strEachLine = strSummary.substr(nStartPos, iter - nStartPos); auto iterProfit = strEachLine.rfind(" "); if (iterProfit != std::string::npos) totalMoney.nTotalProfit += std::stoi(strEachLine.substr(iterProfit)); auto iterPay = strEachLine.rfind(" ", iterProfit - 1); if (iterPay != std::string::npos) totalMoney.nTotalPayment += std::stoi(strEachLine.substr(iterPay, iterProfit - iterPay)); auto iterIncome = strEachLine.rfind(" ", iterPay - 1); if (iterIncome != std::string::npos) totalMoney.nTotalIncome += std::stoi(strEachLine.substr(iterIncome, iterPay - iterIncome)); nStartPos = iter + 1; iter = strSummary.find("\n", iter + 1); } return totalMoney;}
函数开始,main函数实现:
//默认为输入'!'表示结束输入。//用getline()函数读入输入的内容,因该函数会默认忽略换行符,故每一步后都给输入的字符串strInput添加换行符int main(){ std::string strInput(""); std::string consoleInput(""); while (getline(std::cin, consoleInput)) { if (consoleInput == "!") break; strInput += consoleInput + "\n"; } std::string strSummary = generateSummary(strInput); printf("[Summary]\n\n"); printf("%s", strSummary.c_str()); TOTAL_MONEY totalMoney = GetTotalMoney(strSummary); printf("Total Income: %d\n", totalMoney.nTotalIncome); printf("Total Payment: %d\n", -totalMoney.nTotalPayment); printf("Total Profit: %d\n", totalMoney.nTotalProfit); system("pause"); return 0;}
- 2017 ThoughtWorks校招编程作业(C++版)
- 2016 ThoughtWorks 校招编程作业
- ThoughtWorks 2018校招作业
- 2017滴滴校招编程
- 2017 滴滴校招编程
- 2017腾讯校招编程
- ThoughtWorks 2018校园招聘作业
- ThoughtWorks 结对编程
- 2017校招滴滴笔试编程题(深搜+剪枝)
- 聊聊ThoughtWorks面试(郑大版 社招)+ PS:应届生简单流程介绍
- 2017 12.29的c语言编程作业
- 字符串处理(华为2016校招软件类机试编程题)(C++)
- 校招编程题目(1) C++:括号匹配方案(京东)
- 2017年网易校招算法编程题:优雅的点(PHP版)
- 2017年网易校招算法编程题:优雅的点(PHP版)
- 2017 百度校招 编程题
- 携程2017 校招编程题
- 网易2017校招编程:跳石板
- C++“准”标准库Boost学习指南(8):Boost.Tuple
- windows cmd 查看md5 (结果16 进制)
- HTML练习--制作宣传视频
- android:focusableInTouchMode
- String s = new String(“xyz”);产生几个对象?
- 2017 ThoughtWorks校招编程作业(C++版)
- mysql(二):使用聚合函数查询
- php处理微信小程序wx.uploadFile(OBJECT)发送过来的文件
- 修改ubuntu的用户名(注意用户名和主机名的区别)
- I/O之缓冲输入流
- VS2013 编译live555
- 一个灾难性的问题的暂时解决办法
- JDBC和ODBC
- 浏览器工作原理(二):渲染引擎的详细介绍