1、循环在str1中找到与str2相同的字符下标(是对应str1下标不是str2),在根据下标删除下标位置的字符。
//标题:删除公共字符 | 时间限制:1秒 | 内存限制:32768K | 语言限制: 不限 //【删除公共字符】输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符 //串变成”Thy r stdnts.” //输入描述:每个测试输入包含2个字符串 //输出描述:输出删除后的字符串 //示例1: //输入:They are students. aeiou //输出:Thy r stdnts. public class Day1_2 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String str1=scanner.nextLine(); String str2=scanner.nextLine(); for(int i=0;i<str2.length();i++){ int index=0; while((index=str1.indexOf(str2.charAt(i)))!=-1){ str1=str1.substring(0,index)+str1.substring(index); } } System.out.println(str1); } }
2、首先要先排序,再n次循环有一个下标公式:intdex=array.length-2*(i+1)
//【组队竞赛】牛牛举办了一次编程比赛,参加比赛的有3*n个选手,每个选手都有一个水平值a_i. // 现在要将这些选手进行组队,一共组成n个队伍,即每个队伍3人.牛牛发现队伍的水平值等于该队伍队员中第二高水平值。 /* 如果方案为: team1:{1,2,5}, team2:{5,5,8}, 这时候水平值总和为7. 而如果方案为: team1:{2,5,8}, team2:{1,5,5}, 这时候水平值总和为10. 没有比总和为10更大的方案,所以输出10. */ public class Day1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int[] array = new int[3 * n]; int sum = 0; for (int i = 0; i < 3 * n; i++) { array[i] = scanner.nextInt(); } Arrays.sort(array); for (int i = 0; i < n; i++) { int index = array.length - 2 * (i + 1);//一定 sum += array[index]; } System.out.println(sum); } } }