当前位置:网站首页>【C语言练习——打印空心正方形及其变形】
【C语言练习——打印空心正方形及其变形】
2022-06-28 05:19:00 【初学C语言者】
活动地址:毕业季·进击的技术er
夏日炎炎,热浪中我们迎来毕业季,这是告别,也是迈向新起点的开始。这是一个毕业的月份,每年看到很多毕业生都离校上班,在校生准备考试和暑假实习,都非常感慨,不管怎样,作为在校生,玩好的同时,还是要能够扎实的学习,打好坚实基础,从初学C语言开始,为后续的实习找工作做准备。
前言
前面已经在【C语言练习——打印正方形及其变形】、【C语言练习——打印空心下三角及其变形】、【C语言练习——打印空心上三角及其变形】 练过打印空心上三角及其变形、打印空心三角形及其变形、打印正方形及其变形。
本文在此基础上,练习打印空心正方形及其变形
1、打印空心正方形
1.1 图形1——无空格版
//打印空心正方形 无空格
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)//行数
{
for (int j = 0; j < n; j++)//列数
{
//满足条件输出 * ,即在边界上就是 *
if (i == 0 || j == 0 || i == n - 1 || j == n - 1)
printf("*");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
}
return 0;
}
结果见下图:

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

1.3 图形3——旋转45度,没有空格版
//打印正方形,旋转45度,没有空格版
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
//打印上半部分 n
for (int i = 0; i < n; i++)
{
//打印一行
//打印空格
int j = 0;
for (j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
//打印*
for (j = 0; j < 2 * i + 1; j++)
{
//满足条件输出 * ,即在边界上就是 *
if (j == 0 || j == 2 * i )
printf("*");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
//打印下半部分 n-1
for (int i = 0; i < n - 1; i++)
{
//打印一行
//打印空格
int j = 0;
for (j = 0; j <= i; j++)
{
printf(" ");
}
//打印*
for (j = 0; j < (n - 1 - i) * 2 - 1; j++)
{
//满足条件输出 * ,即在边界上就是 *
if (j == 0 || j == (n - 1 - i) * 2 - 2)
printf("*");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
}
return 0;
}
结果见下图:

1.4 图形4——旋转45度,有空格版
//打印正方形,旋转45度,有空格版
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
//打印上半部分 n
for (int i = 0; i < n; i++)
{
//打印一行
//打印空格
int j = 0;
for (j = 0; j < n - 1 - i; j++)
{
printf(" ");
}
//打印*
for (j = 0; j <= i; j++)
{
//满足条件输出 * ,即在边界上就是 *
if (j == 0 || i == j)
printf("* ");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
//打印下半部分 n-1
for (int i = 0; i < n - 1; i++)
{
//打印一行
//打印空格
int j = 0;
for (j = 0; j <= i; j++)
{
printf(" ");
}
//打印*
for (j = 0; j < (n - 1 - i); j++)
{
//满足条件输出 * ,即在边界上就是 *
if (j == 0 || j == n - i - 2)
printf("* ");
else
printf(" ");//不在边界上的点都是空格
}
printf("\n");
}
}
return 0;
}
}
结果见下图:

总结
本文练习打印空心正方形及其变形,主要练习了:
- 外循环、内循环的应用
- 注意行数、空格数、符号*数之间的数学表达式
活动地址:毕业季·进击的技术er
边栏推荐
- Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
- Binary sort tree: BST
- 交流电和直流电的区别是什么?
- 基于微信小程序的婚纱影楼门户小程序
- Study on chemical properties and technology of biovendor rage ELISA Kit
- 证明素数/质数有无限多个
- metaRTC5.0编程之p2p网络穿透(stun)指南
- Dart学习——函数、类
- How to learn programmable logic controller (PLC)?
- Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid
猜你喜欢

JS 文本框失去焦点修改全半角文字和符号

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题

How to learn programmable logic controller (PLC)?

Amino dye research: lumiprobe fam amine, 6-isomer

基于微信小程序的婚纱影楼门户小程序

Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems

Share a powerful tool for factor Mining: genetic programming

DH parameters of robotics and derivation using MATLAB symbolic operation
![[leetcode] 12. Integer to Roman numeral](/img/3e/815f24a85a3333ce924acee1856f62.png)
[leetcode] 12. Integer to Roman numeral

Redis 的 最新windows 版本 5.0.14
随机推荐
How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years
[JVM] - memory partition in JVM
Leetcode 88: merge two ordered arrays
【牛客网刷题系列 之 Verilog快速入门】~ 四选一多路器
Feign implements path escape through custom annotations
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
2022电力电缆判断题模拟考试平台操作
Operation of 2022 power cable judgment question simulation examination platform
quartus 复制IP核
证明素数/质数有无限多个
Sqlmap tool user manual
Assembly common instructions
Rxswift -- (1) create a project
When excel copies the contents of a row, the columns are separated by the tab "\t"
Extjs图书管理系统源码 智能化图书管理系统源码
2022 safety officer-b certificate examination question bank and answers
使用class toplevel的messagebox时,窗口弹出问题。
Unity delegate
Interview: what are the similarities and differences between abstract classes and interfaces?
Liuhaiping's mobile phone passes [[uiapplication sharedapplication] delegate] window. safeAreaInsets. The height of the bottom security zone is 0