描述
已知线性单链表中的元素(整数)以递增有序排列(元素个数用户输入),编写算法删除表中所有大于mink且小于maxk的元素(若表中存在这样的元素),不存在则输出单词Nobeing,如果没有元素了,则输出null
输入
6
1 2 3 4 5 6
2 5
输出
1 2 5 6
参考代码
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)); int n = Integer.parseInt(cin.readLine()); 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 min = Integer.parseInt(sn[0]); int max = Integer.parseInt(sn[1]); Iterator<Integer> it = list.iterator(); boolean has = true; boolean flag = true; while(it.hasNext()){ int val = it.next(); if(!(val > min && val < max)){ flag = false; }else{ has = false; } } if(has){ System.out.println("Nobeing"); }else{ it = list.iterator(); while(it.hasNext()){ int val = it.next(); if(!(val > min && val < max)){ System.out.print(val+" "); flag = false; } } if(flag){ System.out.print("null"); } System.out.println(); } } }
已知线性单链表中的元素(整数)以递增有序排列(元素个数用户输入),编写算法删除表中所有大于mink且小于maxk的元素(若表中存在这样的元素),不存在则输出单词Nobeing,如果没有元素了,则输出null
输入
6
1 2 3 4 5 6
2 5
输出
1 2 5 6
参考代码
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)); int n = Integer.parseInt(cin.readLine()); 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 min = Integer.parseInt(sn[0]); int max = Integer.parseInt(sn[1]); Iterator<Integer> it = list.iterator(); boolean has = true; boolean flag = true; while(it.hasNext()){ int val = it.next(); if(!(val > min && val < max)){ flag = false; }else{ has = false; } } if(has){ System.out.println("Nobeing"); }else{ it = list.iterator(); while(it.hasNext()){ int val = it.next(); if(!(val > min && val < max)){ System.out.print(val+" "); flag = false; } } if(flag){ System.out.print("null"); } System.out.println(); } } }