让我们先来读题:

Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

It is much easier to understand with an example:

* For seconds = 62, your function should return "1 minute and 2 seconds"
* For seconds = 3662, your function should return"1 hour, 1 minute and 2 seconds"

其实理解起来很简单,给定你一个秒数,让你根据现实转换成年,日,小时,分钟,秒的组合。其中要注意标点符号以及"and"的顺序及位置。

做题思路:

首先将各个年月日的计算结果num放到一个vector中,再将各个计算结果对应的英语名称放到另一个vector<string>中。(注意顺序,应该是一一对应的)如果碰到num为0的情况,直接在遍历中continue即可,大于1的情况下,通过stringstream转换成string后再加's'即可。

通过观察可以发现"and"仅会出现一次,并且出现在倒数第二个非零的num所对应的时间跨度以及最后一个非零的num所对应的时间跨度中间。于是我写了一个get_vector_notzero函数用于数出每个num后续对应的非零num个数。其余情况都是", "。

下面请看代码。

#include <string>
#include <vector>
#include <algorithm>
#include <sstream>using namespace std;
int get_vector_notzero(vector<int>vec,int pos)//计算当前位置的num后面还有多少个非零数{ int count = 0;for(auto i =pos+1;i<vec.size();i++){if(vec[i]!=0) count +=1;}return count;}string format_duration(int seconds) {// your code herestringstream ss;//用于int和string类型的转换vector<int> num;vector<string> str1;vector<string> punc;//放置标点符号的vectorstring now = "now";string year = "year";str1.push_back(year);string day = "day";str1.push_back(day);string hour = "hour";str1.push_back(hour);string minute ="minute";str1.push_back(minute);string second ="second";str1.push_back(second);punc.push_back(", ");punc.push_back(" and ");string res;//存放结果的stringint Num_year,Num_day,Num_hour,Num_minute,Num_second;//计算所需的时间Num_year=seconds/31536000;num.push_back(Num_year);Num_day = (seconds-31536000*Num_year)/86400;num.push_back(Num_day);Num_hour = (seconds-31536000*Num_year-86400*Num_day)/3600;num.push_back(Num_hour);Num_minute = (seconds-31536000*Num_year-86400*Num_day-3600*Num_hour)/60;num.push_back(Num_minute);Num_second = seconds-31536000*Num_year-86400*Num_day-3600*Num_hour-60*Num_minute;num.push_back(Num_second);if(seconds==0) return now;else{for(auto i = 0;i<5;i++){if(num[i]==0) continue;else{string temp_str;if(num[i]>1) str1[i]+='s';//res+=num[i];ss<<num[i];ss>>temp_str;res+=temp_str;res+=" ";ss.clear();res+=str1[i];int vector_notzero = get_vector_notzero(num,i);if(i==4) break;//如果是最后一个数,直接跳出循环else if(vector_notzero==0) break;//如果后面的数都为0,无需加标点,直接退出循环else if(vector_notzero==1) //如果后面还有一个数不为0,那么加and{res+=punc[1];//1是and 0是”,“continue;}else res+=punc[0];}}return res;}
}

C++ CodeWar KATA4:Human readable duration format相关推荐

  1. pip list报错:DEPRECATION: The default format will switch to columns in the future.

    pip list报错:DEPRECATION: The default format will switch to columns in the future. sudo gedit ~/.pip/p ...

  2. Android 报错:Conversion to Dalvik format failed: ...

    2019独角兽企业重金招聘Python工程师标准>>> 在android高版本开发环境(sdk 4.4)导入低版本(sdk 3.0)的工程时编译报错,报错信息如:Conversion ...

  3. 解决Jupyter notebook报错:AssertionError: wrong color format ‘var(--jp-mirror-editor-variable-color)‘

    今天使用jupyter notebook时出错: 500: Internal Server Error 错误现象 查看日志: AssertionError: wrong color format 'v ...

  4. Paper reading (二十一):Human gut microbiome: hopes, threats and promises

    论文题目:Human gut microbiome: hopes, threats and promises scholar 引用:120 页数:10 发表时间:2018.04 发表刊物:Gut 作者 ...

  5. 【Python】获取roc、auc时候报错:raise ValueError({0} format is not supported.format(y_type))

    代码: fpr, tpr, thresholds = roc_curve(y_test, test_prob, pos_label=1, sample_weight=None, drop_interm ...

  6. [Android 硬件] Eclipse错误:Conversion to Dalvik format failed with error 1

    今天遇到一个蛋疼的问题,就是题目上方的报错.于是乎,网上搜索各种方法解决问题. 下面是网上的总结,在这里发布一下. 1.Eclipse错误:Conversion to Dalvik format fa ...

  7. codewars - Human Readable Time

    codewars - Human Readable Time 我的代码 def make_readable(seconds):# Do somethinghh = 0mm = 0ss = second ...

  8. Paper reading (八十七):Human microbiome aging clocks based on DL

    论文题目:Human microbiome aging clocks based on deep learning and tandem of permutation feature importan ...

  9. Dataset:Human Detection and Search

    Dataset:Human Detection and Search 1. 背景 主要归纳了一下Human Detection and Search 涉及到的6个数据集, 分别为ETHZ, CityP ...

最新文章

  1. 网站输入正确账号密码页面刷新一下_Folx的密码管理保存网站登陆信息
  2. C语言基础_函数指针
  3. 4-1 复数类的运算符重载
  4. 【bzoj2844 albus就是要第一个出场】
  5. 把软件放到图片里(超强)
  6. spark 把一列数据合并_Spark Java-合并同一列多行 - java
  7. 好插件·用户造【CSND超好用插件】·【机械键盘大放送】
  8. 深入浅出学Hive:Hive体系结构
  9. cf1561D Up the Strip(D1D2)
  10. DB2 9 底子(730 考试)认证指南,第 3 局部: 拜访 DB2 数据(3)
  11. SpringCloud(二) 服务注册与发现Eureka
  12. 可以打开md_大祥MD型耐磨卧式多级离心泵现货出售中大泵业
  13. android信鸽推送demo_android studio 集成腾讯信鸽推送
  14. matlab 图例 显示几个,matlab图例拆分成两个多个分别显示
  15. C51 汇编和C语言编写从1加到100
  16. nextpolish安装_使用nextpolish对三代组装进行polish
  17. 一些简单的css,html,js笔记分享给大家,希望能够帮助到大家
  18. 2019年最新版百度网盘不限速下载工具,无需登录实现高速下载!
  19. k8s与日志--journalbeat源码解读 1
  20. HT合泰单片机入门教程(第五章 PWM)

热门文章

  1. 如何视频改字,修改视频内容
  2. 墨者学院-Bash漏洞分析溯源
  3. JSBridge解析
  4. 2022A特种设备相关管理(电梯)判断题及模拟考试
  5. LINUX服务器集群系统
  6. glob php,php使用glob函数快速查询指定目录文件的方法
  7. 拼多多跨境电商好不好做众心正和
  8. CSE RPC流程分析
  9. OhMyZsh是一款开源工具,可以用于管理Zsh(Linux命令解释器的一种)的配置
  10. 提高应用在 iOS App Store 中排名的 20 条技巧