2016-11-16 3 views
-2

ダイヤモンドパターンでフィボナッチシリーズを印刷するユーザー入力(行数)に基づいています。 ユーザーは、行の番号を入力し、出力がフィボナッチ数をダイヤモンドパターンで印刷する方法

enter image description here

期待出力としてフィボナッチ数列

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++; 
     } 
    } 
} 

に取得出力を印刷し、それらの多くの行が含まれている必要があります。ここ

「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); 
     } 
    }   
} 
関連する問題