fork download
  1.  
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Main {
  7.  
  8.  
  9. public static void main(String[] args) {
  10.  
  11. try (Scanner scanner = new Scanner(System.in)) {
  12.  
  13. People people = new People();
  14.  
  15. while (true) {
  16.  
  17. String name = scanner.next();
  18.  
  19. if(name.startsWith("0")) {
  20. break;
  21. }
  22.  
  23. people.add(new Person(name, scanner));
  24. }
  25.  
  26. people.print();
  27. }
  28. }
  29. }
  30.  
  31.  
  32. class Person {
  33.  
  34. public final String name;
  35. public final double h;
  36. public final double w;
  37. public final double bmi;
  38.  
  39. private Person next;
  40.  
  41. public Person(String name, Scanner scanner) {
  42.  
  43. this.name = name;
  44. this.h = scanner.nextDouble();
  45. this.w = scanner.nextDouble();
  46. this.bmi = w / ((h / 100) * (h / 100));
  47. }
  48.  
  49. public Person() {
  50.  
  51. this.name = null;
  52. this.h = 0.0;
  53. this.w = 0.0;
  54. this.bmi = 0.0;
  55. }
  56.  
  57. public void updateNext(Person next) {
  58.  
  59. Person p = this.next;
  60.  
  61. this.next = next;
  62. next.next = p;
  63. }
  64.  
  65. public Person getNext() {
  66.  
  67. return next;
  68. }
  69.  
  70. @Override
  71. public String toString() {
  72.  
  73. String f = " BMI > %.1f 身長[cm] > %.1f 体重[kg] > %.1f 名前 > %s.";
  74.  
  75. return String.format(f, bmi, h, w, name);
  76. }
  77. }
  78.  
  79.  
  80. class People {
  81.  
  82. private final Person head = new Person();
  83.  
  84. public void add(Person person) {
  85.  
  86. Person prev = head;
  87. Person next = head.getNext();
  88.  
  89. while (next != null) {
  90.  
  91. if(person.bmi < next.bmi) {
  92. break;
  93. }
  94.  
  95. prev = next;
  96. next = next.getNext();
  97. }
  98.  
  99. prev.updateNext(person);
  100. }
  101.  
  102. public void print() {
  103.  
  104. for (Person p = head.getNext(); p != null; p = p.getNext()) {
  105.  
  106. System.out.println(p);
  107. }
  108. }
  109. }
  110.  
Success #stdin #stdout 0.17s 54768KB
stdin
ああああ 100.0 60.0
いいいい 100.0 50.0
うううう 100.0 70.0
0
stdout
 BMI > 50.0  身長[cm] > 100.0  体重[kg] > 50.0  名前 > いいいい.
 BMI > 60.0  身長[cm] > 100.0  体重[kg] > 60.0  名前 > ああああ.
 BMI > 70.0  身長[cm] > 100.0  体重[kg] > 70.0  名前 > うううう.