URI Problem (Easy Fibonacci) 1151 Solution in Java

Problem Solving, URI

URI Problem (Easy Fibonacci) 1151:
Easy Fibonacci is a basic problem on URI online judge for novice problem solver.
You can find details on this Link.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @Author : Muhammad Harun-Or-Roshid
 * @Date : Oct 20, 2016
 * @Time : 12:40:59 PM
 */
public class Uri1151 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int previous = 0;
        int current = 1;
        int x = Integer.valueOf((br.readLine()).trim());
        if (x > 0 && x < 46) {
            if (x == 1) {
                bw.write("" + previous);
            } else if(x==2){
                bw.write("" + previous + " " + current);
            }else{
                bw.write("" + previous + " " + current + " ");
            }

            for (int i = 0; i <= (x - 3); i++) {
                int newCurrent = (previous + current);
                bw.write(newCurrent + "");
                previous = current;
                current = newCurrent;
                if (i == (x - 3)) {

                } else {
                    bw.write(" ");
                }
                bw.flush();
            }   
            bw.write("\n");
            bw.close();
            br.close();
        }
    }
}

0 Comments

You may find interest following article