POJ 3982 序列 [解题报告] Java

序列

问题描述 :

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

 

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

输入:

输入包含多行数据

 

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

数据以EOF结束

输出:

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

样例输入:

1 1 1

样例输出:

69087442470169316923566147

代码实现:
//* @author: 
import java.util.*;
import java.math.BigInteger; 
public class Main {
 static String doAdd(String a, String b) { //两个大数相加的方法。
        String str = "";   
        int lenA = a.length();   
        int lenB = b.length();   
        int maxLen = (lenA > lenB) ? lenA : lenB;   
        int minLen = (lenA < lenB) ? lenA : lenB;   
        String strTmp = "";   
        for (int i = maxLen - minLen; i > 0; i--) {   
            strTmp += "0";   
        }   
        // 把长度调整到相同   
        if (maxLen == lenA) {   
            b = strTmp + b;   
        } else  
            a = strTmp + a;   
        int JW = 0;// 进位   
        for (int i = maxLen - 1; i >= 0; i--) {   
            int tempA = Integer.parseInt(String.valueOf(a.charAt(i)));   
            int tempB = Integer.parseInt(String.valueOf(b.charAt(i)));   
            
            int temp;   
            if (tempA + tempB + JW >= 10 && i != 0) {   
                temp = tempA + tempB + JW - 10;   
                JW = 1;   
            } else {   
                temp = tempA + tempB + JW;   
                JW = 0;   
            }   
            str = String.valueOf(temp) + str;   
        }   
        return str;   
    }   
  


  public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
 
  String a[]=new String [100];
  while(in.hasNext()){
  a[0]=Integer.toString(in.nextInt());
  a[1]=Integer.toString(in.nextInt());
  a[2]=Integer.toString(in.nextInt());
  for(int i=3;i< 100;i++){
     String temp=doAdd(a[i-1],a[i-2]);
     a[i]=doAdd(temp,a[i-3]);
   }
   
   System.out.println(a[99]);
  }
 }
}

方法二:
import java.math.*;
import java.util.*;

public class Main
{
    public static BigInteger calc(BigInteger a,BigInteger b,BigInteger c)
    {
        BigInteger now = c;
        BigInteger last = b;
        BigInteger llast = a;
        BigInteger answer;
        for(int i=0;i< 97;i++) {
            answer = now.add(last);
            answer = answer.add(llast);
            llast = last;
            last = now;
            now = answer;
        }
        return now;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int a0 = in.nextInt();
            BigInteger A0 = BigInteger.valueOf(a0);
            int a1 = in.nextInt();
            BigInteger A1 = BigInteger.valueOf(a1);
            int a2 = in.nextInt();
            BigInteger A2 = BigInteger.valueOf(a2);
            System.out.println(calc(A0,A1,A2));
        }
    }
}

 

转载于:https://www.cnblogs.com/youngchan/p/4610215.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值