Advertisement
paranid5

Sirius4

May 24th, 2021
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.87 KB | None | 0 0
  1. use std::fmt::Debug;
  2. use std::io;
  3. use std::iter::FromIterator;
  4. use std::num::ParseIntError;
  5. use std::str::{FromStr, SplitWhitespace};
  6. use std::cmp::{max, min};
  7.  
  8. #[inline]
  9. #[allow(dead_code)]
  10. fn input<T: FromStr<Err = ParseIntError> + Debug>() -> T {
  11.     let mut input = String::new();
  12.     io::stdin().read_line(&mut input).unwrap();
  13.     input.trim().parse().unwrap()
  14. }
  15.  
  16. #[inline]
  17. #[allow(dead_code)]
  18. fn input_container<T, C>() -> C
  19.     where
  20.         T: FromStr<Err = ParseIntError> + Debug,
  21.         C: FromIterator<T>,
  22. {
  23.     let mut input = String::new();
  24.     io::stdin().read_line(&mut input).unwrap();
  25.     input
  26.         .split_whitespace()
  27.         .map(|x| x.parse::<T>().unwrap())
  28.         .collect::<C>()
  29. }
  30.  
  31. #[inline]
  32. #[allow(dead_code)]
  33. fn input_pair<F, S>() -> (F, S)
  34.     where
  35.         F: FromStr<Err = ParseIntError> + Debug,
  36.         S: FromStr<Err = ParseIntError> + Debug,
  37. {
  38.     let mut input = String::new();
  39.     io::stdin().read_line(&mut input).unwrap();
  40.  
  41.     let mut input = input.split_whitespace();
  42.     (
  43.         input.next().unwrap().parse().unwrap(),
  44.         input.next().unwrap().parse().unwrap(),
  45.     )
  46. }
  47.  
  48. #[inline]
  49. #[allow(dead_code)]
  50. fn input_it(mut input: &mut String) -> SplitWhitespace<'_> {
  51.    io::stdin().read_line(&mut input).unwrap();
  52.    input.split_whitespace()
  53. }
  54.  
  55. fn main() {
  56.    let a = input::<i32>();
  57.    let b = input::<i32>();
  58.    let c = input::<i32>();
  59.    let d = input::<i32>();
  60.  
  61.    let max = max(a, b);
  62.    let min = min(a, b);
  63.  
  64.    println!("{}",
  65.             if c == max {
  66.                 if d == 1 { 1 } else { min }
  67.             } else if c == min {
  68.                 if d == 1 { 1 } else { max }
  69.             } else if c > min {
  70.                 if d == 1 { max - c + 1 } else { c - 1 }
  71.             } else {
  72.                 if d == 1 { min - c + 1 } else { max - min + c }
  73.             }
  74.    )
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement