c/python_题目描述 从键盘输入一个整数n(1sn≤9),打印出指定的菱形。 输入 正整数n(1sn≤9)。 输出 指定的菱形。第一行前面有n-1个空格,第二行有n-2个空格,以此类推。

文章目录

print result:

在这里插入图片描述

c

//打印菱形
#include <stdio.h>
int main(){
int n;
int m;
scanf("%d",&n);
for(int i = 1;i <= n;i ++){
//先打印该行的n-1各空格(连续打印完)
for(int j = 0;j < n - i;j ++){
printf(" ");
}
//接着打印该行的*号
for(int k = 0;k < 2 * i - 1;k++){
printf("*");
}
printf("\n");//位下一行准备
}
for(int i = 1;i < n;i ++){
//先打印该行的n-1各空格(连续打印完)
for(int j = 0;j < i;j ++){
printf(" ");
}
//接着打印该行的*号
m = 2 * n - 1;
for(int k = 0;k < m - 2 * i;k++){
printf("*");
}
printf("\n");
}
}

python

def print_up_triangle(n):
""" to count the space of each line: """
j=n-1
for i in range(0,n):
print(j*' '+(i*2+1)*'*')
j-=1
def print_down_triangle(n):
""" to count the space of each line: """
j=1
for i in range(n-2,-1,-1):
print(j*' '+(2*i+1)*'*')
j+=1
""" the scale n is the sequence:n= 0,1,2,3,...n """
def print_diamond(n):
print_up_triangle(n)
print_down_triangle(n)
scale=input("input a integer to specify the scale of the diamond to be print: ")
scale=(int)(scale)
print_diamond(scale)
posted @   xuchaoxin1375  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示