华为机试HJ34图片整理
题目:
想法:
将输入的字符串中每个字符都转为ASCII码,再通过快速排序进行排序并输出
input_str = input()
input_list = [int(ord(l)) for l in input_str]
def partition(arr, low, high):
i = low - 1
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[j], arr[i] = arr[i], arr[j]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi-1)
quick_sort(arr, pi+1, high)
return arr
result = quick_sort(input_list, 0, len(input_list)-1)
result_str = ""
for r in result:
result_str += chr(r)
print(result_str)