Rinne Loves Math

鏈接:https://ac.nowcoder.com/acm/contest/370/J
來源:牛客網

Rinne Loves Math
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
Rinne 剛剛學習了最簡二次根式,於是她想用程序實現一個。
爲了簡化問題,在本題中,最簡二次根式
a

b
ab 的定義爲:
不存在
b
的一個因子
k

s
.
t
.


x

N

,
x
2

k
不存在b的一個因子k s.t. ∃x∈N∗,x2=k
即根號下的數字不含平方數因子。
舉個最簡二次根式的例子:

5
,

20050117
5,20050117
舉個不是最簡的例子:

20
,

444
20,444
現在 Rinne 給了你形如

n
n 的式子中的 n,讓你輸出化簡後的結果
a

b
ab 中的 a,b,如果這樣的式子在實數範圍內沒有意義,輸出 -1。
輸入描述:
第一行一個整數 T,表示數據組數。
接下來 T 行,每行一個整數 x 表示根號下的數。
輸出描述:
輸出一共 T 行,每行兩個數表示

x
x 化簡後的答案 a,b
示例1
輸入
複製
4
20
25
-2005
11
輸出
複製
2 5
5 1
-1
1 11
說明
20

4
×
5
20=4×5
25

5
×
5
25=5×5
實數範圍內

n
n中 n 小於 0 沒有意義。
11 是個質數。
備註:
T

100
,
0
<
|
x
|

10
7

思路:這個題錯的真的是難受,就是打表的時候數據沒有覆蓋全。也是很水的一題。

錯誤代碼:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        int[] num=new int[1005];
        for(int i=1;i<num.length;i++)
            num[i]=i*i;
        while(t>0){
            int x=sc.nextInt();
            if(x<0) {
                System.out.println(-1);
            }else {
                int a=0;int b=0;
                for(int i=1;i<num.length;i++) {
                    if(x%num[i]==0 && x>=num[i]) {
                        a=i;
                        b=x/num[i];
                    }
                }
                System.out.println(a+" "+b);
            }
            t--;
        }
        sc.close();
    }
}

正確代碼:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        int[] num=new int[10005];
        for(int i=1;i<num.length;i++)
        	num[i]=i*i;
        while(t>0){
            int x=sc.nextInt();
            if(x<0) {
            	System.out.println(-1);
            }else {
            	int a=0;int b=0;
                for(int i=1;i<num.length;i++) {
                	if(x%num[i]==0 && x>=num[i]) {
                		a=i;
                		b=x/num[i];
                	}
                }
                System.out.println(a+" "+b);
            }
            t--;
        }
        sc.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章