Cod sursa(job #2348161)

Utilizator zettaIonel Herescu zetta Data 19 februarie 2019 14:04:34
Problema Pascal Scor 20
Compilator rs Status done
Runda Arhiva de probleme Marime 1.63 kb
use std::{u64};
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::error::Error;

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 in_splitted = content.split_whitespace();
    let row_str = in_splitted.next().expect("Couldn't get the row data");
    let divisible_by_str = in_splitted.next().expect("Couldn't get the divisible data");;

    let row: u64 = row_str.parse::<u64>().unwrap();
    let divisible_by: u64 = divisible_by_str.parse::<u64>().unwrap();
    let mut count = 0;
    let mut prev_element: u64 = 1;

    if prev_element % divisible_by == 0 {
        count+=2;
    }

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

        if current_element % divisible_by == 0 {
            if row % 2 == 0 && i == row/2 - 1 {
                count += 1;
            } else {
                count += 2;
            }
        }
    }

println!("{}", count);
    let path = Path::new("pascal.out");
    let display = path.display();

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

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