C语言 整数分离并排序

C 语言问题
怎么将一个int 整数里面的数字拆开,然后根据奇偶数排序呢
比如231840 让偶数在前面要输出成 284031
考虑过用 两个for循环依次往下换位

逐位分离后,分别存入两个数组,再输出就可以了。
如果分离后不再排序,代码如下:

img

#include <stdio.h>


int main()
{
    int a[20],b[20],na=0,nb=0;
    int n,t,i;
    scanf("%d",&n);
    while(n)
    {
        t = n%10;
        if(t%2==0)
            a[na++]=t;
        else
            b[nb++]=t;
        n/=10;
    }
    
    //输出
    for (i=na-1;i>=0;i--)
        printf("%d",a[i]);
    for(i=nb-1;i>=0;i--)
        printf("%d",b[i]);
    
    return 0;
}

如果分离后,奇数偶数分别按照从大到小的顺序排序,代码如下:

img

#include <stdio.h>

//冒泡排序,flag=0表示从小到大,flag=1表示从大到小
void bubble_sort(int a[],int n,int flag)
{
    int i,j,t;
    for (i=0;i<n-1;i++)
    {
        for (j=0;j<n-1-i;j++)
        {
            if(flag ==0 && a[j] > a[j+1])  //从小到大,升序
            {
                t = a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }else if (flag==1 && a[j]<a[j+1])
            {
                t = a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
}

int main()
{
    int a[20],b[20],na=0,nb=0;
    int n,t,i;
    scanf("%d",&n);
    while(n)
    {
        t = n%10;
        if(t%2==0)
            a[na++]=t;
        else
            b[nb++]=t;
        n/=10;
    }
    bubble_sort(a,na,1);//偶数从大到小
    bubble_sort(b,nb,1);//奇数从大到小
    //输出
    for (i=0;i<na;i++)
        printf("%d",a[i]);
    for(i=0;i<nb;i++)
        printf("%d",b[i]);
    
    return 0;
}

来个不用数组的
你题目的解答代码如下:

#include <stdio.h>

int main()
{
    int n,a=0,b=0,t,ra=1,rb=1;
    scanf("%d",&n);
    do {
        t = n%10;
        if(t%2==0) {
            a += t*ra;
            ra *= 10;
        } else {
            b += t*rb;
            rb *= 10;
        }
        n /= 10;
    } while(n>0);
    n = a*rb+b;
    printf("%d",n);
    return 0;
}

img

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

#include <stdio.h>
#include <string.h>

#define N 100

void swap(char *a, char *b)
{
    char t = *a;
    *a = *b;
    *b = t;
}

void sort(char *s)
{
    size_t n = strlen(s);
    for (size_t i = 0; i < n - 1; i++)
    {
        for (size_t j = 0; j < n - i - 1; j++)
        {
            if ((s[j] - '0') % 2 != 0 && (s[j + 1] - '0') % 2 == 0)
                swap(&s[j], &s[j + 1]);
        }
    }
}

int main()
{
    char s[N];
    scanf("%s", s);
    sort(s);
    printf("%s\n", s);
    return 0;
}

#include <stdio.h>

int main()
{
    int a[20],b[20]; 
    int num,rem,j=0,k=0;
    scanf_s("%d", &num);
    do
    { 
        if ((rem = num % 10) % 2 == 0)
            a[j++] = rem;
        else
            b[k++] = rem;
    } while (num = num / 10);
    while(j)
        printf("%d", a[--j]);
    while(k)
        printf("%d", b[--k]);
    return 0;
}
#include <stdio.h>

int main()
{
    int a[20],b[20]; 
    int num,rem,j=0,k=0;
    scanf_s("%9d", &num);
    do
    { 
        if ((rem = num % 10) % 2 == 0)
            a[j++] = rem;
        else
            b[k++] = rem;
    } while (num = num / 10);
    while(j)
        printf("%d", a[--j]);
    while(k)
        printf("%d", b[--k]);
    return 0;
}


您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632