20220914天翼云笔试
1、线段总长度(a)
public class Main3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); String[] a=new String[n]; int l,r; Map<Integer,Integer> map=new HashMap<>(); for(int i=0;i<n;i++){ a[i]= sc.next(); String[] b=a[i].split(","); l=Integer.parseInt(b[0]); r=Integer.parseInt(b[1])-1; for(int j=l;j<=r;j++){ map.put(j,map.getOrDefault(j,0)+1); } } int count=0; for(Map.Entry<Integer,Integer> entry:map.entrySet()){ if(entry.getValue()==1) count++; } System.out.println(count); } }2、偶数在前奇数在后(a)
3、搬家(贪心,80%)public class Main1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); String s=sc.next(); String[] sp=s.split(","); int[] a=new int[n]; String res=""; for(int i=0;i<n;i++){ a[i]=Integer.parseInt(sp[i]); } ArrayList<Integer> list=new ArrayList<>(); for(int j=0;j<n;j++){ if(a[j]%2==0){ list.add(a[j]); a[j]=-1; } } for(int j:list){ res+=j+","; } for(int j=0;j<n;j++){ if(a[j]!=-1){ res+=a[j]+","; } } res=res.substring(0,res.length()-1); System.out.println(res); } }
public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int muscle=sc.nextInt(); int jiaju=sc.nextInt(); int[] s=new int[jiaju]; for(int i=0;i<jiaju;i++) { s[i]=sc.nextInt(); } int jiaju2=sc.nextInt(); int[] h=new int[jiaju2]; for(int i=0;i<jiaju2;i++) { h[i]=sc.nextInt(); } double[] tanxin=new double[jiaju]; for(int i=0;i<jiaju;i++){ tanxin[i]=(double) s[i]/h[i]; } Arrays.sort(tanxin); int cout=0,musclenow=0; for(int i=jiaju-1;i>=0;i--){ if(musclenow<=musclenow){ if(muscle>=h[i]+musclenow){ cout+=s[i]; musclenow+=h[i]; } } } System.out.println(cout); } }