题解 P1615 【西游记公司】
0AND1STORY
2019-02-05 18:08:31
虽然此题是道红题,毫无技术含量,但也有看起来**技术含量比较高**的方法
比如说用类,构造函数和重载运算符
~~(这样用纯属装X)~~
#### 原理:
原理很简单,即计算两个**时间的差**乘上每秒偷的东西的**个数**
#### 步骤如下:
1. 读入时间:
```cpp
//输入时间
scanf("%lld:%lld:%lld", &hour, &minute, &second);
```
2. 计算时间戳:
公式如下,即将时间换算成秒:
point = hour * 3600 + minute * 60 + second
```cpp
//计算时间戳,时间戳以1秒为1单位
point = hour * 3600 + minute * 60 + second;
```
3. 计算结果并输出:
```cpp
//最后输出相差秒数乘上每秒偷的东西的数量即可
printf("%lld\n", (t2 - t1) * n);
```
#### AC代码如下:
```cpp
#include <cstdio>
using namespace std;
//因为题目说答案有可能会超int,所以这里所有都用long long比较保险
typedef long long ll;
//用一个时间类来保存一个时间
//PS:实际上写个类就是用来装X的
class Time {
private:
//分别存储小时,分钟,秒
ll hour, minute, second;
//存储时间戳(类似于句柄之类的)
ll point;
public:
//构造函数,用输入的内容进行初始化
Time() {
this->input();
}
void input() {
//输入时间
scanf("%lld:%lld:%lld", &hour, &minute, &second);
//计算时间戳,时间戳以1秒为1单位
point = hour * 3600 + minute * 60 + second;
}
//对减法进行重载,方便计算
ll operator-(Time& cmp) {
//返回时间戳的差值
return this->point - cmp.point;
}
};
int main()
{
ll n;
Time t1, t2; //在初始化的时候已经输入了数据
scanf("%lld", &n);
//最后输出相差秒数乘上每秒偷的东西的数量即可
printf("%lld\n", (t2 - t1) * n);
return 0;
}
```
~~(虽然大部分代码都是装X用的,但至少也算是一种新的方法)~~