首页 技术 正文
技术 2022年11月22日
0 收藏 948 点赞 4,270 浏览 1449 个字

Suppose you have an array of N elements, containing three distinct keys, “true”, “false”, and “maybe”. Given an O(N)O(N) algorithm to rearrange the list so that all “false” elements precede “maybe” elements, which in turn precede “true” elements. You may use only constant extra space.

Format of functions:

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

where ElementType A[] contains the N elements.

Sample program of judge:

#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 */

Sample Input:

62 2 0 1 0 0

Sample Output:

false in A[0]-A[0]maybe in A[1]-A[2]true in A[3]-A[5]
////  main.c//  Sort Three Distinct Keys////  Created by 余南龙 on 2016/12/9.//  Copyright © 2016年 余南龙. All rights reserved.//void MySort( ElementType A[], int N ){    int T[N], F[N], M[N];    , j = , k = , index;    ; index < N; index++){        if(true == A[index]){            i++;        }        else if(false == A[index]){            j++;        }        else if(maybe == A[index]){            k++;        }    }    ; index < j; index++){        A[index] = false;    }    for( ; index - j< k; index++){        A[index] = maybe;    }    for( ; index - k - j < i; index++){        A[index] = true;    }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918