题目描述:
D. Vus the Cossack and Numbers
Vus the Cossack has nn real numbers aiai. It is known that the sum of all numbers is equal to 00. He wants to choose a sequence bb the size of which is nn such that the sum of all numbers is 00 and each bibi is either ⌊ai⌋⌊ai⌋ or ⌈ai⌉⌈ai⌉. In other words, bibi equals aiai rounded up or down. It is not necessary to round to the nearest integer.
For example, if a=[4.58413,1.22491,−2.10517,−3.70387]a=[4.58413,1.22491,−2.10517,−3.70387], then bb can be equal, for example, to [4,2,−2,−4][4,2,−2,−4].
Note that if aiai is an integer, then there is no difference between ⌊ai⌋⌊ai⌋ and ⌈ai⌉⌈ai⌉, bibi will always be equal to aiai.
Help Vus the Cossack find such sequence!
Input
The first line contains one integer nn (1≤n≤1051≤n≤105) — the number of numbers.
Each of the next nn lines contains one real number aiai (|ai|<105|ai|<105). It is guaranteed that each aiai has exactly 55 digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to 00.
Output
In each of the next nn lines, print one integer bibi. For each ii, |ai−bi|<1|ai−bi|<1 must be met.
If there are multiple answers, print any.
Examples
input
4
4.58413
1.22491
-2.10517
-3.70387
output
4
2
-2
-4
input
5
-6.32509
3.30066
-0.93878
2.00000
1.96321
output
-6
3
-1
2
2
Note
The first example is explained in the legend.
In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down.
思路:
要保证取整后和为零,就要看是怎么去上整和下整。
举个例子:1.99 2.01 -4取整应该是2 2 -4,那么我们怎么知道该取多少个上整或下整呢?就先把正数小数部分加起来记为posit,负数的小数部分的绝对值加起来记为negt,。用posit-negt得到的数记为gap,即表示正数的小数部分和负数的小数部分相差是什么。如果是零,那么好了,直接把数组的数取整就可以输出了。如果gap>0,说明这个时候正数的整数部分和是小于负数的正数部分和的绝对值的。因为正数还需要小数部分的补充才能和负数打个平手,同理,gap<0,说明负数整数和小于正数整数和。其实按理说小数部分加起来后应该是个整数的,要不然和不可能是零,但是,狗血的来了,这个时候gap的类型是double,后面再计数时要转换成int,如果gap=2,说明他在计算机表示上接近2,但不能看成2,直接转换成整数竟然可以等于一!佛了。这时候就要用round函数了(见参考文章1)。
然后根据gap的正负,把相应的数进行加减操作,比如gap>0,那就要把正的而且原来不是整数的数加1,<0就把负的数而且原来不是整数的数减一,最后输出。
怎么判断原来a数组的数是不是整数呢?我一开始是按要求来fabs(a[i]-b[i])<1(b[i]为取整后的数,整数下取整,负数上取整)来判断的,事实证明,这不行,我怎么这么天真无邪善良可爱(傻),精度还是有问题。就在刚刚写到这里,我突然意识到了什么,不四fabs(a[i]-b[i])<1,而四fabs(a[i]-b[i]-1)<1和fabs(a[i]-b[i]+1)<1.怎么会这样子滋滋滋滋滋~。好,这样是可以的,然后就可以修改了。最后输出✿✿ヽ(°▽°)ノ✿(真好)。
再提一点,在过程中我竟然发现了-0这样的输出,还是负的,用了ceil返回值是double,应该直接截取了整数部分。联想到浮点数的表示,嗯,这是个负的零,只能接近零,但不是零。这就要看浮点数在计算机中的表示了。如果指数是 0 并且尾数的小数部分是 0,这个数是 ±0(和符号位有关)(具体参见参考文章2)
代码:
1 #include <iostream> 2 #include <cmath> 3 #define max_n 100005 4 using namespace std; 5 int n; 6 double a[max_n]; 7 int b[max_n]; 8 double posit = 0; 9 double negt = 0; 10 double eps = 10e-8; 11 int main() 12 { 13 //printf("%d ",-2); 14 cin >> n; 15 for(int i = 0;i<n;i++) 16 { 17 cin >> a[i]; 18 if(a[i]>0) 19 { 20 posit += a[i]-floor(a[i]); 21 b[i] = floor(a[i]); 22 } 23 else if(a[i]<0) 24 { 25 negt += ceil(a[i])-a[i]; 26 b[i] = ceil(a[i]); 27 } 28 } 29 int gap = round(posit-negt); 30 //cout << gap << endl; 31 if(gap>0) 32 { 33 for(int i = 0; i<n&⪆ i++) 34 { 35 if(a[i]>0&&fabs(a[i]-b[i]-1)<1) 36 { 37 b[i] = b[i]+1; 38 gap--; 39 } 40 } 41 } 42 else 43 { 44 for(int i = 0;i<n&⪆i++) 45 { 46 if(a[i]<0&&fabs(a[i]-b[i]+1)<1) 47 { 48 b[i] = b[i]-1; 49 gap++; 50 } 51 } 52 } 53 for(int i = 0;i<n;i++) 54 { 55 cout << b[i] << endl; 56 } 57 return 0; 58 59 }
参考文章:
dangzhangjing97,C语言(C++)中:详解floor函数、ceil函数和round函数,https://blog.csdn.net/dangzhangjing97/article/details/81279862
TwinkleStar0121,浮点数在计算机中的表示,https://blog.csdn.net/jvandc/article/details/81176294
需要清醒,清醒