ZZULI:1077: 空心菱形

sublime 3(集成插件版下载安装,c++编译设置, java编译设置, python设置)

郑州轻工业大学2020年数据结构练习集

ZZULI—刷题之路

zzulioj_1000-1010

zzulioj_1011-1050

1077: 空心菱形

时间限制: 1 Sec  内存限制: 128 MB
提交: 1528  解决: 824
[提交] [状态] [讨论版] [命题人:admin]

题目描述

输入一个整数n,(1<=n<=20),输出一个空心菱形,其中每个边由n个'*'组成。 

 

输入

输入包含一个整数,n(1<=n<=20) 

 

输出

输出一个空心菱形,每个边由n个'*'组成 

 

样例输入

5

 

样例输出


    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

来源/分类

*** 

 

[提交] [状态]

第一种做法:使用函数。

#include 
#include 
#include
void B_s(int n)
{
    int i;
    for(i = 1; i <= n; i++)
        printf(" ");
    printf("*");
}

int main()
{
    int i,n;
    scanf("%d",&n);

    for(i = -(n-1); i <= n-1; i++)
    {
        B_s(abs(i));

        if(i != -(n-1) && i != n-1 )
        B_s( (n - abs(i) - 1)*2 - 1);

        printf("\n");
    }

    return 0;
}

这个是简化之后的代码。

下面是没有简化之前的代码:

#include 
#include 
#include
void kong(int h);
int main(void)
{
    int i,n;
    scanf("%d",&n);
    for(i=-(n-1); i<=n-1; i++)
    {
        {
            if(i== -(n-1)||i ==n-1)
            {
                kong(abs(i));

            }
            else
            {
                kong(abs(i));
                kong((n -abs(i)-1)*2-1);
            }
            putchar('\n');
        }
    }
    return 0;
}
void kong(int h)
{
    int i;
    for(i=1; i<=h; i++)
        printf(" ");
    printf("*");
}

2、使用常规的方法进行解题。

#include
#include
#include
int main()
{
    int n;
    int a, b, c;
    scanf("%d", &n);
    n = n - 1;

    for ( a = -n ; a <= n; a++)//计算行数
    {
        for (b = 1 ; b <= fabs(a); b++ ) //确定最前面空格数量
        {
            printf(" ");
        }
        if((a != -n )&&(a != n))
        {
            printf("*");
        }
        if(a <= 0)
        {
            for(  c = 0; c < (a + n) * 2 - 1; c++ ) //确定符号个数,符号之间的空格,
            {

                printf(" ");
            }
        }
        else
        {
            for(  c = 0; c < (n - a) * 2 - 1; c++ ) //确定符号个数,符号之间的空格,
            {

                printf(" ");
            }
        }
        printf("*\n");

    }
    return 0;

}

 

你可能感兴趣的:(zzuli——我的刷题之路)