2016: C语言实验——打印金字塔
生活随笔
收集整理的這篇文章主要介紹了
2016: C语言实验——打印金字塔
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2016: C語言實驗——打印金字塔
Description
輸入n值,打印下列形狀的金字塔,其中n代表金字塔的層數。Input
輸入只有一個正整數n。Output
打印金字塔圖形,其中每個數字之間有一個空格。Sample Input
3Sample Output
11 2 1 1 2 3 2 1java 代碼
import java.io.*; import java.util.*;public class 打印金字塔 {public static void main(String[] args) {// TODO Auto-generated method stubScanner s = new Scanner(System.in);int n = s.nextInt();for(int i = 1;i<=n;i++){for(int j=1;j<=2*(n-i);j++){System.out.print(" ");}for(int j=1;j<=2*i-1;j++){if(j%2!=0){System.out.print(j/2+1);}else {System.out.print(" ");}}for(int j=2*i-1;j>=2;j--){if(j%2==0){System.out.print(j/2);}else {System.out.print(" ");}}System.out.println();}}}總結
以上是生活随笔為你收集整理的2016: C语言实验——打印金字塔的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mac安装cnpm
- 下一篇: Problem C: 判断字符串是否为回