描述
编写一个算法,从顺序表中删除自第i个元素开始的k个元素,顺序表输入9个元素。(例如从三个开始,不含第三个元素)
输入
1 2 3 4 5 6 7 8 9
0 0
输出
1 2 3 4 5 6 7 8 9
参考代码
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class Main { public static void main(String[] args) throws IOException { BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); String ds[] = cin.readLine().split(" "); List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i < ds.length;i ++){ list.add(Integer.parseInt(ds[i])); } Collections.sort(list); String sn[] = cin.readLine().split(" "); int st = Integer.parseInt(sn[0]); int cnt = Integer.parseInt(sn[1]); while(cnt > 0 && st < list.size()){ list.remove(st); cnt --; } Iterator<Integer> it = list.iterator(); while(it.hasNext()){ System.out.print(it.next()+" "); } System.out.println(); } }
编写一个算法,从顺序表中删除自第i个元素开始的k个元素,顺序表输入9个元素。(例如从三个开始,不含第三个元素)
输入
1 2 3 4 5 6 7 8 9
0 0
输出
1 2 3 4 5 6 7 8 9
参考代码
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class Main { public static void main(String[] args) throws IOException { BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); String ds[] = cin.readLine().split(" "); List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i < ds.length;i ++){ list.add(Integer.parseInt(ds[i])); } Collections.sort(list); String sn[] = cin.readLine().split(" "); int st = Integer.parseInt(sn[0]); int cnt = Integer.parseInt(sn[1]); while(cnt > 0 && st < list.size()){ list.remove(st); cnt --; } Iterator<Integer> it = list.iterator(); while(it.hasNext()){ System.out.print(it.next()+" "); } System.out.println(); } }