Rounded small trick

Question meaning: Portal
Solution: If the classification is based on codeforces codeforcesc o d e f o r c e s is the simplestimplementation implementationi m p l e m e n t i o n , but it stuck me for a long time, and the meaning of the question alsoemphasizes that(a bitter face) needs to be rounded, so what are the small tricks for rounding? First understand that there is around roundR & lt O U n- D function, it may be directly used, thisarticleso that nothing in fact have to be programmed in terms of tangled, such as another is directlyxxx is rounded to a whole number, thenx=(x*1+0.5)/1.0, then rounding to a few decimal places is also written,x=(x*100+0.5)/100.0that is,to keep two decimal places.
Attach the code (use the round function directly):

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=70;
db stus[N][10];
int n;
db sum;
int main()
{
    
    
    sc(n);
    rep(i,1,n){
    
    
        rep(j,1,8){
    
    
            db t;
            scanf("%lf",&t);
            if(j==2&&stus[i][1]!=2)sum+=t;
            stus[i][j]=t;
        }
        if(stus[i][1]==2)stus[i][9]=0;
        else{
    
    
            stus[i][9]+=round(stus[i][3]*stus[i][4]+stus[i][5]*stus[i][6]+stus[i][7]*stus[i][8]);
        }
    }
    db res=0;
    rep(i,1,n){
    
    res+=stus[i][9]*(stus[i][2]/sum);}
    printf("%.2f\n",res);
    return 0;
}

The second type: round up manually by yourself

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define rush() int T;scanf("%d",&T);while(T--)
#define mm(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define fi first
#define se second
#define db double
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=70;
db stus[N][10];
int n;
db sum;
int main()
{
    
    
    sc(n);
    rep(i,1,n){
    
    
        rep(j,1,8){
    
    
            db t;
            scanf("%lf",&t);
            if(j==2&&stus[i][1]!=2)sum+=t;
            stus[i][j]=t;
        }
        if(stus[i][1]==2)stus[i][9]=0;
        else{
    
    
            for(int k=3;k<=7;k+=2){
    
    
                stus[i][9]+=stus[i][k]*stus[i][k+1];
            }
        }
    }
    db res=0;
    rep(i,1,n){
    
    
        int g=(int)(stus[i][9]*1+0.5)/1.0;
        res+=(g*(stus[i][2]/sum));
    }
    res=(int)(res*100+0.5)/100.0;
    printf("%.2f\n",res);
    return 0;
}

Guess you like

Origin blog.csdn.net/zhouzi2018/article/details/104034513