class RandomNums def self.generate size dec_nums (0...size).map{|i| rand(0...(size-i))} end private def self.dec_nums nums array = (0...nums.size).to_a nums.map{|i| array.delete_at(i)} end end # input N = gets.to_i A = N.times.map{ gets.to_i } def check_nums nums nums.each.with_index{|num, i| return i if nums[i] == A[i] } return nil end def change_nums nums, index indexs = (0...nums.size).to_a indexs.delete(index) i = indexs.sample nums[index], nums[i] = nums[i], nums[index] end def check_A A.each{|i| return false if i >= N} A.uniq.size == 1 end if check_A p -1 else nums = RandomNums.generate N while (ng = check_nums(nums)) != nil change_nums nums, ng end nums.each{|i| p i } end