hdu5429(BestCoder Round #54 (div.2) 1003题)

Geometric Progression

Accepts: 40
Submissions: 644
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
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(T20) which denotes the number of test cases. For each test case, there is an positive integer n(1n100) 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".
Sample Input
4
1
0
3
1 1 1
3
1 4 2
5
16 8 4 2 1
Sample Output
Yes
Yes
 
    
分析:大数,直接java过,判断条件a[i]*a[i]==a[i-1]*a[i+1],另外要注意n个数全为0或者一部分为0的情况。
 
    
import java.util.*;
import java.math.*;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BigDecimal[] s = new BigDecimal[110];
        int T,n,flag,i,t;
        BigDecimal a,b;
        Scanner cin = new Scanner(System.in);
        T = cin.nextInt();
        while (T-->0){
            n = cin.nextInt();
            flag = 0;
            t = 0;
            for (i=0; i<n; i++){
                s[i] = cin.nextBigDecimal();
                if (s[i].compareTo(BigDecimal.valueOf(0))==0){
                    t++;
                }
            }
            if (t==n||n==1){
                System.out.println("Yes");
                continue;
            }
            if (t!=0){
                System.out.println("No");
                continue;
            }
                
            for (i=1; i<n-1; i++){
                a = s[i].multiply(s[i]);
                b = s[i-1].multiply(s[i+1]);
                if (a.compareTo(b)!=0){
                    flag = 1;
                    break;
                }
            }
            if (flag==1)
                System.out.println("No");
            else
                System.out.println("Yes");
        }
    }

}


你可能感兴趣的:(hdu5429(BestCoder Round #54 (div.2) 1003题))