Pagini recente » Cod sursa (job #2888562) | Cod sursa (job #369960) | Cod sursa (job #131334) | Cod sursa (job #2701062) | Cod sursa (job #3301911)
use std::fmt::{self, Display};
use std::fs::File;
use std::io::Write;
use std::io::{self, BufRead};
#[derive(Debug, Clone)]
struct BigInt {
digits: Vec<u8>,
}
impl BigInt {
pub fn one() -> Self {
BigInt { digits: vec![1] }
}
pub fn add(&self, other: &Self) -> Self {
let mut result = Vec::new();
let mut rest = 0;
let max_len = self.digits.len().max(other.digits.len());
for i in 0..max_len {
let a = *self.digits.get(i).unwrap_or(&0);
let b = *other.digits.get(i).unwrap_or(&0);
let sum = a + b + rest;
result.push(sum % 10);
rest = sum / 10;
}
if rest > 0 {
result.push(rest);
}
BigInt { digits: result }
}
}
impl Display for BigInt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for &digit in self.digits.iter().rev() {
write!(f, "{}", digit)?;
}
Ok(())
}
}
fn main() {
let input = File::open("nunta.in").expect("Unable to open file");
let mut out = File::create("nunta.out").expect("Unable to create file");
let mut reader = io::BufReader::new(input);
let mut sir = String::new();
reader.read_line(&mut sir).expect("Unable to read line");
let n = sir.trim().parse::<usize>().expect("Unable to parse number");
let mut a = BigInt::one();
let mut b = BigInt::one();
for _ in 2..=n {
let c = a.add(&b);
a = b;
b = c;
}
writeln!(out, "{}", b).expect("Unable to write to file");
}