Cod sursa(job #2348157)

Utilizator zettaIonel Herescu zetta Data 19 februarie 2019 13:55:38
Problema Pascal Scor 20
Compilator rs Status done
Runda Arhiva de probleme Marime 1.66 kb
use std::{i8, i16, i32, i64, u8, u16, u32, u64, isize, usize, f32, f64};
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::error::Error;

use std::io::stdin;

fn main() {
    let mut file = File::open("pascal.in").expect("Can't open file");
    let mut content = String::new();

    file.read_to_string(&mut content).expect("Can't read the file");


    let mut inSplitted = content.split_whitespace();
    let mut rowStr = inSplitted.next().expect("Couldn't get the row data");
    let mut divisibleByStr = inSplitted.next().expect("Couldn't get the divisible data");;

    let row: u64 = rowStr.parse::<u64>().unwrap();
    let divisibleBy: u64 = divisibleByStr.parse::<u64>().unwrap();
    let mut count = 0;
    let mut prevElement: u64 = 1;

    if prevElement % divisibleBy == 0 {
        count+=2;
    }

    for i in 0..row/2 {
        let currentElement = (prevElement * (row - i)) / (i + 1);
        prevElement = currentElement;

        if currentElement % divisibleBy == 0 {
            if i == row/2 - 1 {
                count += 1;
            } else {
                count += 2;
            }
        }
    }

    let path = Path::new("pascal.out");
    let display = path.display();

    let mut outFile = match File::create(&path) {
        Err(why) => panic!("couldn't create {}: {}",
                           display,
                           why.description()),
        Ok(outFile) => outFile,
    };

    match outFile.write_all(count.to_string().as_bytes()) {
        Err(why) => {
            panic!("couldn't write to {}: {}", display,
                                               why.description())
        },
        Ok(_) => {},
    }
}