Careercup - Google面试题 - 5692127791022080
2014-05-08 22:09
原题:
- Implement a class to create timer object in OOP
题目:用OOP思想设计一个计时器类。
解法:我根据自己的思路,用clock()函数为工具,提供启动、停止、显示时长、重置四个方法。
代码:
- // http://www.careercup.com/question?id=5692127791022080
- #include <ctime>
- #include <iostream>
- #include <string>
- using namespace std;
- class Timer {
- public:
- Timer(): begin(), end() {};
- clock_t ticks() {
- if (begin == ) {
- return end;
- } else {
- end = clock();
- return end - begin;
- }
- };
- double seconds() {
- return 1.0 * ticks() / CLOCKS_PER_SEC;
- };
- void start() {
- begin = clock();
- };
- void stop() {
- if (begin > ) {
- end = clock();
- end -= begin;
- begin = ;
- }
- };
- void reset() {
- begin = end = ;
- };
- private:
- clock_t begin, end;
- };
- int main()
- {
- Timer timer;
- string cmd;
- while (cin >> cmd) {
- if (cmd == "start") {
- timer.start();
- } else if (cmd == "stop") {
- timer.stop();
- } else if (cmd == "time") {
- printf("%.2f seconds.\n", timer.seconds());
- } else if (cmd == "reset") {
- timer.reset();
- }
- }
- return ;
- }
Careercup - Google面试题 - 5692127791022080的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- JQuery 的几个有用的技巧
JQuery代码 /* 新窗口打开链接:JQuery filter attr * 禁止鼠标弹出右键菜单:DOM contextmenu * 回到页面顶端:DOM scrollTo * 动态更换Css样 ...
- C# 运行时编辑 节点重命名
方法一: ; bool nodeChanged = false; //右键点击,就进入修改状态 private void treeView1_NodeMouseClick(object sender, ...
- Cassandra 的压缩策略STCS,LCS 和 DTCS
更新说明: 本文编写时最新的Cassandra版本为2.2,最新的稳定版本为2.1.8 2016年6月23日,增加一篇译文,当下最新版本为3.7 最新的Cassandra 2.1 或者更高的版本支持3 ...
- Resources are low on NN. Please add or free up more resources then turn off safe mode manually.
提交spark应用到yarn集群上的时候在跑一段时间就会出现这个报错: 根据上面的报错原因分析是因为集群资源不够,集群的自我保护机制使hdfs处于安全模式,然后我用"hdfs dfsadmi ...
- 2013-10-25笔记,css: mini-width, 标准居中,样式中*号使用,背景图像位置定位
mini-width:设置元素的最小宽度.該屬性值會對元素的寬度設置一個最小限制.因此,元素可以比制定值寬,但不能比制定值窄.不允許指定負值. 完美的居中佈局: body{text-align: ce ...
- Java实现猜数游戏
利用Math.random()方法产生1~100的随机整数,利用JOptionPane.showInputDialog()方法产生一个输入对话框,用户可以输入所猜的数.若所猜的数比随机生成的数大,则显 ...
- 6)Java中String类
1)String对象的初始化 由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下: String s = “abc”; ...
- MongoDB探索之路(一)——入门
1.MongoDB和传统关系型数据库的比较 2.面向文档的 NoSQL 数据库主要解决的问题不是高性能的并发读写,而是保证海量数据存储的同时,具有良好的查询性能. 3.MongoDB可以作为日志分 ...
- Collection、Iterator、Set、HashSet
Collection接口的基本方法 boolean add(Object o) 向集合当中加入一个对象 void clear() 删除集合当中的所有对象 boolean isEmpty() 判断集合是 ...
- java遍历Map的几种方式
1.遍历map的几种方式:private Hashtable<String, String> emails = new Hashtable<String, String>(); ...