[PTA]Week8 Sort Three Distinct Keys
2015-11-15 10:28
253 查看
看了很久题目也没看懂啥意思,莫名其妙就过了,我猜这大概是传说中桶排序 → →
#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; }
相关文章推荐
- Altium Designer应用技巧5: Altium Live账号的获得
- 读取本机电话簿联系人信息返回一个list
- CodeForces 482A Diverse Permutation
- Preamble
- 四到五年的程序员怎么突破?
- struts 提交form表单
- Docker(一):什么是docker
- 005-材质和着色器 srt字幕文件
- (数学)波斯公主选驸马
- VS2013等版本中怎么写函数注释可以鼠标悬停自动显示
- 体悟小节
- Socket心跳包异常检测的C语言实现,服务器与客户端代码案例
- linux学习笔记(8):cd命令
- linux学习笔记(7):cat命令详解
- C# DateTimePicker控件如何精确设置显示时分秒
- Using DOM parsing XML document
- Android 完全掌握Service
- java.awt.EventQueue.invokeLater(new Runnable(){public void run(){}})(二)
- jsp:usebean 常用注意事项
- 为什么写代码的时候听音乐