题意:
n个数,要保证这n个数完全不相同,求需要把原来的数增加多少,求这个值得最小值
Java 程序
import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class B546 { static void run(){ Scanner in = new Scanner(System.in); PrintStream out = System.out; int n = in.nextInt(); int[] a = new int[n]; Set set = new TreeSet(); for(int i=0;i<n;i++){ a[i] = in.nextInt(); } Arrays.sort(a); int count = 0; for(int i=0;i<n;i++){ while(set.contains(a[i])){ a[i]++; count++; } set.add(a[i]); } out.println(count); } public static void main(String[] args){ run(); } }
Python程序
def B546(): n = int(raw_input()) a = map(int, raw_input().split()) d = {} # dict count = 0 for i in range(n): while str(a[i]) in d : a[i]+=1 count+=1 d[str(a[i])] = 1 #print d print count if __name__=='__main__': B546()
上面的d是一个字典,和上面的TreeSet集合差不多的,都是不允许有重复元素出现的。。。