吐血了,第一题全排列不会写,然后最后20分钟写出来了,但是没有DEBUG,交完卷子发现是因为自己用了HashSet线程不安全报错了,然后在结束前10分钟把HashSet改成CopyOnWriteArraySet,全排列成功了。交早了,后悔;
我好渣,第一题ac14%,第二题ac83%;又一次笔试挂了。
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.concurrent.CopyOnWriteArraySet; import java.util.regex.*; public class Main { /*请完成下面这个函数,实现题目要求的功能 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ 通过14% ******************************开始写代码******************************/ static int procee(int[] scores, int[] cards) { //开始用了不安全的数据结构HashSet,交早了。交完,距结算前10分钟做出来了 Set<int[]> set=new CopyOnWriteArraySet<>(); set.add(cards); for(int i=0;i<cards.length-1;i++){ for(int[] a:set){ int[] tmp=a.clone(); int mid=tmp[i]; tmp[i]=tmp[i+1]; tmp[i+1]=mid; set.add(tmp); } } int sum=0; for(int[] a:set){ int sum1=scores[0]; int pre=0; for(int i:a){ if(pre+i<scores.length) { sum1+=scores[pre+i]; pre+=i; }else { break; } } sum=Math.max(sum,sum1); } return sum; } /******************************结束写代码******************************/ public static void main(String[] args){ Scanner in = new Scanner(System.in); int res; int _scores_size = 0; _scores_size = Integer.parseInt(in.nextLine().trim()); int[] _scores = new int[_scores_size]; int _scores_item; for(int _scores_i = 0; _scores_i < _scores_size; _scores_i++) { _scores_item = Integer.parseInt(in.nextLine().trim()); _scores[_scores_i] = _scores_item; } int _cards_size = 0; _cards_size = Integer.parseInt(in.nextLine().trim()); int[] _cards = new int[_cards_size]; int _cards_item; for(int _cards_i = 0; _cards_i < _cards_size; _cards_i++) { _cards_item = Integer.parseInt(in.nextLine().trim()); _cards[_cards_i] = _cards_item; } res = procee(_scores, _cards); System.out.println(String.valueOf(res)); } }
import java.util.Scanner; /** * Created by lwf on 2021/5/13 19:04 * AC 83不行了 */ public class Main2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int N=sc.nextInt(); int[] X=new int[N]; int[] Y=new int[N]; int[] Z=new int[N]; for(int j=0;j<N;j++){ X[j]=sc.nextInt(); } for(int j=0;j<N;j++){ Y[j]=sc.nextInt(); } for(int j=0;j<N;j++){ Z[j]=sc.nextInt(); } int start=0; int sum=0; for(int n=0;n<N;n++) { start=n; while (start < N) { for (int i = 1; i <= N - start; i++) { int sum1 = Z[start]; int pre = start; for (int i1 = start + i; i1 < N; i1++) { if (Y[pre] <= X[i1]) { sum1 += Z[i1]; pre = i1; } } sum = Math.max(sum, sum1); } start++; } } System.out.println(sum); sc.close(); } }
全部评论
(7) 回帖