POJ 3982 序列

Description

数列A满足An = An-1 + An-2 + An-3, n >= 3

编写程序,给定A0, A1 和 A2, 计算A99

Input

输入包含多行数据

每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
数据以EOF结束

Output

对于输入的每一行输出A99的值

Sample Input

1 1 1

Sample Output

方法一:利用String进位法

 1 import java.util.Scanner;
 2 
 3 
 4 public class Main {
 5     
 6     static String doAdd(String a,String b){
 7         String str="";
 8         int lenA=a.length();
 9         int lenB=b.length();
10         int maxLen=(lenA>lenB)?lenA:lenB;
11         int minLen=(lenA<lenB)?lenA:lenB;
12         String strTmp="";
13         for(int i=maxLen-minLen;i>0;i--){
14             strTmp+="0";
15         }
16         //把长度调整到相同
17         if(maxLen==lenA){
18             b=strTmp+b;
19         }else
20             a=strTmp+a;
21         
22         int JW=0;//进位
23         for(int i=maxLen-1;i>=0;i--){
24             int tempA=Integer.parseInt(String.valueOf(a.charAt(i)));
25             int tempB=Integer.parseInt(String.valueOf(b.charAt(i)));
26             
27             int temp;
28             if(tempA+tempB+JW>=10&&i!=0){
29                 temp=tempA+tempB+JW-10;
30                 JW=1;
31             }else{
32                 temp=tempA+tempB+JW;
33                 JW=0;
34             }
35             str=String.valueOf(temp)+str;
36         }
37         return str;
38     }
39 
40     public static void main(String[] args){
41         Scanner sc=new Scanner(System.in);
42         
43         String[] a=new String[100];
44         while(sc.hasNext()){
45             a[0]=Integer.toString(sc.nextInt());
46             a[1]=Integer.toString(sc.nextInt());
47             a[2]=Integer.toString(sc.nextInt());
48             for(int i=3;i<100;i++){
49                 String temp=doAdd(a[i-1],a[i-2]);
50                 a[i]=doAdd(temp,a[i-3]);
51             }
52             
53             System.out.println(a[99]);
54         }
55     }
56 }
View Code

方法二:利用BIG数相加法

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 
 5 public class Main1 {
 6     
 7     public static BigInteger calc(BigInteger a,BigInteger b,BigInteger c){
 8         BigInteger now=c;//现在的
 9         BigInteger last=b;//过去的
10         BigInteger llast=a;//过过去的
11         BigInteger answer;
12         for(int i=0;i<97;i++){
13             //计算前三个和
14             answer=now.add(last);
15             answer=answer.add(llast);
16             //新值覆盖旧值
17             llast=last;
18             last=now;
19             now=answer;
20         }
21         return now;
22     }
23 
24     public static void main(String[] args){
25         Scanner sc=new Scanner(System.in);
26         while(sc.hasNext()){
27             int a0=sc.nextInt();
28             BigInteger A0=BigInteger.valueOf(a0);
29             int a1=sc.nextInt();
30             BigInteger A1=BigInteger.valueOf(a1);
31             int a2=sc.nextInt();
32             BigInteger A2=BigInteger.valueOf(a2);
33             System.out.println(calc(A0,A1,A2));
34         }
35     }
36 }
View Code

 

 

posted @ 2014-01-23 09:43  JL-JackyLee  阅读(276)  评论(0编辑  收藏  举报