Algorithm training Delete Array 0 element time limit: 1.0s memory limit: 512.0MB
Reads n integers from the keyboard into the array, writes the function compactintegers, deletes all elements with a value of 0 in the array, and then moves the elements to the first end of the array. Note that the Compactintegers function needs to accept the array and its number of elements as arguments, and the function return value should be the number of new elements of the array after the delete operation executes. Outputs the number of elements in the array after deletion and sequentially outputs the array elements.
Sample input: (Input format Description: 5 is the number of input data, 3 4 0 0 2 is a space-separated 5 integers)
3 4 0) 0 2
Sample output: (Output Format Description: 3 is the number of non-0 data, 3 4 2 is a space-separated 3 non-zero integers)
3
3 4 2
7
0 0 7 0 0 9 0
Sample output:
2
7 9
3
0 0 0
Sample output:
0
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); while(Sc.hasnext ()) {int[] arr =New int[10010]; intn =Sc.nextint (); for(inti = 0; I < n; i++) {Arr[i]=Sc.nextint (); } intLen =compactintegers (arr, n); System.out.println (len); for(inti = 0; i < Len; i++) { if(i = = len-1) System.out.println (Arr[i]); ElseSystem.out.print (Arr[i]+ " "); }} sc.close (); } Private Static intCompactintegers (int[] arr,intN) { intpos = 0; for(inti = 0; I < n; i++) { if(Arr[i] = = 0) { Continue; } Else{Arr[pos]=Arr[i]; POS++; } } returnPOS; }}
ALGO-79 Deleting an array of 0 elements