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

2个回答

``````// 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;
}

``````

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

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