C++打印菱形相关图形
1.打印 * 型菱形
/*------------------------
功能:打印*菱形
运行结果为:
*
***
*****
*******
*****
***
*
--------------------------
Author: Zhang Kaizhou
Date: 2019-3-26 16:54:17
-------------------------*/
#include <iostream>
using namespace std;
int main(){
for(int i = 1; i <= 4; i++){ // 上半三角
for(int j = 1; j <= 4 - i; j++){ // 打印空格
cout << ' ';
}
for(int j = 1; j <= 2 * i - 1; j++){ // 打印*号
cout << '*';
}
cout << endl;
}
for(int i = 1; i <= 3; i++){ // 下半三角
for(int j = 1; j <= i; j++){ // 打印空格
cout << ' ';
}
for(int j = 1; j <= 7 - 2 * i; j++){ // 打印*号
cout << '*';
}
cout << endl;
}
return 0;
}
2.打印字母菱形
/*------------------------
功能:打印字母菱形
运行结果为:
A
BBB
CCCCC
DDDDDDD
EEEEE
FFF
G
--------------------------
Author: Zhang Kaizhou
Date: 2019-3-26 17:14:32
-------------------------*/
#include <iostream>
using namespace std;
int main(){
char word = 'A';
for(int i = 1; i <= 4; i++){ // 打印上三角形
for(int j = 1; j <= 4 - i; j++){
cout << ' ';
}
for(int j = 1; j <= 2 * i - 1; j++){
cout << (char)word;
}
word++;
cout << endl;
}
for(int i = 1; i <= 3; i++){ // 打印下三角形
for(int j = 1; j <= i; j++){
cout << ' ';
}
for(int j = 1; j <= 7 - 2 * i; j++){
cout << (char)word;
}
word++;
cout << endl;
}
return 0;
}
3.打印空心菱形
/*------------------------
功能:打印空心菱形
运行结果:
*
* *
* *
* *
* *
* *
*
--------------------------
Author: Zhang Kaizhou
Date: 2019-3-26 17:36:20
-------------------------*/
#include <iostream>
using namespace std;
int main(){
for(int i = 1; i <= 4; i++){ // 打印上三角形
for(int j = 1; j <= 4 - i; j++){
cout << ' ';
}
for(int j = 1; j <= 2 * i - 1; j++){
if(j == 1 || j == 2 * i - 1){
cout << '*';
}else{
cout << ' ';
}
}
cout << endl;
}
for(int i = 1; i <= 3; i++){ // 打印下三角形
for(int j = 1; j <= i; j++){
cout << ' ';
}
for(int j = 1; j <= 7 - 2 * i; j++){
if(j == 1 || j == 7 - 2 * i){
cout << '*';
}else{
cout << ' ';
}
}
cout << endl;
}
return 0;
}
还没有评论,来说两句吧...