竞赛
考级
数学函数 题目解析 本道题目考查三个函数的使用。 C++下直接使用函数就行了 特别注意如果是Python下的round()需要特殊的处理一下。 在 Python 中,round() 函数用于将数字四舍五入到给定的精度。它的语法为: * number: 必需参数,表示要四舍五入的数字。 * ndigits: 可选参数,指定要保留的小数位数。如果省略此参数,则默认对整数进行四舍五入。 具体行为 1. 四舍六入五成双: 当对浮点数进行四舍五入时,round() 函数采用"银行家舍入法"(也称为"四舍六入五成双"),即对于位于两边相等的数字(如 2.5),它会将该数字舍入到最接近的偶数。 * round(2.5) 返回 2,而 round(3.5) 返回 4。 2. 保留指定小数位数: 如果 ndigits 参数给定,round() 将四舍五入到指定的小数位数。 * round(3.14159, 2) 返回 3.14。 3. 整数的舍入: 如果没有提供 ndigits,round() 会将浮点数四舍五入为整数。 * round(3.75) 返回 4。 示例 对于更精确的控制,例如你在代码中使用的 Decimal 对象,可以指定具体的舍入模式,如 ROUND_HALF_UP。 AC代码
OK啦,一个一个判断就可以啦
~~***
#include <bits/stdc++.h> using namespace std; double abb(string b,double a){ if(b=="ceil"){ return ceil(a); }else if(b=="floor"){ return floor(a); }else if(b=="round"){ return round(a); } } int main(){ string b; double a; cin>>b>>a; cout<<abb(b,a); return 0; }
用了函数的
代码如下
#include <stdio.h> #include <math.h> int main() { char op[10]; double n; int res; scanf("%s", op); scanf("%lf", &n); if(op[0] == 'c'){ res = (int)ceil(n); }else if(op[0] == 'f'){ res = (int)floor(n); }else{ res = (int)round(n); } printf("%d\n", res); return 0; }
提交答案之后,这里将显示提交结果~