网易运筹优化笔试,编程题第3题题解:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for(int i = 0; i < n; i++){
int num = cin.nextInt();
int[] f = new int[num+1]; //表示前i个人需要的最短时间
//读入单个时间和双人时间
int[] one = new int[num+1];
int[] two = new int[num+1];
for(int j = 1; j <= num ; j++){
one[j] = cin.nextInt();
}
for(int k = 2; k <= num; k++){
two[k] = cin.nextInt();
}
f[1] = one[1];
for(int j = 2; j <= num; j++) {
f[j] = Math.min(f[j - 1] + one[j], f[j - 2] + two[j]);
}
System.out.println(getS(f[num]));
}
}
}
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
for(int i = 0; i < n; i++){
int num = cin.nextInt();
int[] f = new int[num+1]; //表示前i个人需要的最短时间
//读入单个时间和双人时间
int[] one = new int[num+1];
int[] two = new int[num+1];
for(int j = 1; j <= num ; j++){
one[j] = cin.nextInt();
}
for(int k = 2; k <= num; k++){
two[k] = cin.nextInt();
}
f[1] = one[1];
for(int j = 2; j <= num; j++) {
f[j] = Math.min(f[j - 1] + one[j], f[j - 2] + two[j]);
}
System.out.println(getS(f[num]));
}
}
}
全部评论
public static String getS(int num){
String res = null;
String s1 = null;
String s2 = null;
String s3 = null;
String s4 = null;
int s = num%60;
int m = num/60%60;
int h = num/60/60;
if(h>4)
s1 = "pm";
else
s1 = "am";
int h1 = 8+h;
if(h1/10 >= 1)
s2 = ""+h1;
else
s2 = "0"+h1;
if(m/10 >= 1)
s3 = ""+m;
else
s3 = "0"+m;
if(s/10 >=1)
s4 = ""+s;
else
s4 = "0"+s;
res = s2+":"+s3+":"+s4+" "+s1;
return res;
}
相关推荐