Advertisement
paranid5

Сумма цифр

Jul 3rd, 2020
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.81 KB | None | 0 0
  1. use std::io;
  2. use std::str::SplitWhitespace;
  3.  
  4. fn sum_num_ultra(mut x: i128) -> i128 {
  5.     let mut sum: i128 = 0;
  6.     while x > 0 {
  7.         sum += x % 10;
  8.         x /= 10;
  9.     }
  10.     return sum;
  11. } /*общий поиск суммы цифр*/
  12.  
  13. fn main() {
  14.     let mut t: String = String::new();
  15.     io::stdin().read_line(&mut t).unwrap();
  16.     let t: usize = t.trim().parse().unwrap();
  17.     let mut v: Vec<(u32, u32)> = Vec::with_capacity(t);
  18.     v.resize(t, (0, 0));
  19.     for i in 0..t {
  20.         let mut input: String = String::new();
  21.         io::stdin().read_line(&mut input).unwrap();
  22.         let mut it_input: SplitWhitespace = input.split_whitespace();
  23.         let num: u32 = it_input.next().unwrap().parse().unwrap();
  24.         let amn: u32 = it_input.next().unwrap().parse().unwrap();
  25.         v[i].0 = num;
  26.         v[i].1 = amn;
  27.     }
  28.     let mut answer: Vec<i128> = Vec::with_capacity(t);
  29.     for i in v.iter() {
  30.         if i.1 == 0 {
  31.             let mut count: usize = 0;
  32.             let mut target: u32 = i.0;
  33.             while target >= 9 {
  34.                 target -= 9;
  35.                 count += 1;
  36.             }
  37.             let mut str: String = String::with_capacity(count + 1);
  38.             if target > 0 {
  39.                 str.push(std::char::from_digit(target, 10).unwrap());
  40.             }
  41.             for _q in 0..count {
  42.                 str.push(std::char::from_digit(9, 10).unwrap());
  43.             }
  44.             answer.push(str.trim().parse().unwrap());
  45.         } else {
  46.             let mut print: bool = false;
  47.             let mut min: i128 = 69999999999999999;
  48.             for x in 0..10 {
  49.                 let mut pred: i128 = sum_num_ultra(x);
  50.                 let mut sum: i128 = pred;
  51.                 for q in x + 1..x + i.1 as i128 + 1 {
  52.                     if q % 10 != 0 {
  53.                         sum += pred + 1;
  54.                         pred += 1;
  55.                     } else {
  56.                         pred = sum_num_ultra(q);
  57.                         sum += pred;
  58.                     }
  59.                 }
  60.                 if sum == i.0 as i128 {
  61.                     if x < min {
  62.                         min = x;
  63.                     }
  64.                     print = true;
  65.                 } else if sum < i.0 as i128 {
  66.                     let mut target: i128 = i.0 as i128;
  67.                     target -= sum;
  68.                     let mut ur: i128 = 0;
  69.                     let mut nine: bool = false;
  70.                     for b in x..x + i.1 as i128 + 1 {
  71.                         if nine {
  72.                             ur += 1;
  73.                         }
  74.                         if b % 10 == 9 {
  75.                             nine = true;
  76.                         }
  77.                     }
  78.                     target -= ur;
  79.                     if target >= 0 && target % (i.1 as i128 + 1) == 0 {
  80.                         let mut cif: i128 = target / (i.1 as i128 + 1);
  81.                         let mut an: String = String::with_capacity(cif as usize / 9 + 1);
  82.                         an.push(std::char::from_digit(x as u32, 10).unwrap());
  83.                         while cif >= 9 {
  84.                             cif -= 9;
  85.                             an.insert(0, '9');
  86.                         }
  87.                         if cif > 0 {
  88.                             an.insert(0, std::char::from_digit(cif as u32, 10).unwrap());
  89.                         }
  90.                         let a: i128 = an.trim().parse().unwrap();
  91.                         if a < min {
  92.                             min = a;
  93.                         }
  94.                         print = true;
  95.                     }
  96.                 }
  97.             }
  98.             if !print {
  99.                 answer.push(-1);
  100.             } else {
  101.                 answer.push(min);
  102.             }
  103.         }
  104.     }
  105.     for i in answer {
  106.         println!("{}", i);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement