C語言列印菱形字母陣,程式縮短,縮短,再縮短

來源:互聯網
上載者:User
#include <stdio.h>int main(int argc, char *argv[]){char c1[27], c2[27];char high = 0, tmp = 0;int i, k;//建立字母表for(i=0; i<26; i++){c1[i] = 'a' + i;c2[i] = 'z' - i;}c1[26] = c2[26] = 0;printf("請輸入你要的金字塔高度:");scanf("%d", &high);if(high>26){printf("沒那麼多英文字母⊙﹏⊙\n");return 0;}for(i=0; i<high; i++){for(k=0; k<high-i; k++)putchar(' ');tmp = c1[i+1];c1[i+1] = 0;printf("%s", c1);c1[i+1] = tmp;printf("%s\n", c2+26-i);}for(i=high-2; i>=0; i--){for(k=0; k<high-i; k++)putchar(' ');tmp = c1[i+1];c1[i+1] = 0;printf("%s", c1);c1[i+1] = tmp;printf("%s\n", c2+26-i);}return 0;}

再最佳化:

#include <stdio.h>#define print(n, m, s)printf("%"##n##.##m##"s", s);int main(int argc, char *argv[]){char c1[] = "abcdefghigklmnopqrstuvwxyz";//字母表 char c2[] = "zyxwvutsrqponmlkgihgfedcba";//倒序字母表 char high = 0, tmp = 0;int i, k;printf("請輸入你要的金字塔高度:");scanf("%d", &high);if(high>26){printf("沒那麼多英文字母⊙﹏⊙\n");return 0;}for(i=0; i<high; i++){tmp = c1[i+1];c1[i+1] = 0;printf("%26s", c1);c1[i+1] = tmp;printf("%s\n", c2+26-i);}for(i=high-2; i>=0; i--){tmp = c1[i+1];c1[i+1] = 0;printf("%26s", c1);c1[i+1] = tmp;printf("%s\n", c2+26-i);}return 0;}

看這個:

#include <stdio.h>#include <stdlib.h>#define strhigh "19"//高度 #define print(x) printf("%"strhigh"."strhigh"s", x);int main(int argc, char *argv[]){char c1[] = "                         abcdefghigklmnopqrstuvwxyz";//25個空格+字母表 char c2[] = "zyxwvutsrqponmlkgihgfedcba";//倒序字母表 int i, k;int high = atoi(strhigh);for(i=0; i<high; i++){print(c1+26-(high-i));printf("%s\n", c2+26-i);}for(i=high-2; i>=0; i--){print(c1+26-(high-i));printf("%s\n", c2+26-i);}return 0;}

下面這個,您隨便看著辦吧:

#include <stdio.h>int main(int argc, char *argv[]){char c[][52] = {"                         a","\                        aba","\                       abcba","\                      abcdcba","\                     abcdedcba","\                    abcdefedcba","\                   abcdefgfedcba","\                  abcdefghgfedcba","\                 abcdefghihgfedcba","\                abcdefghigihgfedcba","\               abcdefghigkgihgfedcba","\              abcdefghigklkgihgfedcba","\             abcdefghigklmlkgihgfedcba","\            abcdefghigklmnmlkgihgfedcba","\           abcdefghigklmnonmlkgihgfedcba","\          abcdefghigklmnoponmlkgihgfedcba","\         abcdefghigklmnopqponmlkgihgfedcba","\        abcdefghigklmnopqrqponmlkgihgfedcba","\       abcdefghigklmnopqrsrqponmlkgihgfedcba","\      abcdefghigklmnopqrstsrqponmlkgihgfedcba","\     abcdefghigklmnopqrstutsrqponmlkgihgfedcba","\    abcdefghigklmnopqrstuvutsrqponmlkgihgfedcba","\   abcdefghigklmnopqrstuvwvutsrqponmlkgihgfedcba","\  abcdefghigklmnopqrstuvwxwvutsrqponmlkgihgfedcba","\ abcdefghigklmnopqrstuvwxyxwvutsrqponmlkgihgfedcba","\abcdefghigklmnopqrstuvwxyzyxwvutsrqponmlkgihgfedcba","\ abcdefghigklmnopqrstuvwxyxwvutsrqponmlkgihgfedcba","\  abcdefghigklmnopqrstuvwxwvutsrqponmlkgihgfedcba","\   abcdefghigklmnopqrstuvwvutsrqponmlkgihgfedcba","\    abcdefghigklmnopqrstuvutsrqponmlkgihgfedcba","\     abcdefghigklmnopqrstutsrqponmlkgihgfedcba","\      abcdefghigklmnopqrstsrqponmlkgihgfedcba","\       abcdefghigklmnopqrsrqponmlkgihgfedcba","\        abcdefghigklmnopqrqponmlkgihgfedcba","\         abcdefghigklmnopqponmlkgihgfedcba","\          abcdefghigklmnoponmlkgihgfedcba","\           abcdefghigklmnonmlkgihgfedcba","\            abcdefghigklmnmlkgihgfedcba","\             abcdefghigklmlkgihgfedcba","\              abcdefghigklkgihgfedcba","\               abcdefghigkgihgfedcba","\                abcdefghigihgfedcba","\                 abcdefghihgfedcba","\                  abcdefghgfedcba","\                   abcdefgfedcba","\                    abcdefedcba","\                     abcdedcba","\                      abcdcba","\                       abcba","\                        aba","\                         a"};int high, i;printf("請輸入高度:");scanf("%d", &high);for(i=0; i<high; i++)printf("%s\n", c[i]);for(i=52-high; i<51; i++)printf("%s\n", c[i]);return 0;}

得,尋得趙老師指點,終極版:

#include #define ab(i) ( (i^(i>>sizeof(i)))-(i>>sizeof(i)) ) //abs()char c1[]="abcdefghigklmnopqrstuvwxyz";char c2[]="zyxwvutsrqponmlkgihgfedcba";int n,i,k;int main(int argc, char *argv[]){printf("請輸入高度:");scanf("%d", &n);n = n % 26;for (i=1-n; i<n; i++){k=n-ab(i);printf("%*.*s",n,k,c1);printf("%s\n",c2+27-k);}return 0;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.