c++字符、字符串的几种类型题
1.输出字母三角形
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++){
string space=string(n-i,’ ');
string ch=string(2*i-1,‘A’+i-1);
cout<<space+ch<<endl;
}
return 0;
}
2.输出字母或者数字三角形
1
121
#include
#include
using namespace std;
int main()
{
char c;
cin>>c;
if(c>=‘A’&&c<=‘Z’){
for(int i=1;i<=c-‘A’+1;i++)/行数/{
for(int j=1;j<=c-‘A’+1-i;j++)/每行空格数/{
cout<<" “;
}
for(int j=1;j<=i;j++){
cout<<(char)(‘A’+j-1);
}
for(int j=i-1;j>=1;j–){
cout<<(char)(‘A’+j-1);
}
cout<<endl;
}
}
else{
for(int i=1;i<=c-‘1’+1;i++)/行数/{
for(int j=1;j<=c-‘1’+1-i;j++)/每行空格数/{
cout<<” ";
}
for(int j=1;j<=i;j++){
cout<<(char)(‘1’+j-1);
}
for(int j=i-1;j>=1;j–){
cout<<(char)(‘1’+j-1);
}
cout<<endl;
}
}
return 0;
}
3.假设地图是一个n行n列的方格地图,地图每个格子代表一个宝藏。比如2行2列的地图如下:
**
**
对应要建的房子如下:
±±+
|||
±±+
||*|
±±+
告诉行列数,画出房子的设计图
输入格式:
一行两个整数n,m(0<=n,m<=50),分别表示地图行数和列数。
#include
#include
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)/行/{
for(int j=1;j<=m;j++){
cout<<“±”;
}
cout<<"+"<<endl;
for(int j=1;j<=m;j++){
cout<<"|*";
}
cout<<"|"<<endl;
}
for(int j=1;j<=m;j++){
cout<<“±”;
}
cout<<"+"<<endl;
return 0;
}
来自计蒜客的题目整理