Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Solution {
- private static void fibonacci(int num) {
- long f1 = 0;
- long f2 = 1;
- int i = 2;
- System.out.println("1 番目のフィボナッチ数: 1.0\n" +
- "・前回との増加率: --");
- while(f1 + f2 <= num) {
- double rate = 0;
- double f = (f2 + f1);
- if(f1 != 0) {
- rate = f / f2;
- }
- System.out.println(i + " 番目のフィボナッチ数: "
- + f + "\n ・前回との増加率: " + rate);
- long temp = f2;
- f2 = f1 + f2;
- f1 = temp;
- i++;
- }
- }
- public static void main(String[] args) {
- try (Scanner sc = new Scanner(System.in)) {
- System.out.print("使用回数を入力: ");
- int T = sc.nextInt();
- while(T-- > 0) {
- System.out.print("フィボナッチ数を出力する値の範囲を入力: ");
- int N = sc.nextInt();
- System.out.println("==============");
- fibonacci(N);
- System.out.println("==============");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement