[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
题目:输入一个数n,输出n行的菱形
思路:首先,考虑n是否需要分情况
其次,怎样形成一个菱形
找行数与每行输出的结果之间的关系进行嵌套
使用工具:vs2022
#include<stdio.h>//输入n,输出n行菱形 int main() { int n; printf("输入一个数: \n");
scanf_s("%d", &n); int a, b, c;//行数,输出次数,空格数 if (n % 2 == 1)//n为奇数才能输出完整的菱形, {
for (a = 1; a <= (n / 2 + 1); a++)//上半部分输出行数 { c = 1;//空格数 for (b = 0; b < a +
(n / 2); b++)//前半部分每行输出次数n/2+1~n次 { if (c <= (n / 2 + 1) - a)//判断是否输出空格,否则输出* {
printf(" "); c++; } else { printf("*"); } } printf("\n"); } for (a = 1; a <= (n
/ 2); a++)//后半部分输出行数 { c = 1; for (b = 1; b <= n - a; b++)//后半部分每行输出次数 { if (c
<= a) { printf(" "); c++; } else { printf("*"); } } printf("\n"); } }
else//输入的n为偶数,菱形不是完整的,按照n-1行输出 { for (a = 1; a <= (n / 2); a++)//前半部分输出行数的循环 {
c = 1;//空格数 for (b = 1; b <= a + (n / 2 - 1); b++)//每行输出次数循环 { if (c <= (n / 2)
- a)//输出空格还是* { printf(" "); c++; } else { printf("*"); } } printf("\n"); } for
(a = 1; a <= (n / 2 - 1); a++)//后半部分输出行数循环 { c = 1; for (b = 1; b <= (n - 1) -
a; b++)//没行输出次数的循环 { if (c <= a)//输出空格还是* { printf(" "); c++; } else {
printf("*"); } } printf("\n"); } } }