题解 CF1133A 【Middle of the Contest】

一道字符串模拟题,思路如下:

  • 1 输入时将两个时间存储

  • 2 将两个时间化为以分钟做单位

  • 3 得到中间时间与第一个时间的差

  • 4 输出

注意:当时间或分钟数$<=9$时,要用$0$补齐位数

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int h1,h2,m1,m2;
int main()
{
ios::sync_with_stdio(false);
cin>>s1>>s2;
for(int i=0;i<2;i++)//将时间存储
h1+=(s1[i]-'0')*pow(10,1-i),h2+=(s2[i]-'0')*pow(10,1-i);
for(int i=3;i<=4;i++)
m1+=(s1[i]-'0')*pow(10,4-i),m2+=(s2[i]-'0')*pow(10,4-i);
int ans=((h2*60+m2)-(h1*60+m1))/2;//求得中间时间与第一个时间的差
h1+=ans/60;//加上时间差
m1+=ans%60;
if(m1>=60)//如果分钟>=60要进位
{
h1++;
m1%=60;
}
if(h1<=9)//输出,<=9的用0补齐
cout<<0<<h1<<":";
else
cout<<h1<<":";
if(m1<=9)
cout<<0<<m1;
else
cout<<m1;
return 0;
}