2 条题解

  • 1
    @ 2023-10-23 12:43:53

    头文件cmath里的round中文意思是圆、完整的

    round正常用法:

    对于小数而言,round()函数仅仅保留到整数位,即仅仅对​小数点后一位四舍五入​,

    round(1.5)=2.000000
    round(1.56)=2.000000
    round(-1.5)=-2.000000
    round(-1.56)=-2.000000
    

    round保留小数用法:

    如果想要​保留小数位数​,则可以先乘后除

    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
    	double x=1.5684;
    	printf("对1.5684保留两位有效数字:");
    	printf("%.2lf\n",round(x*100)/100);
    
    	return 0;
     } 
    
    
    对1.5684保留两位有效数字:1.57
    

    题中要把个位数四舍五入到十位数.既然round函数只能处理小数的四舍五入,

    我们可以把 个位移到小数位 也就是把结果/10

    x*(z*0.1)=683.76

    x*(z*0.1)10=68.376

    round(68.376) 小数部分 .376就被舍掉了 ,剩下68,68*10=680

    #include<iostream>
    #include<cmath>
    using namespace std;
    int x,d;
    double z,y;
    int main(){
    	cin>>x>>z;
    	y=x*(z*0.1)/10;
    	//cout<<y<<endl;
    	d=round(y);
    	//cout<<d<<endl;
    	cout<<10*d;
    	return 0;
    }
    
    • 0
      @ 2024-7-18 20:06:59
      a,b = input().split()
      a = int(a)
      b = float(b)
      a = a*b*0.1
      a+=5
      a//=10
      a*=10
      print(int(a))
      
      • 1

      信息

      ID
      47
      时间
      1000ms
      内存
      64MiB
      难度
      6
      标签
      递交数
      56
      已通过
      18
      上传者