Category: Rust Exercise

  • Rust練習問題:数の整除

    問題文

    定理:二桁以上の正整数であれば、その一の位を取り除いて、残った数を前記一の位の数字の五倍で割ったら、残りの数が17の倍数の場合かつその場合に限って、元の数も17の倍数である。

    例えば、34が17の場合である。なぜなら、3-20=-17が17の倍数である。201は17の倍数ではない。なぜなら、20-5=15は17の倍数ではない。一個正整数nを入力して、あなたの任務はこれが17の倍数であるかを判断することだ。

    入力フォーマット

    入力ファイルは最多で10セットのテストデータを有する。一セットのテストデータは一行を占める。その行は一個の整数n(1<=n<=10^100)のみがあり、判断待ちの正整数を表す。n=0であれば、終了を意味し、この行を処理すべきではない。

    出力フォーマット

    一セットのテストデータに対して一行を出力して、相応のnが17の倍数であるかを表す。1が肯定を表し、0が否定を表す。

    入力サンプル

    34
    201
    2098765413
    1717171717171717171717171717171717171717171717171718
    0

    出力サンプル

    1
    0
    1
    0

    分析

    この問題の難点は、nの範囲にある。64-bitの整数型を使っても、表示範囲が最大で20桁の十進数くらいになる。100桁まで昇るnとしても使えない。つまり、プログラミング言語のビルトイン型では、nを表現できない。したがって、nを表現できる型またはそれに類するものを作らないといけない。

    また、前記定理によると、nが17の倍数であるかは、nより一桁小さい別の整数で判定できる。

    つまり、$n=n_0$が17の倍数という問題が下記の問題に相当する。
    ・$n_1$が17の倍数であるか、さらに次の問題に相当する。
    ・$n_2$が17の倍数であるか、さらに次の問題に相当する。
    ・$n_3$が17の倍数であるか、さらに次の問題に相当する。
    ・……
    そして、$n_1 > 10 n_2 > 10^2 n_3 > 10^3 n_4 > ……$

    すると、ある$n_p$がプログラミング言語のビルトイン型で表現できる大きさになったら、通常の割り算で17の倍数であるかは判定できるようになる。

    前記の定理の中に、掛け算と引き算がある。掛け算に参加する数字は小さく、引き算に参加する数字が大きい。このため、大きい数字の引き算を実装する必要があることになる。

    回答案

    use std::io;
    
    fn main() {
        let mut line = String::new();
        while io::stdin().read_line(&mut line).unwrap_or(0) > 0 {
            line = String::from(line.trim());
            if line == "0" {
                break;
            }
            let bn = BigNumber { digits: line.clone() };
            println!("{}", if is_divisible_by_17(&bn) { 1 } else { 0 });
            line.clear()
        }
    }
    
    struct BigNumber {
        digits: String
    }
    
    impl BigNumber {
        fn is_negative(&self) -> bool {
            if let Some(first_char) = self.digits.chars().next() {
                match first_char {
                    '-' => true,
                    _ => false
                }
            }
            else {
                false
            }
        }
    
        fn subtract(&self, other: &BigNumber) -> BigNumber {
            let mut result_digits = String::new();
            let mut ards : Vec<i8> = Vec::new();
            let mut brds : Vec<i8> = Vec::new();
            for c in self.digits.chars().rev() {
                match c {
                    '0'..='9' => ards.push((c as u8 - '0' as u8) as i8),
                    _ => panic!("Only unsigned numbers are allowed in minuend")
                }
            }
            for c in other.digits.chars().rev() {
                match c {
                    '0'..='9' => brds.push((c as u8 - '0' as u8) as i8),
                    _ => panic!("Only unsigned numbers are allowed in subtrahend")
                }
            }
            let mut diffds : Vec<i8> = Vec::new();
            let mut carry : i8 = 0;
            let maxlen = if ards.len() > brds.len() {ards.len()} else {brds.len()};
            for di in 0..maxlen {
                let mut df = 
                    if di >= ards.len() {0} else {ards[di]}
                    - if di >= brds.len() {0} else {brds[di]}
                    - carry;
                if df < 0 {
                    carry = 1;
                    df += 10;
                }
                else {
                    carry = 0;
                }
                diffds.push(df);
            }
            if carry == 1 {
                // The result is negative, and need be complemented.
                for d in diffds.iter_mut() {
                    *d = 9 - *d + carry;
                    if *d < 10 {
                        carry = 0;
                    }
                    else {
                        *d -= 10;
                    }
                }
                result_digits.push('-');
            }
            for d in diffds.iter().rev() {
                result_digits.push(('0' as u8 + *d as u8) as char);
            }
            BigNumber {
                digits: result_digits
            }
        }
    
        fn last_digit(&self) -> Option<i8> {
            if let Some(c) = self.digits.chars().rev().next() {
                Some((c as u32 - '0' as u32) as i8)
            }
            else {
                None
            }
        }
    
        fn remove_last_digit(&mut self) {
            if self.is_negative() {
                if self.digits.len() > 2 {
                    self.digits.pop();
                }
                else {
                    self.digits.clear();
                    self.digits.push('0');
                }
            }
            else {
                if self.digits.len() > 1 {
                    self.digits.pop();
                }
                else {
                    self.digits.clear();
                    self.digits.push('0');
                }
            }
        }
    }
    
    fn is_divisible_by_17(number: &BigNumber) -> bool {
        if number.digits.len() < 4 {
            let intval = number.digits.parse::<i32>().unwrap();
            return intval % 17 == 0;
        }
        let last_digit_times_5: u32 = number.last_digit().unwrap() as u32 * 5;
        let mut shortened = BigNumber {digits: number.digits.clone()};
        shortened.remove_last_digit();
        let subtrahend = BigNumber {digits: last_digit_times_5.to_string()};
        let difference = shortened.subtract(&subtrahend);
        return is_divisible_by_17(&difference);
    }
  • Rust練習問題:弟の算数検査

    問題文

    弟は100以内の足し算と引き算をやりました。チャックしてあげてください。核問題の形式はa+b=c或いはa-b=c、どれも100を超えない非負整数です。cは弟が算出した回答で、200以内の比数整数であるか、一個の「?」かです。「?」は回答不能を意味します。

    入力形式

    入力は100行以内とし、EOF記号で終了します。一行ごとに、一問があります。形式は前述規定に則し、いかなるスペースを含みません。入力されたすべての整数に左に不要な0をつけていません。

    出力形式

    一行のみを出力します。一個の非負整数のみが出て、つまり、弟が正解した問題の数。

    入力サンプル

    1+2=3
    3-1=5
    6+7=?
    99-0=99

    回答案

    use std::io;
    use core::iter::Peekable;
    use core::str::Chars;
    pub struct Expression {
        a: i32,
        b: i32,
        op: char,
        answer: i32,
        unanswered: bool
    }
    
    fn main() {
        let mut line: String = String::new();
        let mut correct_count: i32 = 0;
        while io::stdin().read_line(&mut line).unwrap() > 0 {
            let mut char_seq: Peekable<Chars<'_>> = line.trim().chars().peekable();
            let a: i32   = read_number(&mut char_seq).unwrap().unwrap();
            let op: char = char_seq.next().unwrap();
            let b: i32   = read_number(&mut char_seq).unwrap().unwrap();
            assert!(char_seq.next().unwrap()=='=');
            let got_answer = read_number(&mut char_seq).unwrap();
            let expression = Expression {
                a: a,
                op: op,
                b: b,
                answer: got_answer.unwrap_or_default(),
                unanswered: got_answer.is_none()
            };
            match check_expression(&expression) {
                Ok(r) => correct_count += if r {1} else {0},
                Err(e) => println!("Error: {}", e)
            }
            line.clear();
        }
        println!("{}", correct_count);
    }
    
    fn read_number(text_iter: &mut Peekable<Chars<'_>>) -> Result<Option<i32>, &'static str> {
        let mut number_str: String = String::new();
        while let Some(&c) = text_iter.peek() {
            match c {
                c if c >= '0' && c <= '9' => {
                    number_str.push(c);
                    text_iter.next();
                },
                '?' => {
                    number_str.push(c);
                    text_iter.next();
                    break;
                },
                _ => break
            }
        }
        if number_str == "?" {
            Ok(None)
        } else if number_str.len() > 0 {
            Ok(Some(number_str.parse().unwrap()))
        }
        else {
            Err("Reading a number while not at a number.")
        }
    }
    
    fn check_expression(expr: &Expression) -> Result<bool, &'static str> {
        let expected: i32;
        match expr.op {
            '+' => expected = expr.a + expr.b,
            '-' => expected = expr.a - expr.b,
            '*' => expected = expr.a * expr.b,
            '/' => expected = expr.a / expr.b,
            _ => return Err("Invalid operator.")
        }
        Ok(!expr.unanswered && expr.answer == expected)
    }