11 Ocak 2013 Cuma

數字菱形輸出


給你一個數字,請你參考範例輸入輸出的形式顯示圖形樣式。

輸入說明輸入一個正整數,介於 1 ~ 9 。
輸出說明:請參考範例輸出。
範例:


Sample Input:Sample Output:
1
2
3
1
ST13-1
ST13-2
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. void diamond(int n)  
  4. {  
  5.     int i,j,k;  
  6.     for(i=1;i<=n;i++)  
  7.     {  
  8.         k=n-i;  
  9.         for(j=1;j<=n-i;j++)  
  10.         {  
  11.             printf(" ");  
  12.         }  
  13.         for(j=1;j<=i;j++)  
  14.             printf("%d",j);  
  15.         for(j=i-1;j>=1;j--)  
  16.             printf("%d",j);  
  17.         for(j=1;j<=n-i;j++)  
  18.         {  
  19.             printf(" ");  
  20.         }  
  21.         printf("\n");  
  22.     }  
  23.     for(i=n-1;i>=1;i--)  
  24.     {  
  25.         k=n-i;  
  26.         for(j=1;j<=n-i;j++)  
  27.         {  
  28.             printf(" ");  
  29.         }  
  30.         for(j=1;j<=i;j++)  
  31.             printf("%d",j);  
  32.         for(j=i-1;j>=1;j--)  
  33.             printf("%d",j);  
  34.         for(j=1;j<=n-i;j++)  
  35.         {  
  36.             printf(" ");  
  37.         }  
  38.         printf("\n");  
  39.     }  
  40. }  
  41.       
  42. int main()  
  43. {  
  44.     int n;  
  45.     while(scanf("%d",&n)!=EOF)  
  46.     {  
  47.         diamond(n);  
  48.     }  
  49.     return 0;  
  50. }  

Hiç yorum yok:

Yorum Gönder

Binary Conversion

Problem Description Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary number...