题目:
描述
在数学上,我们发现有一类图形是对称图形。我们对于左右一样的图形叫做沿 y 轴对称,对于上下一样的图形叫做沿 x 轴对称。
如下图所示的图形就是沿 y 轴对称的两个三角形:
而下图所示的图形就是沿 x 轴对称的两个三角形:
请从键盘读入一个整数 n ,代表三角形的行数,再读入一个字符,该字符为 ’x’ 或者 ’y’ ,代表是沿 x 轴对称还是沿 y 轴对称,输出对应的轴对称图形。(8.1)
输入描述
第一样是一个整数 n ( n <= 10 )
第二行是一个字符(字符 ’x’ 或者字符 ’y’ )
输出描述
按照要求输出的轴对称图形
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char ch;
cin>>n>>ch;
if(ch=='x'){
for(int i=1;i<=n;i++){
for(int j=n-i;j>0;j--){
cout<<" ";
}for(int k=1;k<=i*2-1;k++){
cout<<"*";
}cout<<endl;
}
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
cout<<" ";
}for(int k=1;k<=2*(n-i)+1;k++){
cout<<"*";
}cout<<endl;
}
}else if(ch=='y'){
int a=n-1,b=n*2-2;
for(int i=1;i<=n;i++){
for(int j=1;j<=a;j++)
cout<<" ";
for(int k=1;k<=i*2-1;k++)
cout<<"*";
for (int j=1;j<=b;j++)
cout<<' ';
for(int h=1;h<=i*2-1;h++)
cout<<"*";
a--;
b=b-2;
cout<<endl;
}
}
return 0;
}