USACO 1.2.3 delete duplicate items in sorted array JAVA

The question is very short and the solution is also very convenient; this is also a question that you will not know what to do if you just start to study the question, and you need to be patient and carefully look at the information. Take a look at the conditions, the array is sorted, and the solution can be easily obtained.

I think it's just an output length, so I simply counted the lengths of different numbers in the array without performing any delete operations, and it was done on PTA.

From a respectful point of view and an honest attitude, I symbolically deleted the answer after outputting it, denoted by -1.

package delDulplicate;

import java.util.Scanner;

//输入数据到数组
//输入的数据本来就是排好序的
//如果当前位和前一位一样 后面提上来

public class Main {
    
    
	static final int MAXN= 110;
	static int n;
	static int[] array= new int[MAXN];
	public static void main(String[] args) {
    
    
		Scanner console= new Scanner(System.in);
		n= console.nextInt();
		for(int i=0; i<n; i++) {
    
    
			array[i]= console.nextInt();
		}
		
		int count=1;
		for(int i=0; i<n-1; i++) {
    
    
			if(array[i]!=array[i+1])
				count++;
		}
		
		System.out.println(count);
		
		//执行重复删除操作
		for(int i=n-1; i>=1; i--) {
    
    
			if(array[i]==array[i-1])
				move(i);
		}
		
		for(int i=count; i<n; i++) {
    
    
			array[i]= -1;
		}
		
//		for(int i=0; i<n; i++) {
    
    
//			System.out.println(array[i]);
//		}
		
	}
	
	//将i之后的都向前移动一位
	public static void move(int i) {
    
    
		for(int j=i; j<n-1; j++) {
    
    
			array[j]= array[j+1];
		}
	}

}

Guess you like

Origin blog.csdn.net/roswellnotfound/article/details/108930680