AcWing 1231. 航班时间(java版)
原题链接
简单
作者:
tqyue
,
2020-05-22 16:20:19
,
所有人可见
,
阅读 1593
分析
首先,飞机飞过去,消耗的时间是(从china往usa加一个时差) end-start+time
然后,飞机从usa飞到china,然后减一个时差 end-start-time
两式合并 end1-start1+end2—start2=2*t
t=(end1-start1+end2—start2)/2
ps:java版由于没那个sscanf,只能截取字符串,很恶心
java 代码
public class Main {
static BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
private static int get_second(int h,int m,int s)
{
return 3600*h+m*60+s;
}
private static int get_time() throws IOException
{
String str=buf.readLine();
int day=0;
if(str.charAt(str.length()-1)==')')
{
day=Integer.parseInt(""+str.charAt(str.length()-2));
str=str.substring(0, str.length()-5);
}
String s[]=str.split(" ");
String s1[]=s[0].split(":");
String s2[]=s[1].split(":");
int a1=Integer.parseInt(s1[0]);
int a2=Integer.parseInt(s1[1]);
int a3=Integer.parseInt(s1[2]);
int b1=Integer.parseInt(s2[0]);
int b2=Integer.parseInt(s2[1]);
int b3=Integer.parseInt(s2[2]);
return get_second(b1, b2, b3)-get_second(a1, a2, a3)+day*24*3600;
}
public static void main(String args[]) throws IOException
{
int n=Integer.parseInt(buf.readLine());
while(n--!=0)
{
int time=(get_time()+get_time())/2;
int hour=time/3600;
int minute=time%3600/60;
int secound=time%60;
System.out.printf("%02d:%02d:%02d", hour,minute,secound);
System.out.println();
}
}
}
楼主,java的split可以指定多个分隔符,get_time函数可以写的更简单些。
https://www.acwing.com/activity/content/code/content/2604187/