Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const STDIN_FILENO: i32 = 0;
- const STDOUT_FILENO: i32 = 1;
- enum FILE {}
- extern {
- #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
- link_name = "fputs$UNIX2003")]
- fn fputs(s: *const u8, stream: *mut FILE)-> i32;
- fn fgets(buf: *mut u8, n: i32, stream: *mut FILE) -> *mut u8;
- fn fdopen(fd: i32, mode: *const u8) -> *mut FILE;
- }
- fn main() {
- let mut chars: [u8; 15] = unsafe { std::mem::zeroed() };
- unsafe {
- let input = fdopen(STDIN_FILENO, "r\0".as_ptr());
- if fgets(chars.as_mut_ptr(), chars.len() as i32, input) == std::ptr::null_mut() {
- panic!();
- }
- let mut n: u32 = 0;
- for &c in chars.iter() {
- if c == b'\n' {
- break;
- }
- n = n * 10 + (c - b'0') as u32;
- }
- let mut tuples = std::iter::repeat(String::new())
- .take(101)
- .collect::<Vec<_>>();
- for _ in 0..n {
- chars = std::mem::zeroed();
- if fgets(chars.as_mut_ptr(), chars.len() as i32, input) == std::ptr::null_mut() {
- panic!();
- }
- let mut j = chars.len() - 1;
- while !chars[j].is_ascii_digit() {
- j -= 1;
- }
- let chars_count = j + 2; // 123\n
- let mut m = 0u8;
- let mut multiplier = 1;
- while chars[j] != b' ' {
- let digit = chars[j] - b'0';
- m += digit * multiplier;
- if j == 0 || multiplier == 100{
- break;
- }
- j -= 1;
- multiplier *= 10;
- }
- tuples[100 - m as usize].push_str(std::str::from_utf8_unchecked(&chars[0..chars_count]));
- }
- let output = fdopen(STDOUT_FILENO, "w\0".as_ptr());
- for g in &mut tuples {
- g.push('\0');
- fputs(g.as_ptr(), output);
- }
- };
- }
Add Comment
Please, Sign In to add comment