a059: 完全平方和

題目:http://zerojudge.tw/ShowProblem?problemid=a059
程式碼:

import java.util.Scanner;
public class a059 {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  while(sc.hasNext()){
   int n = sc.nextInt();
   int i,a,b,c=0;
   for(i=1;i<=n;i++){
    a = sc.nextInt();
    b = sc.nextInt();
    for(int j=a;j<=b;j++){
     if(j == Math.pow(Math.ceil(Math.sqrt(j)),2)){//pow平方ceil無條件捨去sqrt開根號
      c+=j;
     }
    }
    System.out.println("Case "+i+": "+c);
    c = 0;//要歸0否則會累加上次;
   }
   
  }

 }

}

留言

熱門文章