#3090: 不明白為甚麼總不能通過


master (Master)


我已經用了while , 不管有多少筆資料應該都可以讀取, 但就是不給我通過

import java.util.Scanner;

public class JAVA
{
 public static void main(String[] args)
 {
  Scanner sc=new Scanner(System.in);
  
  int x, y;

  while (true)
  {
  x=sc.nextInt();
  y=sc.nextInt();
  
  System.out.println(x+y);
  }
 }
}

#3093: Re:不明白為甚麼總不能通過


example (學姊)


我已經用了while , 不管有多少筆資料應該都可以讀取, 但就是不給我通過

import java.util.Scanner;

public class JAVA
{
 public static void main(String[] args)
 {
  Scanner sc=new Scanner(System.in);
  
  int x, y;

  while (true)
  {
  x=sc.nextInt();
  y=sc.nextInt();
  
  System.out.println(x+y);
  }
 }
}

 while() 迴圈裡面改成 sc.hasNext() 就可以了
#3094: Re:不明白為甚麼總不能通過


master (Master)


解出來了

 AC (108ms, 5.7MB)

 可是後面的數字是甚麼意思? 5.7MB?

108ms是解題的速度嗎? 我見別人都很快, <10ms, 難道這麼簡單的加法, 也有更高速的寫法?

#3095: Re:不明白為甚麼總不能通過


example (學姊)


解出來了

 AC (108ms, 5.7MB)

 可是後面的數字是甚麼意思? 5.7MB?

108ms是解題的速度嗎? 我見別人都很快, <10ms, 難道這麼簡單的加法, 也有更高速的寫法?

 因為要跨平台
#3097: Re:不明白為甚麼總不能通過


example (學姊)


解出來了

 AC (108ms, 5.7MB)

 可是後面的數字是甚麼意思? 5.7MB?

108ms是解題的速度嗎? 我見別人都很快, <10ms, 難道這麼簡單的加法, 也有更高速的寫法?

 因為要跨平台


 一句話可能不足以表達我的意思

 5.7 MB 是此程式記憶體使用量

 而 108ms 是解題的速度沒錯

 但是由於 JAVA 有做到跨平台的設計

 所以當你用 C/C++/PASCAL 解題時花費 0ms 

 但是用 JAVA 時卻要 100ms

 所以 JAVA 由於先天限制

 解題的時間也就比其他語言多三倍 ( 1s -> 3s, 10s -> 30s )