C++向上取整,向下取整,四舍五入实例解读---L1-015. 跟奥巴马一起画方块
2018-01-31 17:27
691 查看
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!
输入格式:
输入在一行中给出正方形边长N(3<=N<=21)和组成正方形边的某种字符C,间隔一个空格。
输出格式:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。
输入样例:
输出样例:
代码示例:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double n;
int line,column,i,j;//line代表横行,column代表竖列。
char c;
cin>>n>>c;
line=round(n/2);//round四舍五入,ceil向上取整,floor向下取整
column=n;
for(i=1;i<=line;i++)
{
for(j=1;j<=column;j++)
{
cout<<c;
}
//cout<<endl;
if(i!=line)
{
cout<<endl;
}
}
}
三个函数均在math文件头中。
以下是关于这三个函数的用法详解:
Requirements
输入格式:
输入在一行中给出正方形边长N(3<=N<=21)和组成正方形边的某种字符C,间隔一个空格。
输出格式:
输出由给定字符C画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的50%(四舍五入取整)。
输入样例:
10 a
输出样例:
aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaaaaaaaaaaaa
代码示例:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double n;
int line,column,i,j;//line代表横行,column代表竖列。
char c;
cin>>n>>c;
line=round(n/2);//round四舍五入,ceil向上取整,floor向下取整
column=n;
for(i=1;i<=line;i++)
{
for(j=1;j<=column;j++)
{
cout<<c;
}
//cout<<endl;
if(i!=line)
{
cout<<endl;
}
}
}
三个函数均在math文件头中。
以下是关于这三个函数的用法详解:
floor
Syntax
double floor( double x ); float floor( float x ); // C++ only long double floor( long double x ); // C++ only float floorf( float x ); long double floorl( long double x );
Requirements
Function | Required header |
---|---|
floor, floorf, floorl | <math.h> |
Example
// crt_floor.c // This example displays the largest integers // less than or equal to the floating-point values 2.8 // and -2.8. It then shows the smallest integers greater // than or equal to 2.8 and -2.8. #include <math.h> #include <stdio.h> int main( void ) { double y; y = floor( 2.8 ); printf( "The floor of 2.8 is %f\n", y ); y = floor( -2.8 ); printf( "The floor of -2.8 is %f\n", y ); y = ceil( 2.8 ); printf( "The ceil of 2.8 is %f\n", y ); y = ceil( -2.8 ); printf( "The ceil of -2.8 is %f\n", y ); }Output
The floor of 2.8 is 2.000000 The floor of -2.8 is -3.000000 The ceil of 2.8 is 3.000000 The ceil of -2.8 is -2.000000
ceil
Syntax
double ceil( double x ); float ceil( float x ); // C++ only long double ceil( long double x ); // C++ only float ceilf( float x ); long double ceill( long double x );
Requirements
Routine | Required header |
---|---|
ceil, ceilf, ceill | <math.h> |
Example
See the example for floor.round
Syntax
double round( double x ); float round( float x ); // C++ only long double round( long double x ); // C++ only float roundf( float x ); long double roundl( long double x );
Requirements
Routine | Required header |
---|---|
round, roundf, roundl | <math.h> |
Example
// crt_round.c // Build with: cl /W3 /Tc crt_round.c // This example displays the rounded results of // the floating-point values 2.499999, -2.499999, // 2.8, -2.8, 2.5 and -2.5. #include <math.h> #include <stdio.h> int main( void ) { double x = 2.499999; float y = 2.8f; long double z = 2.5; printf("round(%f) is %.0f\n", x, round(x)); printf("round(%f) is %.0f\n", -x, round(-x)); printf("roundf(%f) is %.0f\n", y, roundf(y)); printf("roundf(%f) is %.0f\n", -y, roundf(-y)); printf("roundl(%Lf) is %.0Lf\n", z, roundl(z)); printf("roundl(%Lf) is %.0Lf\n", -z, roundl(-z)); }Output
round(2.499999) is 2 round(-2.499999) is -2 roundf(2.800000) is 3 roundf(-2.800000) is -3 roundl(2.500000) is 3 roundl(-2.500000) is -3
相关文章推荐
- 团体程序设计天梯赛-练习集L1-015. 跟奥巴马一起画方块
- Sql Server 里的向上取整、向下取整、四舍五入取整的实例!
- L1-015. 跟奥巴马一起画方块
- L1-015. 跟奥巴马一起画方块
- PAT_L1-015. 跟奥巴马一起画方块
- L1-015. 跟奥巴马一起画方块
- L1-015. 跟奥巴马一起画方块
- L1-015. 跟奥巴马一起画方块(C++)
- Sql 获取向上取整、向下取整、四舍五入取整的实例
- Sql Server 里的向上取整、向下取整、四舍五入取整的实例!
- L1-015. 跟奥巴马一起画方块
- Sql Server 里的向上取整、向下取整、四舍五入取整的实例!
- L1-015. 跟奥巴马一起画方块 Java
- L1-015. 跟奥巴马一起画方块
- 天梯赛练习L1-015. 跟奥巴马一起画方块
- 天梯赛题解--L1-015. 跟奥巴马一起画方块
- L1-015. 跟奥巴马一起画方块
- L1-015. 跟奥巴马一起画方块
- 团体程序设计天梯赛-练习集L1-015. 跟奥巴马一起画方块
- 【C++】 / % 四舍五入 向上取整(ceil()) 向下取整(floor())