Day 2: Operators(操作数,四舍五入,向上,向下取整)

 

1.HackerRank中的C++代码:

#include <bits/stdc++.h>

using namespace std;

// Complete the solve function below.
void solve(double meal_cost, int tip_percent, int tax_percent) {
    double tip,tax;
    int sum;
    tip=meal_cost*tip_percent/100;
    tax=meal_cost*tax_percent/100;
    sum=meal_cost+tip+tax;
    cout << round(sum) << endl; // 使用round() 四舍五入。
                                // 或者使用 int(sum+0.5),四舍五入。
                                // 使用强制类型转换,只去除小数部分,降低精度。
}

int main()
{
    double meal_cost;
    cin >> meal_cost;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    int tip_percent;
    cin >> tip_percent;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    int tax_percent;
    cin >> tax_percent;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    solve(meal_cost, tip_percent, tax_percent);

    return 0;
}

2.VS2017中的C++代码: 

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <iostream>
using namespace std;

// Complete the solve function below.
void solve(double meal_cost, int tip_percent, int tax_percent) {
	double tip, tax, sum;
	tip = meal_cost * tip_percent / 100;
	tax = meal_cost * tax_percent / 100;
	sum = meal_cost + tip + tax;
	cout << round(sum) << endl; // 导入<math.h>,使用round() 四舍五入。
                                // 或者使用 int(sum+0.5),四舍五入。
                                // 使用强制类型转换,只去除小数部分,降低精度。
}

int main()
{
	double meal_cost;
	cout << "meal_cost:";
	cin >> meal_cost;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	int tip_percent;
	cout << "tip_percent:";
	cin >> tip_percent;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	int tax_percent;
	cout << "tax_percent:";
	cin >> tax_percent;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	solve(meal_cost, tip_percent, tax_percent);

	system("pause"); //VS2017加上这句话,防止程序运行完直接退出
	return 0;
}

 3.HackerRank中的python3代码:

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the solve function below.
def solve(meal_cost, tip_percent, tax_percent):
    tip=meal_cost*tip_percent/100;
    tax=meal_cost*tax_percent/100;
    sum=meal_cost+tip+tax;
    print(round(sum));  # 四舍五入

if __name__ == '__main__':
    meal_cost = float(input())

    tip_percent = int(input())

    tax_percent = int(input())

    solve(meal_cost, tip_percent, tax_percent)

总结:

  C++:

         int()       直接截去小数部分(返回值为整型),使用强制类型转换,只去除小数部分,降低精度。
         floor()    向下取整(返回值为整型)      导入math.h:   #include <math.h>

         ceil()      向上取整(返回值为整型)      导入math.h:   #include <math.h>
         round()  四舍五入(返回值为整型)      导入math.h:   #include <math.h> 

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    double a=2.5;
    cout<<ceil(a)<<endl;   //向上取整
    cout<<floor(a)<<endl;   //向下取整
    cout<<round(a)<<endl;   //四舍五入
    //不使用函数实现
    //向下取整
    cout<<(int)a<<endl;
    //向上取整
    cout<<(int)a+1<<endl;
    //四舍五入
    cout<<(int)(a+0.5)<<endl;
    return 0;

}

numeric_limits<> :对于给定的基础类型用来判断在当前系统上的最大值、最小值 

cin.ignore(numeric_limits<streamsize>::max(), '\n');

 这里是设置要忽略的最大字符数,cin忽略输入流大小的上限,不限输入的字符数。 

python3:

     int()       直接截去小数部分(返回值为整型)
     math.floor()    向下取整(返回值为整型)   导入math :import math

     math.ceil()    向上取整(返回值为整型)     导入math :import math
     round()  四舍五入(返回值为整型)            导入math :import math

import math

if __name__ == '__main__':
    sum=1.89
    print(round(sum));      # 四舍五入  输出2

    print(int(sum+0.5));    # 不用函数的四舍五入 输出2

    print(math.floor(sum)); # 向下取整  输出1

    print(math.ceil(sum));  # 向上取整  输出1

    print(int(sum));        # 取整,降低精度  输出1

    

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值