close

給你a, b,找出a,  b之間(inclusive)的完全平方數有幾個。

--------

package uva11461;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author awesq
 */
public class UVa11461 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final int tr = 1, fa = -1, unknown = 0;
        int[] numbers = new int[100001]; // From 0 ~ 100000 but 0 isn't used.
        Scanner sc = new Scanner(System.in);
        Arrays.fill(numbers, unknown);

        while (true) {
            try {
                int a, b;
                a = sc.nextInt();
                b = sc.nextInt();
                if (0 == a && 0 == b) {
                    break;
                }

                int squares = 0;
                for (; a <= b; a++) {
                    if (unknown == numbers[a]) {
                        double sq = Math.sqrt(a);
                        if (sq == Math.floor(sq)) {
                            numbers[a] = tr;
                            squares++;
                        } else {
                            numbers[a] = fa;
                        }
                    } else if (tr == numbers[a]) {
                        squares++;
                    }
                }
                System.out.println("" + squares);
            } catch (Exception e) {
                System.out.println("Wrong!");
            }
        }
    }
}
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()