2016-11-16 49 views
-2

基於用戶輸入(行數)以菱形圖案打印斐波納契數列。 用戶將輸入的行數和輸出應該包含這些多行打印斐波納契數列如何打印菱形圖案中的斐波那契數字

import java.util.*; 

public class HelloWorld { 
    public static void main(String []args) { 
     Scanner sc = new Scanner(System.in); 

     //Taking noOfRows value from the user 
     System.out.println("Enter no of rows:"); 
     int noOfRows = sc.nextInt(); 

     //Initializing rowCount with 1 
     int rowCount = 1; 

     //Implementing the logic 
     int previous = 0, prevtoprev; 
     for (int i = noOfRows; i > 0; i--) { 

      //Printing i*2 spaces at the beginning of each row 
      for (int j = 1; j <= i*2; j++) { 
       System.out.print(" "); 
      } 

      //Printing j value where j value will be from 1 to rowCount 
      for (int j = 1; j <= rowCount; j++) { 
       if (j%2 == 0) { 
        System.out.print("+"); 
       } else { 
        System.out.print(j); 
       } 
      } 

      //Printing j value where j value will be from rowCount-1 to 1 
      for (int j = rowCount-1; j >= 1; j--) { 
       if (j%2 == 0) { 
        System.out.print("+"); 
       } else { 
        System.out.print(rowCount); 
       } 
      } 

      System.out.println(); 

      //Incrementing the rowCount 
      previous = rowCount; 
      rowCount++; 
     } 
    } 
} 

獲取輸出

enter image description here

預期輸出:

這裏的「34 「被拆分以將」4「放置到下一行中以形成鑽石,並且」144「被拆分以將」1「放置在最後一行中並且剩餘的數字」44「被丟棄。

enter image description here

+0

爲什麼很少有人給予否定檢查 – Girish

+0

預期產出是多少? –

+1

請[編輯]問題。通過選擇並單擊「{}」按鈕來正確地設置代碼的格式。解釋什麼是「菱形格式」,以及您的預期輸出是什麼,然後*提出問題*。 – RealSkeptic

回答

0

我能夠根據行 的數量打印斐波納契數列,但仍是不正確的,我需要用「+」號的菱形圖案安排,也丟棄多餘的數字,如果它不適合。

import java.util.*; 
    public class HelloWorld{ 

    public static void main(String []args){ 
     meth(); 
    } 
    public static int meth(){ 
     int row, column, first_no = 1, second_no = 1, sum = 1; 

     Scanner sc = new Scanner(System.in); 
     //Taking noOfRows value from the user 
     System.out.println("Enter no of rows:"); 
     row = sc.nextInt(); 

     for (int i = 1; i <= row; i++) { 
      for (column = 1; column <= i; column++) { 
       if (i == 1 && column == 1) { 
       System.out.printf("%"+(row)+"d\t",second_no); 
       continue; 
     } 
     System.out.printf(" %d ", sum); 

     //Computes the series 
     sum = first_no + second_no; 
     first_no = second_no; 
     second_no = sum; 
     } 
     System.out.println(""); 
    } 
    return 0; 
} 
} 

輸入:4

獲取輸出:

enter image description here

2

下面是一些對你的代碼。嘗試理解並在必要時進行修改。它只適用於行號爲奇數且行號不大於23. 請嘗試自行改進。

import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class NewClass { 

    public static void main(String []args) { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter number of rows:"); 
     int noOfRows = sc.nextInt(); 
     while(noOfRows%2==0){ 
      System.out.println("Only an odd number can be inserted. Enter number of rows:"); 
      noOfRows = sc.nextInt(); 
     } 
     String str = concatFibSeries(noOfRows*3); 
     List<String> list = chopp(str,noOfRows); 
     printDimondPattern(list); 
    } 
    // Formula for the n-th Fibonacci number 
    static int nthFib(int n){ 
     return (int)((1/Math.sqrt(5))* ((Math.pow(((1+Math.sqrt(5))/2),n)) - (Math.pow(((1-Math.sqrt(5))/2),n))));  
    } 
    // returns a string of all Fibonacci numbers upto the nth Fibonacci number concatenated with "+" 
    static String concatFibSeries(int n){ 
     StringBuilder sb = new StringBuilder(); 
     for(int i= 1; i<=n; i++){ 
      sb.append(nthFib(i)).append("+"); 
     } 
     return sb.toString(); 
    } 
    // cuts the string returned by the method concatFibSeries(int n) into small chunks 
    // returns a list of strings with list.size equal to given rows 
    // the length of the strings beginns by one and increases by two on each step till the middle row is reached 
    // and decreases by two till the end of rows is reached 
    static List<String> chopp(String concatenatedString,int rows){ 
     List<String> list = new ArrayList<>(); 
     for(int i = 1, j = 1; i <= rows; i++, j=(i<= Math.ceil(rows/2.))? j+2 : j-2){ 
      list.add(concatenatedString.substring(0,j)); 
      concatenatedString = concatenatedString.substring(j); 
      if (concatenatedString.startsWith("+")){ 
       concatenatedString = concatenatedString.substring(1); 
      } 
     } 
     return list;  
    } 
    // adds the required space before and after each string and prints the string 
    static void printDimondPattern(List<String> list){ 
     for(int i = 0; i< list.size();i++){ 
      String str =""; 
      for (int j = 0; j<(Math.abs(list.size()-Math.ceil(list.size()/2.)-i));j++){ 
       str +=" "; 
      } 
      System.out.println(str+list.get(i)+str); 
     } 
    }   
}