[PTA] Sort Three Distinct Keys

#include <stdio.h>
#include <stdlib.h>

typedef enum { true, false, maybe } Keys;
typedef Keys ElementType;

void Read( ElementType A[], int N ); /* details omitted */

void MySort( ElementType A[], int N );

void PrintA( ElementType A[], int N )
{
    int i, k;

    k = i = 0;
    for ( ; i<N && A[i]==false; i++ );
    if ( i > k )
        printf("false in A[%d]-A[%d]\n", k, i-1);
    k = i;
    for ( ; i<N && A[i]==maybe; i++ );
    if ( i > k )
        printf("maybe in A[%d]-A[%d]\n", k, i-1);
    k = i;
    for ( ; i<N && A[i]==true; i++ );
    if ( i > k )
        printf("true in A[%d]-A[%d]\n", k, i-1);
    if ( i < N )
        printf("Wrong Answer\n");
}

int main()
{
    int N;
    ElementType *A;

    scanf("%d", &N);
    A = (ElementType *)malloc(N * sizeof(ElementType));
    Read( A, N );
    MySort( A, N );
    PrintA( A, N );
    return 0;
/* Your function will be put here */
}

void Read( ElementType A[], int N )
{
	int i;
	for(i=0;i<N;i++){
		scanf("%d",&A[i]);
	}
	return;
}

void MySort( ElementType A[], int N )
{
	int i;
	int count1,count2,count3;
	count1=count2=count3=0;
	for(i=0;i<N;i++){
		if(A[i]==false){
			count1++;
		}
		else if(A[i]==maybe){
			count2++;
		}
		else if(A[i]==true){
			count3++;
		}
	}
	for(i=0;i<count1;i++){
		A[i]=false;
	}
	for(i=count1;i<count1+count2;i++){
		A[i]=maybe;
	}
	for(i=count1+count2;i<N;i++){
		A[i]=true;
	}
	return;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值