• 2186 ACM 水题 int 向下取整


    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2186

    扩展:

    #include <cstdio>

    使用floor函数。floor(x)返回的是小于或等于x的最大整数。
    如:     floor(10.5) == 10    floor(-10.5) == -11   floor(10)==10

    使用ceil函数。ceil(x)返回的是大于x的最小整数。
    如:     ceil(10.5) == 11    ceil(-10.5) ==-10   ceil(10)==10

    floor()是向负无穷大舍入,floor(-10.5) == -11;
    ceil()是向正无穷大舍入,ceil(-10.5) == -10;

    心得:

    今天用到ceil() 向下取整的时候遇到报错:有多个重载函数ceil实例与参数列表相匹配!;

    原因是没有指定输入参数的类型,导致程序不知道该匹配哪个重载函数;

    只要加上数据类型在前面就好了。

    例如 k1=ceil(a/10); 改为  k1=ceil((double)a/10);

    代码:

    #include <iostream>
    #include <cstdio>
    #include<cmath>
    using namespace std;
    
    int main()
    {
        int n;
        cin>>n;
        while(n--)
        {
            int m,k1=1,k2=1,k3=1;
            cin>>m;
            int a,b,c;
            a=m/2;
            b=(m-a)*2/3;
            c=m-a-b;
            k1=ceil((double)a/10);
            k2=ceil((double)b/10);
            k3=ceil((double)c/10);
            cout<<k1+k2+k3<<endl;
        }
        return 0;
    }
  • 相关阅读:
    创建本地源,使用yum install
    查找SCAN大量块的一个sql
    好的代码像首诗,差的代码像坨屎。
    ps
    eclipse程序正确运行却有红叉
    JS中文乱码解决方案
    初学JQuery
    初学JQuery 2
    大神的电脑软件
    eclipse导入已存在于workspace的项目
  • 原文地址:https://www.cnblogs.com/CheeseIce/p/9649395.html
Copyright © 2020-2023  润新知