2016-11-16 50 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); 
     } 
    }   
}