本帖最后由 bin554385863 于 2019-10-30 20:45 编辑 #include <stdio.h>
void InputArray(int arr[], size_t arr_size) //创建数组
{
for (size_t i = 0; i < arr_size; i++)
{
scanf("%d", &arr[i]);
}
}
void SortArray(int arr[], size_t arr_size) //冒泡
{
for (size_t i = 0; i < arr_size; i++)
{
for (size_t j = 0; j < i; j++)
{
if (arr[j] > arr[i])
{
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
}
void outputArray(int arr[], int arr_size, char flag) //输出
{
if ((flag != '+') && (flag != '-'))
{
printf("Parameter Error");
}
else if (flag == '+') //+号表示正序输出;
{
for (size_t i = 0; i < arr_size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
else if (flag == '-')
{
for (size_t j = arr_size -1; j != -1; j--) //-号代表反序
{
printf("%d ", arr[j]);
}
printf("\n");
}
}
void reverse(int arr[], int arr_size)//翻转数组
{
for (size_t i = arr_size -1; i != -1; i--)
{
int tmp = arr[i];
arr[arr_size -1-i] = tmp;
}
}
int main(int argc, char const *argv[])
{
int arr[5] = {0};
InputArray(arr, 5);
SortArray(arr, 5);
printf("UP\n");
outputArray(arr, 5, '+');
printf("DOWN\n");
outputArray(arr, 5, '-');
return 0;
}
---------------------------------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-fjbfyzrh.lgp --stdout=Microsoft-MIEngine-Out-lpm5sbkr.0tp --stderr=Microsoft-MIEngine-Error-1gv22jtx.vuy --pid=Microsoft-MIEngine-Pid-tyuk4jfi.rnc "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
E:\Users\86184\Documents\Code>cmd /C "c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ytznha1m.a3y --stdout=Microsoft-MIEngine-Out-mr5tkgrt.ugy --stderr=Microsoft-MIEngine-Error-jey2wj1x.zvl --pid=Microsoft-MIEngine-Pid-dxs4cjcs.qee "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi "
6 5 3 2 0
UP
0 2 3 5 6
DOWN
6 5 3 2 0
E:\Users\86184\Documents\Code>
============================================
其实反序输出只要排序号的数组尾部遍历就行 |