You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.3 KiB

fn cancel(n: u32, k: u8) -> u32 {
let mut res = 0u32;
let mut ds = Vec::new();
let mut nn = n;
while nn > 0 {
let dd = nn % 10;
if dd != k as u32 {
ds.push(nn % 10);
}
nn /= 10;
}
for x in ds.iter().rev() {
res *= 10;
res += x;
}
return res;
}
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let remainder = a % b;
a = b;
b = remainder;
}
a
}
struct Fr {
n: u32,
d: u32
}
impl Fr {
fn from(n: u32, d: u32) -> Fr {
return Fr { n: n, d: d };
}
fn simple(&self) -> Fr {
let g = gcd(self.n, self.d);
let n = self.n / g;
let d = self.d / g;
return Fr { n: n, d: d };
}
fn print(&self) {
println!("{}/{}", self.n, self.d);
}
fn eq(&self, other: &Fr) -> bool {
let a = self.simple();
let b = other.simple();
return a.n == b.n && a.d == b.d;
}
fn ds(&self) -> Vec<u8> {
let mut res = Vec::new();
let mut n = self.n;
let mut d = self.d;
while n > 0 {
res.push((n % 10) as u8);
n /= 10;
}
while d > 0 {
res.push((d % 10) as u8);
d /= 10;
}
return res;
}
fn cancel(&self, k: u8) -> Fr {
let n = cancel(self.n, k);
let d = cancel(self.d, k);
return Fr{ n: n, d: d };
}
fn can_cancel(&self, k: u8) -> bool {
return cancel(self.n, k) != 0 && cancel(self.d, k) != 0;
}
fn test(&self) -> bool {
let ds = self.ds();
for k in ds {
if !self.can_cancel(k) { continue; }
let cn = self.cancel(k);
if self.eq(&cn) {
return true;
}
}
return false;
}
}
fn main() {
let mut res_n = 1;
let mut res_d = 1;
for n in 10..99 {
for d in n+1..99 {
//println!("{}/{}", n, d);
if n % 10 == 0 && d % 10 == 0 { continue; }
let f = Fr::from(n, d);
if f.test() {
f.print();
res_n *= n;
res_d *= d;
}
}
}
let res_fr = Fr::from(res_n, res_d).simple();
println!("{}", res_fr.d);
}