input
在文件random.in里边存放两行数
第一行一个数为第二行数的个数
第二行为随机数
output
在文件random.out里边存放两行数
第一行一个数为去重后第二行数的个数
第二行为input里边第二行去重的而且从小到大排序的随机数
input (random.in)
22
20 40 32 67 40 20 89 300 400 15 27 67 300 400 12 17 21 1 2 3 4 5
output (randim.out)
17
1 2 3 4 5 12 15 17 20 21 27 32 40 67 89 300 400
程序:
#-*- coding: utf-8 -*- def get_sort_data(rand_num,num): cp_num=num[:] replace_num=[] i=0 k=0 while(i<rand_num): #提取出input文件中第二行,有相同的值 first_num = num[i] j=i+1 while(j<rand_num): if num[j] == first_num: replace_num.append(first_num) j = j+1 i = i+1 while (k<len(replace_num)): #将有重复值,去除 cp_num.remove(replace_num[k]) k = k+1 cp_num.sort() num = cp_num[:] # print(len(num)) # print(num) tem = '%d'%len(num) output_file = open("C:\\Users\\sxf\\Desktop\\random.out", "w") output_file.write(tem+"\n") p = 0 while (p<len(num)): temp_value = '%d '%num[p] #将整数转化为字符串 output_file.write(temp_value) p = p+1 output_file.close() if __name__=='__main__': input_file = open("C:\\Users\\sxf\\Desktop\\random.in","r") i=0 for line in input_file: if i==0: rand_num=int(line) else: str = line i = i+1 str_num = str.split() j=0 num=[] while(j<len(str_num)): num.append(int(str_num[j])) j = j+1 get_sort_data(rand_num,num) input_file.close()