weixin_42207215
weixin_42207215
采纳率25%
2018-10-23 05:14 阅读 1.7k
已采纳

C语言打印正三角形,使用/\_

图片说明
如图,如何用C语言打印此三角形?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2018-10-23 06:09
    // Q703885.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int size = 2;
        int level = 3;
        for (int i = 0; i < level; i++)
        {
            for (int k = 0; k < size; k++)
            {
                for (int j = 0; j < (level - i) * size - k - 1; j++)
                    printf(" ");
                if (k != size - 1)
                {
                    for (int j = 0; j <= i; j++)
                    {
                        printf("/");
                        for (int l = 0; l < k * 2; l++)
                            printf(" ");
                        printf("\\");
                        for (int l = 0; l < (size - k - 1) * 2; l++)
                            printf(" ");
                    }
                }
                else
                {
                    for (int j = 0; j <= i; j++)
                    {
                        printf("/");
                        for (int l = 0; l < (size - 1) * 2; l++)
                            printf("_");
                        printf("\\");
                    }
                }
                printf("\n");
            }       
        }
        return 0;
    }
    
    

    图片说明

    如果问题解决,麻烦点下我回答右边的采纳,谢谢

    点赞 评论 复制链接分享
  • qq_40946921 Italink 2018-10-23 05:20
     #include<stdio.h>
    int main() {
        int level, size;
        printf("Enter level and size :");
        scanf("%d%d", &level, &size);
        int left, right,
            width = size * 2;  //小三角形的宽度
        for (int i = 0; i < level*size; i++) {
            for (int j =level * size -1-i; j >= 0; j--)     //输出每行前的空格
                printf(" ");
            left = 0;       //行左边界(可省略)
            right = 2 * i+1;  //行右边界
            for (int j = left; j <= right; j++) {
                if (j%width == 0)                  //与左边界相比每隔width个打印"/"
                    printf("/");
                else if ((right - j) % width == 0) //与右边界相比每隔width个打印"\"
                    printf("\\");
                else if ((i + 1) % size == 0)      //每隔size行不打印空格,打印"_"
                    printf("_");
                else                               //补上空格
                    printf(" ");
            }
            printf("\n");
        }
        return 0;
    }
    
    点赞 评论 复制链接分享

相关推荐