1036 跟奥巴马一起编程
刷PTA小结,记录一下漫漫编程道路上的一点点小的成长…
此题关键为两点:
1.如何使用for循环绘制图形;
2.如何处理四舍五入问题(因为C++没有round()函数,所以我们需要自己去定义)
话不多说,上代码!
#include<iostream>
using namespace std;
int round(int c){//自定义四舍五入函数
int m=(int)c*0.5;
double n=(double)c*0.5;
int h=(n-m>=0.5)?m+1:m;
return h;
}
int main()
{
int n;
char b;
cin>>n>>b;
int h=round(n);
for(int i=1;i<=h;i++){
if(i==1||i==h){ //第一行和最后一行需要打印n个字符
for(int j=0;j<n;j++){
cout<<b;
}
cout<<endl;
}
else{
cout<<b;//打印第一个
for(int j=1;j!=n-1;j++){
cout<<" ";//中间输出空格
}
cout<<b;//打印最后一个
cout<<endl;
}
}
return 0;
}
方法比较笨,请多多指教~