zoukankan      html  css  js  c++  java
  • Vus the Cossack and Numbers CF-1186D(思维)

    题意:

    给一个浮点数序列$a$,其和为$0$。求一个序列$b$,满足$b[i]=ceil(a[i])$或者$b[i]=floor(a[i])$。

    思路:

    全部数字向下取整,赋值给$b$,统计$b$的和,若和不为$0$,则使原本不为整数的$a[i]$对应的$b[i]+1$,直到和为$0$。

    代码:

     1 //#include<bits/stdc++.h>
     2 #include <set>
     3 #include <map>
     4 #include <stack>
     5 #include <cmath>
     6 #include <queue>
     7 #include <cstdio>
     8 #include <string>
     9 #include <vector>
    10 #include <cstring>
    11 #include <iostream>
    12 #include <algorithm>
    13 
    14 #define ll long long
    15 #define pll pair<ll,ll>
    16 #define pii pair<int,int>
    17 #define bug printf("*********
    ")
    18 #define FIN freopen("input.txt","r",stdin);
    19 #define FON freopen("output.txt","w+",stdout);
    20 #define IO ios::sync_with_stdio(false),cin.tie(0)
    21 #define ls root<<1
    22 #define rs root<<1|1
    23 #define pb push_back
    24 
    25 using namespace std;
    26 const int inf = 2e9 + 7;
    27 const ll Inf = 1e18 + 7;
    28 const int maxn = 1e5 + 5;
    29 const int mod = 1e9 + 7;
    30 
    31 double a[maxn];
    32 int b[maxn];
    33 
    34 int main()
    35 {
    36     int n;
    37     ll sum = 0;
    38     scanf("%d", &n);
    39     for (int i = 1; i <= n; ++i)
    40     {
    41         scanf("%lf", &a[i]);
    42         b[i] = floor(a[i]);
    43         sum += 1LL * b[i];
    44     }
    45     for (int i = 1; i <= n; ++i)
    46     {
    47         if (sum == 0)        break;
    48         if (a[i] - b[i] < 1e-7)    continue;
    49         b[i]++;
    50         sum++;
    51     }
    52     for (int i = 1; i <= n; ++i)    cout << b[i] << endl;
    53 }
  • 相关阅读:
    使用turtle库绘制一个叠加等边三角形
    使用turtle库绘制图形
    tar命令常用参数讲解
    elasticsearch 中geo point地理位置数据类型
    count(*)和count(1)的sql性能分析
    别再if/else走天下了
    正则表达式 匹配0次1次或者无限次
    linux shell 字符串操作(长度,查找,替换)
    linux expect工具使用
    mongodb分片balance
  • 原文地址:https://www.cnblogs.com/zhang-Kelly/p/12686559.html
Copyright © 2011-2022 走看看