program main implicit none integer::N,i character*2,allocatable::cards(:) integer,allocatable::x(:),y(:) read *,N allocate(cards(N),x(N),y(N)) read *,cards do i=1,N x(i) = mark2num(cards(i)) end do y = qsort(x) do i=1,N cards(i) = num2mark(y(i)) end do do i=1,N-1 write(*, '(a," ")',advance='no') cards(i) end do print '(a)',cards(N) contains function mark2num(c) result(num) character*2::c integer::num num = 0 select case(ICHAR(c(1:1))) case (ICHAR('D')) num = num case (ICHAR('C')) num = num + 14 case (ICHAR('H')) num = num + 28 case (ICHAR('S')) num = num + 42 end select select case(ICHAR(c(2:2))) case (ICHAR('A')) num = num + 1 case (ICHAR('T')) num = num + 10 case (ICHAR('J')) num = num + 11 case (ICHAR('Q')) num = num + 12 case (ICHAR('K')) num = num + 13 case default num = num + (ICHAR(c(2:2))-ICHAR('0')) end select end function mark2num function num2mark(num) result(c) character*2::c integer::num select case(num/14) case (0) c(1:1) = 'D' case (1) c(1:1) = 'C' case (2) c(1:1) = 'H' case (3) c(1:1) = 'S' end select select case(MOD(num,14)) case (1) c(2:2) = 'A' case (10) c(2:2) = 'T' case (11) c(2:2) = 'J' case (12) c(2:2) = 'Q' case (13) c(2:2) = 'K' case default c(2:2) = CHAR(ICHAR('0')+MOD(num,14)) end select end function num2mark recursive function qsort(x) result(y) integer,intent(in) ::x(:) integer,allocatable::y(:) integer::pivot,total total = size(x) if (total <=1) then y = x else pivot = x(total/2) y = [qsort(pack(x, x .lt. pivot)), & pack(x, x .eq. pivot), & qsort(pack(x, x .gt. pivot))] endif end function qsort end program main