赞
踩
第一题给出7个24小时制的时间区间,计算用的分钟数;
package saima;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
//小红书测试笔试题1
public class totaltime {
public static void main(String args[]) throws ParseException {
Scanner cin = new Scanner(System.in);
int[] mins = new int[14];
int[] hours = new int[14];
for(int i=0; i<14; i++){
String time = cin.nextLine();
String[] arrtime = time.split(":");
hours[i] = Integer.parseInt(arrtime[0]);
mins[i] = Integer.parseInt(arrtime[1]);
}
int ans =0;
for (int i =0;i<=6;i++){
if(hours[2*i]==hours[2*i+1]) ans = ans+(mins[2*i+1]-mins[2*i]);
//hours[i]<hours[i+1]
else{
ans = ans +( (hours[2*i+1]-hours[2*i])*60 + (mins[2*i+1]-mins[2*i]));
// System.out.println(ans);
}
}
System.out.println(ans);
}
}
第二题给出一个字符串,统计频次至少为3的字符串;
package saima;
import java.util.*;
//小红书测试笔试2——词频
public class recommend {
public static void main(String arg[]) {
Scanner in = new Scanner(System.in);
String words = in.nextLine();
String[] arrwords = words.split(" ");
HashMap<String, Integer> hash = new HashMap<String, Integer>();
HashMap<String, Integer> hashans = new HashMap<String, Integer>();
for(int i =0; i<arrwords.length;i++){
if(hash.containsKey(arrwords[i])){
int value = hash.get(arrwords[i]);
value= value+1;
hash.put(arrwords[i],value);
if(hash.get(arrwords[i])>=3){
hashans.put(arrwords[i],hash.get(arrwords[i]));
}
}
else{
hash.put(arrwords[i],1);
}
}
//对hashans排序
List<Map.Entry<String,Integer>> listsort = new ArrayList<>(hashans.entrySet());
//重写比较器
listsort.sort((a,b)->{
if(a.getValue().equals(b.getValue())) return a.getKey().compareTo(b.getKey());
return b.getValue().compareTo(a.getValue());
});
for(Map.Entry entry:listsort){
System.out.println(entry.getKey());
}
}
}
第三题给出总的时间,精力值,每个事件的所需时间,精力值,以及收获的开心值,计算能收获的最多的开心值;
package saima;
import java.sql.SQLOutput;
import java.util.Scanner;
//小红书测试笔试3
public class happyValues {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n= cin.nextInt();
int T = Integer.parseInt(cin.next());
int H =Integer.parseInt( cin.next());
// System.out.println(T);
int[] arrT = new int[n];//时间
int[] arrH = new int[n];//精力
int[] arra = new int[n]; // 快乐值
for(int i=0;i<n ;i++){
arrT[i]= Integer.parseInt(cin.next());
arrH[i]= Integer.parseInt(cin.next());
arra[i] = Integer.parseInt(cin.next());
// System.out.println(arra[i]);
}
int[][] dp = new int[T+1][H+1];
for(int i = 0; i <n; i++){
for(int j =T; j >= arrT[i]; j--)
for(int k = H; k >= arrH[i]; k--)
dp[j][k] = Math.max(dp[j][k], dp[j-arrT[i]][k-arrH[i]] + arra[i]);
}
System.out.println(dp[T][H]);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。