0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

オフラインどう書く第九回の解答

Posted at

問題はこちら->http://nabetani.sakura.ne.jp/hena/ord9busfare/

乗客のグループわけの記載がないので、全員同行者だとして解いてみました。
年齢区分と料金区分を':'でつなぎ乗客のリストを作ります。
大人一人につき幼児二人をfind/2で料金の高いほうから選びzero/2で削除していきます。
(処理の終わった大人はappendで最後に移り、findの入力と出力が同じになったら終わり)
その後は一人ずつfare/3で料金を求めcalc/5で足していきます。

%begin.
%SWI-Prolog version 7.4.2
%:-initialization(begin).   %ideone

calc(L,N,P,R1,R):-select(P:X,L,L1),fare(X,N,N1),R2 is R1+N1,calc(L1,N,P,R2,R).
calc(L,_,_,R,[L,R]).

zero(['A':X|T],R):-
     find(T,L1),find(L1,L2),append(L2,['A':X],R1),(L1\=L2->zero(R1,R);R=R1).
zero(R,R).

find(L,L1):-select('I':'n',L,L1),!.
find(L,L1):-select('I':'w',L,L1),!.
find(L,L).

w(N,R):-R is ((N div 10 + 1) div 2)*10.

fare('n',N,N).
fare('p',_,0).
fare('w',N,R):-w(N,R).

solve(L,N,R):-
     msort(L,L1),zero(L1,L2),calc(L2,N,'A',0,[L3,S1]),w(N,N1),
     calc(L3,N1,'C',S1,[L4,S2]),calc(L4,N1,'I',S2,[_,R]).

go([]):-!.
go([M,A,B,C|T]):-
     number_string(N,A),atomics_to_string(L,",",B),maplist(cha,L,L1),
     solve(L1,N,R),write(M),write("->"),write(R),number_string(R,R1),
     (R1==C->Str=" ok";Str=" no"),write(" "),writeln(Str),go(T).

cha(S,R):-atom_chars(S,[X,Y]),R=X:Y.
begin:-str(S),split_string(S,":\s\n","\s",L),go(L),!.


str("0  210:Cn,In,Iw,Ap,Iw      170
1       220:Cp,In       110
2       230:Cw,In,Iw    240
3       240:In,An,In    240
4       250:In,In,Aw,In         260
5       260:In,In,In,In,Ap      260
6       270:In,An,In,In,Ip      410
7       280:Aw,In,Iw,In         210
8       200:An  200
9       210:Iw  60
10      220:Ap  0
11      230:Cp  0
12      240:Cw  60
13      250:In  130
14      260:Cn  130
15      270:Ip  0
16      280:Aw  140
17      1480:In,An,In,In,In,Iw,Cp,Cw,In,Aw,In,In,Iw,Cn,Aw,Iw    5920
18      630:Aw,Cw,Iw,An,An      1740
19      340:Cn,Cn,Ip,Ap         340
20      240:Iw,Ap,In,Iw,Aw      120
21      800:Cw,An,Cn,Aw,Ap      1800
22      1210:An,Ip,In,Iw,An,Iw,Iw,An,Iw,Iw      3630
23      530:An,Cw,Cw    810
24      170:Aw,Iw,Ip    90
25      150:In,Ip,Ip,Iw,In,Iw,Iw,In,An,Iw,Aw,Cw,Iw,Cw,An,Cp,Iw  580
26      420:Cn,Cw,Cp    320
27      690:Cw,In,An,Cp,Cn,In   1220
28      590:Iw,Iw,Cn,Iw,Aw,In,In,Ip,Iw,Ip,Aw    1200
29      790:Cw,Cn,Cn    1000
30      1220:In,In,An,An,In,Iw,Iw,In,In,Ip,In,An,Iw     4590
31      570:Cw,Cn,Cp    440
32      310:Cn,Cw,An,An,Iw,Cp,Cw,Cn,Iw  1100
33      910:Aw,In,Iw,Iw,Iw,Iw,Iw,An,Cw,In       2290
34      460:Iw,Cw,Cw,Cn         590
35      240:Iw,Iw,In,Iw,In,In,Cn,In,An  780
36      1240:In,In,In,Ap,In,Cw,Iw,Iw,Iw,Aw,Cw   2170
37      1000:Iw,Ip,In,An,In,In,In,An,In,Iw,In,In,Iw,In,Iw,Iw,Iw,An      5500
38      180:In,Aw,Ip,Iw,In,Aw,In,Iw,Iw,In       330
39      440:In,Ip,Cp,Aw,Iw,In,An        660
40      1270:Ap,In,An,Ip,In,Ip,Ip       1270").
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?