48 lines
1.6 KiB
Plaintext
48 lines
1.6 KiB
Plaintext
···
|
|
http://rosettacode.org/wiki/Dinesman's_multiple-dwelling_problem
|
|
···
|
|
import java.util.HashSet
|
|
|
|
■ Dinesman
|
|
§ static
|
|
|
|
houses⦂ HashSet⟨String⟩°
|
|
|
|
▶ main
|
|
• args⦂ String[]
|
|
· Baker, Cooper, Fletcher, Miller, and Smith …
|
|
build *StringBuilder°, *StringBuilder "BCFMS"
|
|
∀ house ∈ houses⦂ String
|
|
if verify house
|
|
System.out.println house.toString°
|
|
|
|
▶ verify⦂ boolean
|
|
• house⦂ String
|
|
· Baker does not live on the top floor.
|
|
return false if house.charAt 4 = 'B'
|
|
· Fletcher does not live on either the top or the bottom floor.
|
|
return false if house.charAt 0 = 'F' or house.charAt 4 = 'F'
|
|
· Cooper does not live on the bottom floor.
|
|
return false if house.charAt 0 = 'C'
|
|
· Miller lives on a higher floor than does Cooper.
|
|
return false if house.indexOf "M" ≤ house.indexOf "C"
|
|
· Smith does not live on a floor adjacent to Fletcher's.
|
|
return false if Math.abs (house.indexOf "S") - (house.indexOf "F") = 1
|
|
· Fletcher does not live on a floor adjacent to Cooper's.
|
|
return false if Math.abs (house.indexOf "F") - (house.indexOf "C") = 1
|
|
return true
|
|
|
|
▶ build
|
|
• house⦂ StringBuilder
|
|
• people⦂ StringBuilder
|
|
if people.length° = 0
|
|
houses.add house.toString°
|
|
else
|
|
∀ i ∈ 0…people.length°
|
|
person⦂ char: people.charAt i
|
|
house.append person
|
|
people.deleteCharAt i
|
|
build house, people
|
|
people.insert i, person
|
|
house.setLength house.length° - 1
|