c语言:输入一个五位整数将数中的五个数值进行从大到小的顺序排序形成一个新的五位整数并输出这个整数。

要求:用函数调用
2025-12-04 03:46:39
推荐回答(2个)
回答1:

根据题意得到如下代码:

#include 

int getMaxFive(int n)
{
    int a[5], i, j, k = 0, t;
    while (n!=0){
        a[k] = n%10;
        n /= 10;
        k++;
    }
    if (k != 5)return -1;
    for (i = 0; i < k; ++i){
        for (j = 0; j < k-i-1; ++j){
            if (a[j] < a[j+1]){
                t = a[j], a[j] = a[j+1], a[j+1] = t;
            }
        }
    }
    t = 0;
    for (i = 0; i < k; ++i){
        t = t*10 + a[i];
    }
    return t;
}

int main()
{
    int e = getMaxFive(82731);
    printf ("%d\n", e);
    return 0;
}

回答2:

#include 
struct student
{
    char name[15];
    struct student *next;
};
 
struct student *link(struct student *a, struct student *b) {
    struct student *p = a;
    while (p->next)
        p = p->next;
    p->next = b; // p->next = b->next; 这里你确定链表是有头指针还是没有头指针吗?
    return a;
}
 
int main() {
    struct student stu1, stu2;
    stu1.next = NULL;
    stu2.next = NULL;
    scanf("%s %s", stu1.name, stu2.name);
    printf("%s", link(&stu1, &stu2));
}