Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Dl {
  3.     public static void main(String[] args){
  4.         Scanner scan = new Scanner(System.in);
  5.         int n = scan.nextInt();
  6.         int[] a = new int[1];
  7.         a[0]=1;
  8.         int[] b = new int[1];
  9.         b[0]=1;
  10.         int[] c;
  11.         for(int i=3;i<n+1;i++) {
  12.             c = add(a, b);
  13.             a = b;
  14.             b = c;
  15.             out(c);
  16.         }
  17.  
  18.     }
  19.     public static int[] add(int[] a, int[] b) {
  20.         int max = Math.max(a.length, b.length);
  21.         int[] c = new int [max];
  22.         int d = 0;
  23.         for(int i =0;i<max;i++){
  24.             if(i<a.length){
  25.                 c[i]+=a[i];
  26.             }
  27.             if(i<b.length){
  28.                 c[i]+=b[i];
  29.             }
  30.             c[i]+=d;
  31.             d=c[i]/10;
  32.             c[i]=c[i]%10;
  33.         }
  34.         if(d>0){
  35.             c[max+1]=d;
  36.             return c;
  37.         }else{
  38.             int [] c1=new int [max];
  39.             for(int i=0; i<max;i++){
  40.                 c1[i]=c[i];
  41.             }
  42.             return c1;
  43.         }
  44.     }
  45.     public static void out(int[] c) {
  46.         for(int i=c.length-1;i>=0;i--){
  47.             System.out.print(c[i]);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement