标题:C语言编程…数组排列问题……求帮忙
只看楼主
l18896916690
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-5-6
结帖率:0
已结贴  问题点数:20 回复次数:4 
C语言编程…数组排列问题……求帮忙
编写程序,从键盘上读取10个数字存放到a数据中,然后对a数组中的10个元素,分别对其中的偶数进行从小到大排列,对其中的奇数从大到小排列,然后展现在屏幕上。要求:排序不影响数组中元素内容的奇偶性,即原来位置上是偶数,依然是偶数,是奇数,仍然是奇数。
搜索更多相关主题的帖子: 编写程序 C语言 奇偶性 键盘 影响 
2016-05-06 13:28
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
得分:7 
程序代码:
#include<stdio.h>
#include<string.h>

main()
{
    int a[10] = { 0 }, flagou[10] = {0}, flagji[10] = { 0 };
    int i = 0, j = 0;
    int maxou = 0, maxji = 0;
    int count = 0;
    printf("请输入十个整数:\n");
    for (i = 0; i < 10; i++)
    {
        scanf("%d",&a[i]);
        if (a[i] % 2 == 0)
        {
            flagou[maxou++] = i;
        }
        else 
        {
            flagji[maxji++] = i;
        }
    }
    for (i = 0; i < maxou-1; i++)
    {
        for (j = 0; j < maxou-1; j++)
        {
            if (a[flagou[j]] > a[flagou[j + 1]])
            {
                count = a[flagou[j]];
                a[flagou[j]] = a[flagou[j + 1]];
                a[flagou[j + 1]] = count;
            }
        }
    }

    for (i = 0; i < maxji - 1; i++)
    {
        for (j = 0; j < maxji - 1; j++)
        {
            if (a[flagji[j]] > a[flagji[j + 1]])
            {
                count = a[flagji[j]];
                a[flagji[j]] = a[flagji[j + 1]];
                a[flagji[j + 1]] = count;
            }
        }
    }

    printf("排列后:\n ");
    for (i = 0; i < 10; i++)
    {
        printf("%d ", a[i]);        
    }
    printf("\n");
    return 0;
}

2016-05-06 16:07
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
得分:0 
简化一下:
程序代码:
#include<stdio.h>
#include<string.h>

void sort(int a[], int flag[], int count);

int main()
{
    int a[10] = { 0 }, flagou[10] = {0}, flagji[10] = { 0 };
    int i = 0, j = 0;
    int maxou = 0, maxji = 0;
    int count = 0;
    printf("请输入十个整数:\n");
    for (i = 0; i < 10; i++)
    {
        scanf("%d",&a[i]);
        if (a[i] % 2 == 0)
        {
            flagou[maxou++] = i;
        }
        else 
        {
            flagji[maxji++] = i;
        }
    }

    sort(a, flagou, maxou);
    sort(a, flagji, maxji);

    printf("排列后:\n ");
    for (i = 0; i < 10; i++)
    {
        printf("%d ", a[i]);        
    }
    printf("\n");
    return 0;
}

void sort(int a[], int flag[], int n)
{
    int i = 0, j = 0, count = 0;
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - 1; j++)
        {
            if (a[flag[j]] > a[flag[j + 1]])
            {
                count = a[flag[j]];
                a[flag[j]] = a[flag[j + 1]];
                a[flag[j + 1]] = count;
            }
        }
    }

}
2016-05-06 16:14
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
得分:7 
#include<stdio.h>
int main()
{
    int a[10];
    int i,j,min,max;
    printf("输入数组a:");
    for(i=0;i<10;i++)
    scanf("%d",&a[i]);
    for(i=0;i<10;i++)
    {
        if(a[i]%2==0)
        {
            for(j=i+1;j<10;j++)
            {
                if(a[j]%2==0)
                {
                    if(a[i]>a[j])
                    {
                        min=a[i];
                        a[i]=a[j];
                        a[j]=min;
                    }
                }
               
            }
        }
    }
    for(i=0;i<10;i++)
    {
        if(a[i]%2!=0)
        {
            for(j=i+1;j<10;j++)
            {
                if(a[j]%2!=0)
                {
                    if(a[i]<a[j])
                    {
                        max=a[i];
                        a[i]=a[j];
                        a[j]=max;
                    }
                }
               
            }
        }
    }
    printf("输出排序后的数组a:\n");
    for(i=0;i<10;i++)
    printf("%d  ",a[i]);
    return 0;
}
2016-05-06 17:24
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:7 
程序代码:
#include<stdio.h>

#define ASCENDING   0 
#define DESCENDING  1
#define ODDNUMBER   0
#define EVENNUMBER  1

void _Sort(int n[], int m, int parity, int order)
{
    for (int i=0; i<m; i++)
    {
        if ((parity == ODDNUMBER) ? (n[i]%2 != 0) : (n[i]%2 == 0))
        {
            for (int j=i+1; j<m; j++)
            {
                if ((parity == ODDNUMBER) ? (n[j]%2 != 0) : (n[j]%2 == 0))
                {
                    if ((order == ASCENDING) ? (n[i] > n[j]) : (n[i] < n[j]))
                    {
                        int tmp = n[j];
                        n[j] = n[i];
                        n[i] = tmp;
                    }
                }
            }
        }
    }
}

main()
{
    //int n[] = {1,8,3,6,5,4,7,10,9,2};
    int n[] = {1,1,3,8,8,6,3,6,5,5,2,4,7,7,9,10,2,9};
    int m = sizeof(n)/sizeof(int);
    for (int i=0; i<m; i++)
        printf("%d ", n[i]);
    printf("\n");
    _Sort(n, m, ODDNUMBER, DESCENDING);
    _Sort(n, m, EVENNUMBER, ASCENDING);
    for (int i=0; i<m; i++)
        printf("%d ", n[i]);
}
2016-05-06 23:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-464461-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021190 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved