当前位置:网站首页>【C 练习】打印菱形
【C 练习】打印菱形
2022-07-15 10:34:00 【Domeecky】
例:输入8,打印如下菱形。(输入的数字为菱形上半部分的行数)

我们可以将菱形看做是两部分,如图。

这时我们就可以分为上下两部分打印,假设输入的数字为 line,那么上面部分的行数就为 line,而下面部分的行数就为 line -1。
上半部分每行打印的空格个数如下 :

上半部分每行打印的星号个数如下 :

下半部分打印空格个数如下:

下半部分每行打印的星号个数如下 :

根据这些数据,就可以写出代码了。
#include<stdio.h>
int main()
{
int line = 0;
scanf("%d", &line);
//上半部分
for (int i = 0; i < line; i++)
{
for (int j = 0; j < line-i-1; j++)
printf(" ");
for (int j = 0; j < 2*i+1; j++)
printf("*");
printf("\n");
}
//下半部分
for (int i = 0; i < line - 1; i++)
{
for (int j = 0; j < 1 + i; j++)
printf(" ");
for (int j = 0; j < 2*(line-1-i)-1; j++)
printf("*");
printf("\n");
}
return 0;
}边栏推荐
- Today's sleep quality record is 80 points
- Summary of Halcon common image preprocessing operators
- 善良的人都晚熟
- Cloud native: docker practical experience (III) deploy MySQL 8 master-slave replication on docker
- There are certificates and directories! | Haitai password service platform
- C语言家族树管理系统
- JMeter 21 day clock in day04
- How much do you know about programming paradigms -- clarify the mainstream programming paradigms
- A Tong's little wish
- Node connects to the database for addition, deletion, modification and query
猜你喜欢

Large end byte order and small end byte order

对接企业微信,客户关系管理也可以很简单!

Common vulnerability types of tcp/ip protocol

030.文件的删除和链接

【LaTex 中英文样式&加粗】自由选择中英文字体样式,中英文字体粗黑程度设置

There are certificates and directories! | Haitai password service platform

Matlab bottom source code realizes image corrosion and expansion operation (consistent with Halcon effect)

【HCIA】OSI 模型

基于机器学习的笑脸检测

JMeter 21 天打卡 day04
随机推荐
There are certificates and directories! | Haitai password service platform
Matlab底层源码实现prewitt边缘检测和Sobel,Laplace边缘检测(实现效果与Halcon一致)
矩阵快速幂
手机浏览器产品分析
让AI像婴儿一样思考!DeepMind「柏拉图」模型登Nature子刊
Everyone, oraclecdc always reports this error. Do you know what happened?
Codeforces Round #806 (Div. 4)
云小店商城源码修复30套模板支持一键对接各大系统
Learning letter cup -train code learning
坚持写下去的原因
H5 page in wechat evokes applet & app
计算方法工程数学第一节课Doolittle
C语言实现手机通讯录
How Axure achieves screen adaptation
网站ICP备案是什么呢?
001 an instance of an internal class gets an instance of its external class (reflection)
Dbpack read / write separation function release announcement
2020CCPC秦皇岛 Exam Results(尺取)
C语言指针相关问题解析
Have you ever encountered the problem of getting the latest log stuck in Flink Oracle CDC? How to solve it?