数字之战

时间:2015-08-18 21:05:24

标签: java

大家好我是Java的新手,我有一个示例问题。我也有自己的解决方案。有没有其他更好的方法来解决这个问题?

在遥远的王国,有数字战。在那个王国中的国王要求你创建一个程序,以确定该事件的获胜者/冠军。参赛者人数未定。比赛的胜者通常是BIGGEST号码。这场比赛是一场淘汰赛。比赛的顺序是First IN vs Last IN,Second In vs Last Second In等等......如果某个特定号码没有对手,他或她将被视为默认赢家。

示例输出:

输入参赛人数:5

参加参赛者#1:50

参加参赛者#2:30

参加参赛者#3:8

参加参赛者#4:11

参加参赛者#5:20

模拟:

第一轮:50比20胜者:50

第2轮:30比11胜者:30

第3轮:默认获胜者是:8

下一步......

第4轮:50比8胜者:50

第5轮:默认赢家为30

下一步......

第六轮:50比30胜者:50

冠军:50

总括号匹配:3

总回合数:6

我的解决方案:

import java.util.Scanner;

public class BattleofNumbers{
public static void main(String[] args){
    double num1, num2, num3, num4, num5;
    double chal1, chal2, chal3, champion;
    double bracket, round = 0;

    System.out.println("Please Enter 5 Numbers: ");
    System.out.println("");

    Scanner in = new Scanner(System.in);
    num1 = in.nextDouble();
    num2 = in.nextDouble();
    num3 = in.nextDouble();
    num4 = in.nextDouble();
    num5 = in.nextDouble();

    System.out.println("");

    if (num1 > num5){
        chal1 = num1;
        round++;
        System.out.println("Round 1 Winner: " + chal1);
        }
    else{
        chal1 = num5;
        round++;
        System.out.println("Round 1 Winner: " + chal1);
    }
    if (num2>num4){
        chal2 = num2;
        round++;
        System.out.println("Round 2 Winner: " + chal2);
    }else{
        chal2 = num4;
        round++;
        System.out.println("Round 2 Winner: " + chal2);
    }
    if (chal1>num3){
        chal3 = chal1;
        round++;
        System.out.println("Round 3 Winner: " + chal3);
    }else{
        chal3 = num3;
        round++;
        System.out.println("Round 3 Winner: " +chal3);
    }
    if (chal3 > chal2){
        champion = chal3;
        round++;
        System.out.println("Round 4 Winner: " + champion);
    }else{
        champion = chal2;
        round++;
        System.out.println("Round 4 Winner: " + champion);
    }
    bracket = round / 2;
    System.out.println("=====================");

    System.out.println("The Champion: " + champion);
    System.out.println("No. of Rounds: " + round );
    System.out.println("No. of Brackets: " + bracket);
    }
}

3 个答案:

答案 0 :(得分:0)

  • 我不知道你是多么有经验,但你应该使用数组而不是5个单变量:double[] nums = new double[5];。此外,变量"括号"不需要因为它总是圆/ 2所以你可以替换它:System.out.println("No. of Brackets: " + round/2);

  • 因为在if-和else-block中都使用了每个if-else-block的第2行和第3行,所以你应该将它们外包(从而减少代码量):

    if (num1 > num5){
     chal1 = num1;
     // round++;
     // System.out.println("Round 1 Winner: " + chal1);
     }
    else {
     chal1 = num5;
     // round++;
     // System.out.println("Round 1 Winner: " + chal1);
     }
    round++;
    System.out.println("Round 1 Winner: " + chal1);
    
  • "轮"似乎是4的常数。你可以停止计算它,并用4表示它:double round = 4;

答案 1 :(得分:0)

此解决方案向您展示如何使用ArrayList进行此类操作(以及如何在实际代码中使用它):

import java.util.ArrayList;
import java.util.Scanner;

public class Prog{
    public static void main(String[] args){
      ArrayList<Double> nums = new ArrayList<Double>();
      Scanner in = new Scanner(System.in);
      System.out.println("How many contestants are there? ");
      int size = in.nextInt();
      while(size-- > 0){
        System.out.println("Contestant: ");
        nums.add(in.nextDouble());
      }
      while(nums.size() > 1){
        System.out.println("ROUND STARTED, contestants are: " + nums);
        int i = 0;

        while(i < nums.size()){
          nums.set(i, Math.max(nums.get(i), nums.get(nums.size()-1)));
          if(i != nums.size() - 1)
            nums.remove(nums.size() -1);
          i++;
        }
      }
      System.out.println("Competition has ended, the winner is: " + nums.get(0));
    }
}

答案 2 :(得分:0)

我尝试了一个使用数组的小实现,我称之为半递归。我确定它并不完美,但它是一种更通用的方法,因为输入数量是动态的,条件检查只编程一次。随意提供反馈,也许有人有想法递归执行每个检查。

import java.util.Scanner;

public class BattleofNumbers {

  double[] nums;
  int amount, round = 0;

  double champ;

  Scanner in;

  public BattleofNumbers() {
      in = new Scanner(System.in);
      initValues();
      processGame();
  }

  public void processGame() {
      champ = calcChamp(nums);
      System.out.println("=====================");
      System.out.println("The Champion: " + champ);
      System.out.println("No. of Rounds: " + round);
      System.out.println("No. of Brackets: "
            + (int) Math.ceil((double) round / 2));
  }

  private void initValues() {
      System.out.println("Please enter amount of numbers:");
      amount = in.nextInt();

      nums = new double[amount];

      for (int i = 0; i < amount; i++) {
          System.out.println("Please enter " + (i + 1) + ". number:");
          nums[i] = in.nextDouble();
      }

      System.out.println("");
  }

  public double calcChamp(double[] nums) {
      double[] nexts = new double[(int)Math.ceil(((double) nums.length / 2))];

      for (int first = 0, second = (nums.length - 1); first <= second; first++, second--) {
          if (nums[first] > nums[second]) {
              nexts[first] = nums[first];
          } else {
              nexts[first] = nums[second];
          }
          round++;
          System.out
                .println("Winner of round " + round + ": " + nexts[first]);
      } 

      if (nexts.length == 1) {
          return nexts[0];
      } else {
          return calcChamp(nexts);
      }
  }

  public static void main(String[] args) {
      new BattleofNumbers();
  }

}