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