SHARE
TWEET

Untitled

a guest Jul 18th, 2019 135 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn process_fat_accounts(accounts: &mut Vec<f32>, base: f32) -> f32 {
  2.     let mut sum = 0_f32;
  3.     for i in 0..accounts.len() {
  4.         let money = accounts[i];
  5.         if money > base {
  6.             let f = (money / base).powf(1.3);
  7.             if accounts[i] - f > base {
  8.                 accounts[i] -= f;
  9.                 sum += f;
  10.             }
  11.         }
  12.     }
  13.     sum
  14. }
  15.  
  16. fn process_poor_accounts(accounts: &mut Vec<f32>, base: f32, benefits: f32) {
  17.     let small_amount_limit = |x: f32| -> f32 {
  18.         if x > 20_f32 {
  19.             20_f32
  20.         } else {
  21.             x
  22.         }
  23.     };
  24.  
  25.     /*
  26.     real    0m2.385s
  27.     user    0m2.360s
  28.     sys 0m0.019s
  29.     */
  30.     let mut all_factors = 0_f32;
  31.     for i in 0..accounts.len() {
  32.         let mm = accounts[i];
  33.         if mm < base {
  34.             all_factors += small_amount_limit(base / mm);
  35.         }
  36.     }
  37.  
  38.     let amount_to_add = benefits / all_factors;
  39.  
  40.     for i in 0..accounts.len() {
  41.         let money = accounts[i];
  42.         if money < base {
  43.             let f = small_amount_limit(base / money) * amount_to_add;
  44.             accounts[i] += f;
  45.         }
  46.     }
  47. }
  48.  
  49. //use std::mem;
  50. fn main() {
  51.     const NUM: usize = 1000000;
  52.     let mut accounts: Vec<f32> = Vec::with_capacity(NUM);
  53.  
  54.     //let mut accounts = [0_f32; 10000];
  55.     for i in 0..NUM {
  56.         accounts.push((i * 2) as f32);
  57.     }
  58.  
  59.     //println!("account2: {} bytes in the stack", mem::size_of_val(&accounts2));
  60.  
  61.     let base: f32 = accounts.iter().sum::<f32>() / accounts.len() as f32;
  62.     println!("base: {:.2}", base);
  63.  
  64.     for i in 0..100 {
  65.         let benefits = process_fat_accounts(&mut accounts, base);
  66.         process_poor_accounts(&mut accounts, base, benefits);
  67.         //println!("[ {}] счета {:.2?} снято с  жирных счетов #{:.2}", i, accounts.iter().take(10), benefits);
  68.         println!(
  69.             "[ {}] снято с  жирных счетов #{:.2}",
  70.             i, benefits
  71.         );
  72.         //accounts.iter().fold(String::new(), |acc, &num| acc + &num.to_string() + ", ")
  73.     }
  74.     println!("processed:");
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top