Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Solution {
  4.  
  5. private static void fibonacci(int num) {
  6. long f1 = 0;
  7. long f2 = 1;
  8. int i = 2;
  9. System.out.println("1 番目のフィボナッチ数: 1.0\n" +
  10. "・前回との増加率: --");
  11. while(f1 + f2 <= num) {
  12. double rate = 0;
  13. double f = (f2 + f1);
  14. if(f1 != 0) {
  15. rate = f / f2;
  16. }
  17. System.out.println(i + " 番目のフィボナッチ数: "
  18. + f + "\n ・前回との増加率: " + rate);
  19. long temp = f2;
  20. f2 = f1 + f2;
  21. f1 = temp;
  22. i++;
  23. }
  24. }
  25.  
  26. public static void main(String[] args) {
  27. try (Scanner sc = new Scanner(System.in)) {
  28. System.out.print("使用回数を入力: ");
  29. int T = sc.nextInt();
  30. while(T-- > 0) {
  31. System.out.print("フィボナッチ数を出力する値の範囲を入力: ");
  32. int N = sc.nextInt();
  33. System.out.println("==============");
  34. fibonacci(N);
  35. System.out.println("==============");
  36. }
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement