豚吐露@wiki

問07回答

最終更新:

ohden

- view
管理者のみ編集可


(関数の定義)
len(S): 文字列Sの長さを返す。Sが空文字列のときは0を返す。
first(S): 文宇列Sの先頭の1文字のASCⅡコードを返す。Sが空文字列のときはエラーを返す。
butfrst(S): 文字列Sの先頭の1文字を除いた残りの文字列を返す。Sが空文字列のときはエラーを返す。

comp(A, B)
begin
if len(A)=0 and len(B)=0 then return 0;
if len(A)=0 and len(B)≠0 then return 1;
if len(A)≠0 and len(B)=0 then return -1;
if first(A)<first(B) then return 1;
if first(A)>first(B) then return -1;
return comp(butfirst(A), butfirst(B));
end










comp("11", "101") 処理⑥が実行される。
戻り値は『comp(butfirst("11"), butfirst("101")) → comp("1", "01")』の戻り値となる。
⇒ comp("1", "01") 処理⑤が実行される。
『first(A)>first(B)』で1文字目のASCIIコードの比較を行うため『'1'>'0' ⇒ 31>30』が真となり戻り値は『-1』となる。
『comp("11", "101")』の戻り値は本処理の戻り値となるため、『-1』となる。



更新日: 2009年12月09日 (水) 18時38分36秒
記事メニュー
ウィキ募集バナー