从键盘输入十个整数 将其由小到大排序后输入
时间: 2023-12-12 17:04:09 浏览: 43
好的,这是一个典型的排序问题,我可以为您提供一个简单的解决方案。以下是使用C++编写的代码示例:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[10];
// 从键盘输入十个整数
cout << "请输入十个整数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> arr[i];
}
// 将整数由小到大排序
sort(arr, arr + 10);
// 输出排序后的结果
cout << "排序后的结果为:" << endl;
for (int i = 0; i < 10; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
这个程序会先让你从键盘输入十个整数,然后使用STL中的sort函数将它们从小到大排序,最后输出排序后的结果。希望这个代码示例对您有所帮助!
相关问题
利用指针从键盘输入十个整数,并按选择法由小到大排序
以下是C语言代码实现从键盘输入十个整数,并按照选择排序法由小到大排序:
```c
#include <stdio.h>
void selection_sort(int *arr, int n);
int main() {
int arr[10];
printf("请输入十个整数:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
selection_sort(arr, 10);
printf("排序后的结果为:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
void selection_sort(int *arr, int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
if (min_index != i) {
int temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
}
```
程序首先定义一个长度为10的整数数组arr,并使用循环从键盘输入10个整数。然后,程序调用名为selection_sort的函数,该函数使用选择排序法对数组进行排序。选择排序法的基本思路是从数组中选择最小的元素并将其放在第一位,然后从剩余的元素中再选择最小的元素并将其放在第二位,以此类推,直到整个数组按升序排列。在C语言中,可以通过指向数组元素的指针来访问和修改数组元素。因此,我们将数组作为指向整数的指针传递给selection_sort函数。
排序完成后,程序打印排序后的结果。
利用指针从键盘输入10个整数,并按选择法由小到大排序
好的,下面是代码实现:
```c
#include <stdio.h>
void selection_sort(int *arr, int n) {
int i, j, minIndex, tmp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (*(arr + j) < *(arr + minIndex)) {
minIndex = j;
}
}
if (minIndex != i) {
tmp = *(arr + i);
*(arr + i) = *(arr + minIndex);
*(arr + minIndex) = tmp;
}
}
}
int main() {
int arr[10];
int *p = arr;
int i;
printf("请输入10个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", p + i);
}
selection_sort(p, 10);
printf("排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", *(p + i));
}
printf("\n");
return 0;
}
```
这里定义了一个`selection_sort`函数,用于实现选择法排序,接受一个指向整型数组的指针和数组的长度作为参数。
在`main`函数中,定义了一个长度为10的整型数组`arr`和一个指向`arr`的指针`p`。通过循环,从键盘输入10个整数,使用指针`p`操作数组元素,并调用`selection_sort`函数对数组进行排序。最后输出排序后的结果。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)