当前位置:网站首页>【C语言练习——打印空心上三角及其变形】
【C语言练习——打印空心上三角及其变形】
2022-06-26 15:03:00 【初学C语言者】
活动地址:毕业季·进击的技术er
夏日炎炎,热浪中我们迎来毕业季,这是告别,也是迈向新起点的开始。这是一个毕业的月份,每年看到很多毕业生都离校上班,在校生准备考试和暑假实习,都非常感慨,不管怎样,作为在校生,玩好的同时,还是要能够扎实的学习,打好坚实基础,从初学C语言开始,为后续的实习找工作做准备。
前言
前面已经在 【C语言练习——打印上三角及其变形】 、【C语言练习——打印上三角及其变形(带空格版)】 、【C语言练习——打印空心下三角及其变形】练过打印上三角及其变形、打印空心三角形及其变形了。
本文在此基础上,练习打印空心上三角及其变形,如下图所示:分别是上三角的无空格版、带空格版、空心版

1、打印空心上三角
1.1 空心上三角左对齐版
//打印空心上三角———左对齐版
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
int j = 0;
for (j = 0; j < n - i; j++)
{
//满足条件输出 * ,即在边界上就是 *
if (i == 0 || j == 0 || j == n - i - 1 )
printf("* ");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
}
return 0;
}
结果见下图:

1.2 空心上三角中间对齐版
//打印空心上三角———中间对齐版
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
int j = 0;
for (j = 0; j <= i; j++)
{
printf(" ");
}
for (j = 0; j < n - i; j++)
{
//满足条件输出 * ,即在边界上就是 *
if (i == 0 || j == 0 || j == n - i - 1 )
printf("* ");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
}
return 0;
}
结果见下图:

1.3 空心上三角右对齐版
//打印空心上三角———右对齐版
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
int j = 0;
for (j = 0; j <= 2 * i; j++)
{
printf(" ");
}
for (j = 0; j < n - i; j++)
{
//满足条件输出 * ,即在边界上就是 *
if (i == 0 || j == 0 || j == n - i - 1)
printf("* ");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
}
return 0;
}
结果见下图:

总结
本文练习打印空心上三角其变形,主要练习了:
- 外循环、内循环的应用
- 注意行数、空格数、符号*数之间的数学表达式
活动地址:毕业季·进击的技术er
边栏推荐
- Redis集群消息
- Lexin AWS IOT expresslink module achieves universal availability
- Optimizing for vectorization
- Idea shortcut key
- 【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
- 打新债注册开户安全吗,有没有什么风险?
- Halcon C# 设置窗体字体,自适应显示图片
- 同花顺注册开户安全吗,有没有什么风险?
- 【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
- R language dplyr package bind_ The rows function merges the rows of the two dataframes vertically. The final number of rows is the sum of the rows of the original two dataframes (combine data frames)
猜你喜欢

【小程序实战系列】小程序框架 页面注册 生命周期 介绍

Restcloud ETL extraction de données de table de base de données dynamique

10分钟了解BIM+GIS融合,常见BIM数据格式及特性

TS common data types summary

1. accounting basis -- several major elements of accounting (general accounting theory, accounting subjects and accounts)
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction](/img/05/8ec56393cac534cb5a00c10a1a9f32.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction

【TcaplusDB知识库】TcaplusDB系统管理介绍

小程序:uniapp解决 vendor.js 体积过大的问题
![[tcapulusdb knowledge base] Introduction to tcapulusdb general documents](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb general documents
![[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested](/img/f9/8e2b55a33e1613a49e08e119ae0086.png)
[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested
随机推荐
5张图诠释了容器网络
One click GCC script installation
Pod of kubernetes
cluster addslots建立集群
There are so many vulnerabilities in tcp/ip protocol?
The DOTPLOT function in the epidisplay package of R language visualizes the frequency of data points in different intervals in the form of point graphs, specifies the grouping parameters with the by p
[async/await] - the final solution of asynchronous programming
Shell script multi process concurrent writing method example (high level cultivation)
Pod scheduling of kubernetes
Talk about the RPA direction planning: stick to simple and valuable things for a long time
Halcon C# 设置窗体字体,自适应显示图片
Redis transaction and watch instruction
Is the QR code for account opening given by the manager of the securities firm safe? Who can I open an account with?
Unity C# 网络学习(十)——UnityWebRequest(二)
[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
Lexin AWS IOT expresslink module achieves universal availability
【TcaplusDB知识库】TcaplusDB OMS业务人员权限介绍
【毕业季·进击的技术er】 什么是微信小程序,带你推开小程序的大门
功能:crypto-js加密解密