gpt4 book ai didi

c - 移动数组中的所有数字

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:55 25 4
gpt4 key购买 nike

实际上,我正在解决 Hackerrank 中的一个挑战。我是编程新手。我面临的挑战是显示对给定整数数组进行排序所需的移位次数。我不明白的是将所有数字转移到排序数组中的方法。有什么简单的方法吗?

最佳答案

假设tempindexcount 是整型变量。 (温度=0 和计数=0)。 array 是我们要排序的数组。 n数组 的长度。如果当前数组元素的值低于先前元素的值,则这些值将交换。每次有交换时,count 加 1。

  for (int c = 1 ; c <= n - 1; c++) {
index= c;

while ( index > 0 && array[index] < array[index-1]) {
temp = array[index];
array[index] = array[index-1];
array[index-1] = temp;
count=count+1;
index--;
}
}

关于c - 移动数组中的所有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48486427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com