• c语言 422 显示出一个横向较长的长方形


    c语言 4-22 显示出一个横向较长的长方形

    1、for语句

    #include <stdio.h>
    
    int main(void)
    {
        int i, j, side1, side2;
        puts("please input the sides.");
        do
        {
            printf("side1 = "); scanf("%d", &side1);
            printf("side2 = "); scanf("%d", &side2);
            if (side1 == side2)
                puts("side1 == side2!");
        }
        while (side1 == side2);
        
        if (side1 >= side2)
        {
            for(i = 1; i <= side2; i++)
            {
                for(j = 1; j <= side1; j++)
                {
                    putchar('*');
                }
                putchar('\n');
            }
        }
        else
        {
            for(i = 1; i <= side1; i++)
            {
                for(j = 1; j <= side2; j++)
                {
                    putchar('*');
                }
                putchar('\n');
            }
        }
        return 0;
    }

    2、for语句

    #include <stdio.h>
    
    int main(void)
    {
        int i, j, side1, side2, shorter, longer;
        puts("please input the sides.");
        do
        {
            printf("side1 = "); scanf("%d", &side1);
            printf("side2 = "); scanf("%d", &side2);
            if(side1 == side2)
                puts("side1 == side2!");
        }
        while(side1 == side2);
        
        shorter = side1 > side2 ? side2:side1;
        longer  = side1 > side2 ? side1:side2;
        
        for(i = 1; i <= shorter; i++)
        {
            for(j = 1; j <= longer; j++)
            {
                putchar('*');
            }
            putchar('\n');
        }
        return 0;
    }

    3、

    #include <stdio.h>
    
    int main(void)
    {
        int i, j, height, width, tmp;
        puts("please input the height and width.");
        do
        {
            printf("height = "); scanf("%d", &height);
            printf("width = "); scanf("%d", &width);
            if (height == width)
                puts("height == width!");
        }
        while (height == width);
        
        if (height > width)
        {
            tmp = height;
            height = width;
            width = tmp;
        }
        
        for(i = 1; i <= height; i++)
        {
            for(j = 1; j <= width; j++)
            {
                putchar('*');
            }
            putchar('\n');
        }
        return 0;
    }
  • 相关阅读:
    Python2-列表
    C#1-变量和表达式
    Python1-变量
    grep输出带有颜色设置
    ftp服务器的安装与使用
    慕课网-哒哒租车系统
    ulimit 命令
    ARP与RARP
    return 和exit
    java运行机制
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14707530.html
Copyright © 2020-2023  润新知