#include <stdio.h> #include <stdlib.h> int isEven(int number); void printLovingHeart(int number,int left); //打印爱心 void printUpLovingHeart(int number,int left); //打印爱心的上半部分 void printDownTri(int number, int left); //打印边长为number的向下正三角形,左边空left列 void printLovingHeartRow(int number, int row, int left); void printRow(int number, int row, int left); int main(){ int number; while (fflush(stdin), scanf("%d", &number) != EOF){ if (isEven(number)) printLovingHeart(number,0); else printf("Please input a even number.\n"); } system("pause"); return 0; } int isEven(int number){ if (number % 2 == 0) return 1; else return 0; } void printDownTri(int number, int left){ for (int i = number; i > 0; --i){ //i为当前行*数 printRow(number, i, left); printf("\n"); } } void printUpLovingHeart(int number,int left){ for (int i = 2; i < number/2; ++i) //i为当前行 printLovingHeartRow(number, i, left); } void printLovingHeartRow(int number, int row, int left){ printRow(number/2-1,row,left+1); printRow(number/2-1,row,number/2-row+2); printf("\n"); } void printRow(int number, int row, int left){ for (int j = 0; j < left; ++j) //打印空列 printf(" "); for (int j = 0; j < number - row; ++j) //打印左方空白 printf(" "); for (int j = 0; j < row; ++j) //打印三角形的一行 printf("* "); printf("\b"); } void printLovingHeart(int number,int left){ printUpLovingHeart(number,left); printDownTri(number,left); }
时间: 2024-12-17 23:12:10