Browse Source

057 ok

master
Coin de Gamma 10 months ago
parent
commit
44bd4d30d8
  1. 88
      057/main.rs

88
057/main.rs

@ -1,33 +1,101 @@
struct Num { rdigits: Vec<u8> }
impl Num {
pub fn from(s: &String) -> Num {
let rd: Vec<u8> = s.chars().rev()
.map(|c| String::from(c).parse::<u8>().unwrap())
.collect();
Num { rdigits: rd }
}
pub fn fromn(n: u32) -> Num {
let mut rd = Vec::<u8>::new();
let mut nn = n;
while nn > 0 {
let d = (nn % 10) as u8;
rd.push(d);
nn /= 10;
}
Num { rdigits: rd }
}
pub fn add(&self, n: &Num) -> Num {
let mut xs = self.rdigits.clone();
let mut ys = n.rdigits.clone();
let mut rs: Vec<u8> = Vec::new();
while xs.len() < ys.len() { xs.push(0); }
while ys.len() < xs.len() { ys.push(0); }
let mut rm = 0u8;
for (a, b) in xs.iter().zip(ys.iter()) {
if a + b + rm > 9 {
rs.push((a + b + rm) - 10);
rm = 1u8;
} else {
rs.push(a + b + rm);
rm = 0u8;
}
}
if rm > 0 { rs.push(1u8); }
Num { rdigits: rs }
}
pub fn mult(&self, n: u32) -> Num {
let mut res = self.add(&Num::fromn(0));
for n in 1..n {
res = res.add(&self);
}
return res;
}
pub fn print_10(&self) {
for d in self.rdigits.iter().rev().take(10) { print!("{}", d); }
//println!("");
}
}
struct D {
a: u32,
b: u32
a: Num,
b: Num
}
impl D {
pub fn from(a: u32, b: u32) -> D {
pub fn from(a: Num, b: Num) -> D {
D { a: a, b: b }
}
pub fn print(&self) {
println!("{}/{}", self.a, self.b);
self.a.print_10();
print!("/");
self.b.print_10();
println!("");
}
pub fn add_n(&self, n: u32) -> D {
let aa = self.b;
return D::from(aa + self.a, self.b);
let nn = self.b.mult(n);
return D::from(nn.add(&self.a), self.b.add(&Num::fromn(0)));
}
pub fn iter(&self) -> D {
let mut res = self.add_n(1);
let dd = D::from(res.b, res.a);
return dd.add_n(1);
}
pub fn bigger(&self) -> bool {
return self.a.rdigits.len() > self.b.rdigits.len()
}
}
fn main() {
let mut d = D::from(1, 2);
let mut d = D::from(Num::fromn(1), Num::fromn(2));
let mut count = 0;
d = d.add_n(1);
d.print();
for n in 0..100 {
//d.print();
for n in 0..1000 {
d = d.iter();
d.print();
//d.print();
if d.bigger() {
count += 1;
}
}
println!("{}", count);
}

Loading…
Cancel
Save