输入一个五位整数,对此整数中的五个数值进行从大到小的顺序排序,形成一个新的五位整数,并输出这个整数。要求:用C语言函数调用。并且写上每一行代码的注释
时间: 2023-02-18 13:44:56 浏览: 103
。
/* 定义一个五位整数变量 */
int num;
/* 读取五位整数 */
scanf("%d", &num);
/* 对五位整数中的数字进行拆分 */
int a = num / 10000;
int b = num % 10000 / 1000;
int c = num % 1000 / 100;
int d = num % 100 / 10;
int e = num % 10;
/* 对五个数字进行排序,从大到小 */
int temp;
if (a < b) {
temp = a;
a = b;
b = temp;
}
if (a < c) {
temp = a;
a = c;
c = temp;
}
if (a < d) {
temp = a;
a = d;
d = temp;
}
if (a < e) {
temp = a;
a = e;
e = temp;
}
if (b < c) {
temp = b;
b = c;
c = temp;
}
if (b < d) {
temp = b;
b = d;
d = temp;
}
if (b < e) {
temp = b;
b = e;
e = temp;
}
if (c < d) {
temp = c;
c = d;
d = temp;
}
if (c < e) {
temp = c;
c = e;
e = temp;
}
if (d < e) {
temp = d;
d = e;
e = temp;
}
/* 将排序后的五个数字重新组成一个五位整数 */
int new_num = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
/* 输出新的五位整数 */
printf("%d", new_num);
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)