题目描述
现在给出一个表达式,形如 𝑎1/𝑎2/𝑎3/.../𝑎𝑛。
如果直接计算,就是一个个除过去,比如 1/2/1/4=1/8。
然而小AA看到一个分数感觉很不舒服,希望通过添加一些括号使其变成一个整数。一种可行的办法是 (1/2)/(1/4)=2 。
现在给出这个表达式,求问是否可以通过添加一些括号改变运算顺序使其成为一个整数。
输入格式
一个测试点中会有多个表达式。
第一行 𝑡 ,表示表达式数量。
对于每个表达式,第一行是 𝑛,第二行 𝑛 个数,第 𝑖 个数表示 𝑎𝑖。
输出格式
输出 𝑡 行。
对于每个表达式,如果可以通过添加括号改变顺序使其变成整数,那么输出 Yes
,否则输出 No
。
代码:
package lanqiao;
import java.util.*;
public class Main{
public static int gcd(int x,int y)
{
if(y == 0)
{
return x;
}
return gcd(y,x % y);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[10001];
for(int i = 0;i < n;i ++)
{
int m = sc.nextInt();
a[0] = sc.nextInt();
a[1] = sc.nextInt();
a[1] /= gcd(a[0],a[1]); //求最大公约数
for(int j = 2;j < m;j ++){
a[j] = sc.nextInt();
a[1] /= gcd(a[1],a[j]);
}
if(a[1] == 1)
{
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
}