跟奥巴马一起编程 (15分)
总结:
1.按题目条件,直接打印。
2.四舍五入:
①使用round函数,需要头文件#include <cmath>。注意类型round(double , double ),返回值也为double。
②分析:偶数可以整除,奇数/2 四舍五入即是(col+1)/2 或 col/2 + col%2 。
3.注意第二个输入字符,类型char,格式符%c。
代码:
#include <cstdio>
#include <cmath> //round函数 头文件
int main(){
int col,row; //column列 row行
char x; //字符
scanf("%d %c",&col,&x);
//row = (int)round( (double)col/2.0 ); //round四舍五入 注意类型转换double
if(col%2) row=(col+1)/2;
else row = col/2;
//row1
for(int j=0; j<col; j++)
printf("%c", x);
printf("\n");
//中间row-2行
for(int i=0; i<row-2; i++){
printf("%c", x);
for(int j=1; j<col-1; j++)
printf(" ");
printf("%c", x);
printf("\n");
}
//row m
for(int j=0; j<col; j++)
printf("%c", x);
return 0;
}