Skip to content

Instantly share code, notes, and snippets.

@peter279k
Created December 22, 2015 02:08
Show Gist options
  • Save peter279k/07861414c51afdbb1fed to your computer and use it in GitHub Desktop.
Save peter279k/07861414c51afdbb1fed to your computer and use it in GitHub Desktop.
import java.util.*;
public class main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int []fib_arr = new int[39];
fib_arr[0] = 1;
fib_arr[1] = 2;
for(int i=2;i<39;i++) {
fib_arr[i] = fib_arr[i-1] + fib_arr[i-2];
}
int numbers = input.nextInt();
ArrayList res_list;
for(int i=1;i<=numbers;i++) {
int fib_num = input.nextInt();
if(fib_num == 0) {
System.out.println("1 = " + "0 (fib)");
}
else if(fib_num == 1) {
System.out.println("1 = " + "1 (fib)");
}
else {
String res = "";
String result = "";
res_list = new ArrayList();
boolean is_equal = false;
for(int j=0;j<fib_arr.length;j++) {
if(fib_arr[j] == fib_num) {
is_equal = true;
res_list.add("1");
break;
}
else {
res_list.add("0");
}
}
if(is_equal) {
}
else {
res = "";
int temp = fib_num;
boolean add_zero = false;
for(int j=fib_arr.length-1;j>=0;j--) {
if(fib_arr[j] <= temp) {
temp = temp - fib_arr[j];
res += "1";
add_zero = true;
}
else {
if(add_zero)
res += "0";
}
}
}
for(int h=res_list.size()-1;h>=0;h--) {
result += res_list.get(h);
}
if(res != "")
System.out.println(fib_num + " = " + res + " (fib)");
else
System.out.println(fib_num + " = " + result + " (fib)");
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment