【Java大数】hdu 5429 Geometric Progression
2015-09-14 23:05
549 查看
Source : hdu 5429 Geometric Progression
Determine whether a sequence is a Geometric progression or not.
In mathematics, a geometric progression, also known as a geometric sequence, is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, … is a geometric progression with common ratio 3. Similarly 10, 5, 2.5, 1.25, … is a geometric sequence with common ratio 1/2.
Examples of a geometric sequence are powers rk of a fixed number r, such as 2k and 3k. The general form of a geometric sequence is
a, ar, ar2, ar3, ar4, …
where r ≠ 0 is the common ratio and a is a scale factor, equal to the sequence’s start value.
Input
First line contains a single integer T(T≤20) which denotes the number of test cases.
For each test case, there is an positive integer n(1≤n≤100) which denotes the length of sequence,and next line has n nonnegative numbers Ai which allow leading zero.The digit’s length of Ai no larger than 100.
Output
For each case, output “Yes” or “No”.
4
1
0
3
1 1 1
3
1 4 2
5
16 8 4 2 1
Sample Output
Yes
Yes
No
Yes
加粗
斜体
引用
插入链接
插入代码
插入图片
提升标题
有序列表
无序列表
横线
撤销
重做
http://acm.hdu.edu.cn/showproblem.php?pid=5429
Problem DescriptionDetermine whether a sequence is a Geometric progression or not.
In mathematics, a geometric progression, also known as a geometric sequence, is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, … is a geometric progression with common ratio 3. Similarly 10, 5, 2.5, 1.25, … is a geometric sequence with common ratio 1/2.
Examples of a geometric sequence are powers rk of a fixed number r, such as 2k and 3k. The general form of a geometric sequence is
a, ar, ar2, ar3, ar4, …
where r ≠ 0 is the common ratio and a is a scale factor, equal to the sequence’s start value.
Input
First line contains a single integer T(T≤20) which denotes the number of test cases.
For each test case, there is an positive integer n(1≤n≤100) which denotes the length of sequence,and next line has n nonnegative numbers Ai which allow leading zero.The digit’s length of Ai no larger than 100.
Output
For each case, output “Yes” or “No”.
题意
给n个数,判断能否构成等比数列(注意数的范围)
示例
Sample Input4
1
0
3
1 1 1
3
1 4 2
5
16 8 4 2 1
Sample Output
Yes
Yes
No
Yes
思路
大数据,我用的JAVA!参考代码
import java.io.*; import java.util.*; import java.math.*; public class Main{ static BigInteger []a = new BigInteger [110]; static BigInteger zero = BigInteger.ZERO; public static void main(String[] args){ Scanner cin = new Scanner(System.in); int T = cin.nextInt(); while(T-- >0){ int n = cin.nextInt(); boolean has0 = false,all0 = true; for(int i = 0; i < n; ++ i){ a[i] = cin.nextBigInteger(); if(a[i].compareTo(zero) == 0) has0 = true; else all0 = false; } if(all0 == true){System.out.println("Yes");continue;} if(has0 == true){System.out.println("No");continue;} boolean ok = true; for(int i = 2; i < n; ++ i){ if(a[i-1].multiply(a[i-1]).equals(a[i-2].multiply(a[i])) == false) { ok = false; break; } } if(ok) System.out.println("Yes"); else System.out.println("No"); } } }
加粗
Ctrl + B
斜体
Ctrl + I
引用
Ctrl + Q
插入链接
Ctrl + L
插入代码
Ctrl + K
插入图片
Ctrl + G
提升标题
Ctrl + H
有序列表
Ctrl + O
无序列表
Ctrl + U
横线
Ctrl + R
撤销
Ctrl + Z
重做
Ctrl + Y
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统