Java 7, 164 byte
String c(String a,String b){long s=x(b,1)-x(a,1)+(x(b,0)-x(a,0))*60,m=s%60;return(s/60)+":"+(m>9?m:"0"+m);}long x(String s,int i){return new Long(s.split(":")[i]);}
Penjelasan:
String c(String a, String b){ // Method with two String parameters and String return-type
long s = x(b,1) - x(a,1) // Get difference in seconds from input times
+ (x(b,0) - x(a,0)*60, // plus the difference in minutes times 60 to get the seconds
m = s%60; // Temp variable of seconds after we've subtracted the minutes (used multiple times)
return (s/60) // Return minutes
+":" // plus ":"
+(m>9?m:"0"+m); // plus seconds (with a leading 0 if necessary)
} // End of method
long x(String s,int i){ // Separate ethod with String and Integer parameters and long return-type
return new Long(s.split(":")[i]; // Return either minutes or seconds of String parameter based on the index
} // End of method
Kode uji:
Coba di sini.
class M{
String c(String a,String b){long s=x(b,1)-x(a,1)+(x(b,0)-x(a,0))*60,m=s%60;return(s/60)+":"+(m>9?m:"0"+m);}long x(String s,int i){return new Long(s.split(":")[i]);}
public static void main(String[] a){
M m = new M();
System.out.println(m.c("0:00", "0:01"));
System.out.println(m.c("0:55", "1:00"));
System.out.println(m.c("1:45", "3:15"));
}
}
Keluaran:
0:01
0:05
1:30
:
merupakan perintah (data sebagai filosofi kode). Apakah saya diizinkan menggunakan spasi atau apakah saya perlu menemukan bahasa lain untuk menjawab ini?