importjava.util.Arrays;importjava.util.HashSet;importjava.util.Random;importjava.util.Scanner;importjava.util.Set;publicclassNumberGuessingGame{publicstaticStringgenerateRandomNumber(){Random random =newRandom();Integer[] digits ={0,1,2,3,4,5,6,7,8,9};Arrays.asList(digits);Arrays.sort(digits,(a, b)-> random.nextInt(3)-1);// Shuffle the array randomlyreturnArrays.toString(Arrays.copyOf(digits,4)).replaceAll("\\D+","");}publicstaticbooleanhasDuplicateDigits(String number){Set<Character> digitSet =newHashSet<>();for(char digit : number.toCharArray()){if(!digitSet.add(digit)){returntrue;// Duplicate digit found}}returnfalse;// No duplicate digits}publicstaticint[]evaluateGuess(String secretNumber,String userGuess){int[] result =newint[2];// Index 0: A (correct digit and position), Index 1: B (correct digit but wrong position)for(int i =0; i < secretNumber.length(); i++){char secretChar = secretNumber.charAt(i);char guessChar = userGuess.charAt(i);if(secretChar == guessChar){result[0]++;}elseif(secretNumber.contains(String.valueOf(guessChar))){result[1]++;}}return result;}publicstaticvoidmain(String[] args){Scanner scanner =newScanner(System.in);String secretNumber =generateRandomNumber();int attemptsLeft =10;System.out.println("欢迎来到猜数字游戏!");System.out.println("每个数字都是0到9之间的不同数字。");System.out.println("你有10次机会猜对它。");while(attemptsLeft >0){System.out.print("请输入你的猜测(四位数):");String userGuess = scanner.nextLine();// Check if the input is a four-digit number with distinct digitsif(!userGuess.matches("\\d{4}")||hasDuplicateDigits(userGuess)){System.out.println("请确保输入是一个四位数且没有相同的数字。");continue;}int[] evaluation =evaluateGuess(secretNumber, userGuess);System.out.println("结果:"+ evaluation[0]+"A"+ evaluation[1]+"B");if(evaluation[0]==4){System.out.println("恭喜你猜对了!答案是 "+ secretNumber);break;}attemptsLeft--;System.out.println("剩余猜测次数:"+ attemptsLeft);}if(attemptsLeft ==0){System.out.println("很遗憾,你没有在10次内猜对。正确答案是 "+ secretNumber);}}}
A - Three Threes
题目大意:给你一个整数n,将这个数n输出n次。
呃呃 B - Pentagon
题目大意:给你一个正五边形ABCDE,给你任意两条边,判断是否相等 主要问题要判断一下内边:AD,AC,…
切片与数组类似,但更强大和灵活。与数组一样,切片也用于在单个变量中存储相同类型的多个值。然而,与数组不同的是,切片的长度可以根据需要增长和缩小。在 Go 中,有几种创建切片的方法: 使用[]datatype{valu…