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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章