几道打印题

打印字符金字塔

#include<stdio.h>

 

int main(){

       int rows,spaces,up,down;

       char enter;

 

       printf("Please input a capital:\n");

       scanf("%c", &enter);

 

       for(rows = 0; rows <= enter - 'A'; rows++){     //外层循环控制行数,输入的字母是第几个就有几行

              for(spaces = enter - 'A';spaces >= rows; spaces--) //控制空格

                     printf(" ");

              for(up = 0; up <= rows; up++)   //字母升序打印,第几行就有几个字母,从A开始打印

                     printf("%c",'A' + up);

              for(down = rows; down > 0; down--) //字母降序打印,从输入字母的前一个字母开始打印

                     printf("%c",'A' + down - 1);

              printf("\n");

       }

 

       return 0;

}

 

 

数字金字塔

#include<stdio.h>

int main()

{

       int lenth=8;

       int i,j,k;

       printf("Enter n(1-9):\n");

       scanf("%d",&lenth);

       for(i=0;i<lenth;i++)

       {

              for(j=1;j<i+1;j++)

              {

                     printf(" ");

              }

              for(k=2*lenth;k>2*i+1;k--)

              {

 

                     printf("%d",lenth-i);

              }

              printf("\n");

       }

       int a,b,c,d;

       for(a=lenth,d=0;a<2*lenth-1,d<lenth-1;a++,d++)

       {

              for(b=a-lenth;b<lenth-2;b++)

              {

                     printf(" ");

              }

              for(c=3*a+3-2*lenth;c>a;c--)

              {

                     printf("%d",d+2);

              }

              printf("\n");

       }

       return 0;

 }

 

上一篇:腾讯校招编程题


下一篇:Leetcode笔记-9 回文数