A-题解

时间规划

简单思路:全部转换成分钟后再计算结果,7:00可以看作从0:00时刻开始过了630分钟。

针对样例一

纸上或者注释

$$\begin{align*} \text{开始时间} &= 9 \times 60 + 5 \\ &= 545 \\ \text{结束时间} &= 9 \times 60 + 6 \\ &= 546 \\ \text{间隔时间} &= 546 - 545 \\ &= 1 \end{align*}$$

样例通过,解法(表达式)正确

替换

我们令A表示开始时刻的小时,B表示开始时刻的分钟,C表示结束时刻的小时,D表示结束时刻的分钟,S表示开始时间,E表示结束时间,ans表示间隔时间。

$$\begin{align*} \text{S} &= A \times 60 + B \\ \text{E} &= C \times 60 + D \\ \text{ans} &= E - S \end{align*}$$

写代码

#include <bits/stdc++.h> // 万能头
using namespace std;

int main() {
  // 处理读入
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  
  // 处理数据
  int S = A * 60 + B; 
  int E = C * 60 + D;
  int ans = E - S;
  
  // 处理输出
  cout << ans << '\n';
  
  return 0;
}

买文具

简单思路:计算出总花费,和手里的现在有的钱比较下是多还是少。

针对样例二

纸上或者注释

$$\begin{align*} \text{总花费} &= 2 \times 1 + 5 \times 1 + 3 \times 1 \\ &= 10 \\ \text{差} &= 5 - 10\\ &= -5 \end{align*}$$

因为差值是负数,所以钱不够,还差5块钱。负数在这里表示差多少钱,正数在这里表示剩多少钱。

替换

我们令X表示买签字笔的数量,令Y表示买记事本的数量,令Z表示买直尺的数量,Q表示现在手里有的钱,令S表示总花费,ans表示差值。

$$\begin{align*} \text{S} &= 2 \times X + 5 \times Y + 3 \times Z \\ \text{ans} &= S - Q\\ \end{align*}$$

写代码

#include <bits/stdc++.h>
using namespace std;
int main() {
  // 处理读入
  int X, Y, Z, Q;
  cin >> X >> Y >> Z >> Q;
  
  //处理数据
  S = 2 * x + 5 * y + 3 * z;
 ans = S - Q;
  
  // 处理输出 
  if (ans >= 0) {
    cout << "Yes" << '\n';
    cout << ans << '\n';
  } else {
  	// ans 为负数,-ans表示乘上一个-1变成整数,表示缺少的钱数
  	cout << "No" << '\n';
    cout << -ans << '\n';
  }
  
  return 0;
}