andy6804tw
3/15/2017 - 11:29 AM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2456 這題用ArrayList先建表,計算輸入a、b序列中有幾個是符合平方的數

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2456

這題用ArrayList先建表,計算輸入a、b序列中有幾個是符合平方的數

import java.util.*;  
public class Main {  
	
    public static void main(String[] args) {  
        Scanner scn = new Scanner(System.in);  
        ArrayList<Integer> list=new ArrayList<Integer>();
        int i=1,num=1;
        while(num<=100000){
        	list.add(num);
        	i++;
        	num=i*i;
        }
        while(true){
        	int a=scn.nextInt(),b=scn.nextInt(),count=0;
        	if(a==0&&b==0)
        		break;
        	if(a==b)
        		b++;
        		for(int k=0;k<list.size();k++){
        			if(list.get(k)>=a&&list.get(k)<=b)
        				count++;
        	}
        		System.out.println(count);
        }
        
    }
    /*題目:Q11461:Square Numbers
    作者:1010
    時間:西元 2017 年 3 月 */
}