Advertisement
Venciity

[SoftUni Java] ExamPreparation 02.Tribonacci

May 19th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3. public class Program {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner(System.in);
  7.         long t1 = input.nextLong();
  8.         BigInteger first = BigInteger.valueOf(t1);
  9.         long t2 = input.nextLong();
  10.         BigInteger second = BigInteger.valueOf(t2);
  11.         long t3 = input.nextLong();
  12.         BigInteger third = BigInteger.valueOf(t3);
  13.        
  14.         BigInteger next = BigInteger.valueOf(0);
  15.        
  16.         int n = input.nextInt();
  17.        
  18.         if (n == 1) {
  19.             System.out.println(t1);
  20.         }
  21.         else if (n == 2) {
  22.             System.out.println(t2);
  23.         }
  24.         else if (n== 3) {
  25.             System.out.println(t3);
  26.         }
  27.         else {
  28.             for (int i = 4; i <= n; i++) {
  29.                 next = next.add(first);
  30.                 next = next.add(second);
  31.                 next = next.add(third);
  32.                
  33.                 first = BigInteger.valueOf(0);
  34.                 first = first.add(second);
  35.                
  36.                 second = BigInteger.valueOf(0);
  37.                 second = second.add(third);
  38.                
  39.                 third = BigInteger.valueOf(0);
  40.                 third = third.add(next);
  41.                
  42.                 next = BigInteger.valueOf(0);
  43.             }
  44.             System.out.println(third);
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement