#10716: Line4 WA?


manhong2112 (飄夢)


求Line4測資, 我都用公式解了為啥還差1(201/202) ???!

import java.util.*;
public class a148 {
  public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
    String[] str;
    double m;
    double n;
    double c;
    while(input.hasNext()) {
      str = input.nextLine().split(" ");
      n = Integer.valueOf(str[0]);
      c = Integer.valueOf(str[1]);
      m = (Math.sqrt(Math.abs(8 * c + 4 * n * n - 4 * n + 1)) - 1) / 2 + 0.5;
      System.out.println(((int) m) - ((int) n) + 1);
    }
  }
}

#12113: Re:Line4 WA?


lessonone (我好弱)


求Line4測資, 我都用公式解了為啥還差1(201/202) ???!

import java.util.*;
public class a148 {
  public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
    String[] str;
    double m;
    double n;
    double c;
    while(input.hasNext()) {
      str = input.nextLine().split(" ");
      n = Integer.valueOf(str[0]);
      c = Integer.valueOf(str[1]);
      m = (Math.sqrt(Math.abs(8 * c + 4 * n * n - 4 * n + 1)) - 1) / 2 + 0.5;
      System.out.println(((int) m) - ((int) n) + 1);
    }
  }
}



因為要加到比m還要大 不能相等於m喔

例如n=-100 m=0

那麼要加到202項 總和=1 才會大於0

#12114: Re:Line4 WA?


lessonone (我好弱)


求Line4測資, 我都用公式解了為啥還差1(201/202) ???!

import java.util.*;
public class a148 {
  public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
    String[] str;
    double m;
    double n;
    double c;
    while(input.hasNext()) {
      str = input.nextLine().split(" ");
      n = Integer.valueOf(str[0]);
      c = Integer.valueOf(str[1]);
      m = (Math.sqrt(Math.abs(8 * c + 4 * n * n - 4 * n + 1)) - 1) / 2 + 0.5;
      System.out.println(((int) m) - ((int) n) + 1);
    }
  }
}

此外 你還要考慮一種很極端的現象


例如 n= 1000 m= -100000000000000000000

此時只要數1次就可以大於m了 但是當你把這帶入你的公式解 你就知道會有很嚴重的錯誤