L1-015 跟奥巴马一起画方块 (15 分)
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!
输入格式:
输入在一行中给出正方形边长N(3≤N≤21)和组成正方形边的某种字符C,间隔一个空格。
输出格式:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。
输入样例:
10 a
输出样例:
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
这个题不太会做 就用的本方法 判断 它的数 四舍五入
可是在写这个博客的过程中 突然想到不是奇数就是偶数 oh Mygod 笨死了
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int a;
char c;
int i, j;
scanf("%d %c", &a, &c);
double b = (double) a / 2;
int d = b * 10;
int e;
e = d % 10;
if(e >= 5)
{
d = (d / 10) + 1;
}
else
{
d = d / 10;
}
//printf("%.2f %d %d\n", b, d, e);
for(i = 1; i <= d; i++)
{
for(j = 1; j <= a; j++)
{
printf("%c", c);
}
printf("\n");
}
return 0;
}
一个余数取出来判断是不是大于五 唉气 笨死了
想通了然后代码就这样简单了
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int a, b;
char c;
int i, j;
scanf("%d %c", &a, &c);
if(a % 2 != 0)
{
b = (a / 2) + 1;
}
else
{
b = a / 2;
}
for(i = 1; i <= b; i++)
{
for(j = 1; j <= a; j++)
{
printf("%c", c);
}
printf("\n");
}
return 0;
}