結果
| 問題 |
No.355 数当てゲーム(2)
|
| コンテスト | |
| ユーザー |
jj
|
| 提出日時 | 2017-01-08 06:45:42 |
| 言語 | Fortran (gFortran 14.2.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,767 bytes |
| コンパイル時間 | 323 ms |
| コンパイル使用メモリ | 35,708 KB |
| 実行使用メモリ | 25,452 KB |
| 平均クエリ数 | 25.96 |
| 最終ジャッジ日時 | 2024-07-16 12:12:39 |
| 合計ジャッジ時間 | 4,883 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 44 RE * 8 |
ソースコード
program main
implicit none
integer::n(1:4),m(1:4)
integer::hit,blow,phit,pblow,i,j
data n/0,1,2,3/
call get(n,hit,blow)
if(hit.eq.4) return
! selection
if(hit+blow.ne.4) then
do i=1,4
phit = hit
pblow = blow
m = n
do j=0,9
if(ANY(n.eq.j)) cycle
m(i) = j
call get(m,hit,blow)
if(hit+blow.eq.4) then
n = m
goto 10
else if(hit+blow.gt.phit+pblow) then
n = m
exit
end if
end do
end do
end if
10 continue
! shuffle
m = n
n = qsort(m)
do
call get(n,hit,blow)
if(hit.eq.4) then
return
end if
call next_permutation(n)
end do
contains
subroutine get(n,hit,blow)
integer::n(1:4)
integer::hit,blow
print '(i0," ",i0," ",i0," ",i0)',n
read *,hit,blow
end subroutine get
subroutine next_permutation(array)
implicit none
integer::array(:),d,i,j,k,swap
d = 4
do i=d,2,-1
if(array(i-1).lt.array(i)) exit
end do
do j=d,i+1,-1
if(array(i-1).lt.array(j)) exit
end do
swap = array(i-1)
array(i-1) = array(j)
array(j) = swap
do k=0, (d-1-i)/2
swap = array(i+k)
array(i+k) = array(d-k)
array(d-k) = swap
end do
end subroutine next_permutation
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
jj