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.ios
要保證取整後和爲零,就要看是怎麼去上整和下整。git
舉個例子: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.怎麼會這樣子滋滋滋滋滋~。好,這樣是能夠的,而後就能夠修改了。最後輸出✿✿ヽ(°▽°)ノ✿(真好)。spa
再提一點,在過程當中我居然發現了-0這樣的輸出,仍是負的,用了ceil返回值是double,應該直接截取了整數部分。聯想到浮點數的表示,嗯,這是個負的零,只能接近零,但不是零。這就要看浮點數在計算機中的表示了。若是指數是 0 而且尾數的小數部分是 0,這個數是 ±0(和符號位有關)(具體參見參考文章2).net
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\n",-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/81279862rest
TwinkleStar0121,浮點數在計算機中的表示,https://blog.csdn.net/jvandc/article/details/81176294code
須要清醒,清醒blog