Re: [问题] 1-9位数不重复印出来 (Rust)

楼主: Neisseria (Neisseria)   2016-12-06 23:41:06
Rust 的版本,板上似乎还没人贴
写得很长,不太优雅,请多包涵
use std::io;
use std::io::Write;
use std::process;
use std::collections::HashSet;
fn main() {
print!("Input a number: ");
let _ = io::stdout().flush();
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("failed to read from stdin");
let n = match input.trim().parse::<u32>() {
Ok(i) => i,
Err(_) => {
println!("Invalid integer");
process::exit(1);
}
};
let x: i32 = 10;
let mut count = 1;
for i in 1..(x.pow(n)) {
if count % 10 == 0 {
println!("");
}
let num_string = i.to_string();
let mut chars = num_string.chars();
let mut hash = HashSet::new();
let mut c = chars.next();
while c != None {
hash.insert(c);
c = chars.next();
}
let mut digit = 0;
let mut j = i;
while j > 0 {
j = j / 10;
digit += 1;
}
let passed = hash.len() as i32 >= digit;
if passed {
print!("{} ", i);
count += 1;
}
}
println!("");
}
话说 Rust 真的很难搞,常常得和 Rust 的编译器弄好久 Orz
没有必要的话,还是乖乖写 Perl/Python/Ruby 之流比较省事
※ 引述《mikemagic88 (Mikemagic88)》之铭言:
: 使用者输入1 印1-9
: 使用者输入2 印1-98 (11, 22, 33等重复的不印)
: 使用者输入3 印1-987 (121, 988, 667等有重复的不印)

Links booklink

Contact Us: admin [ a t ] ucptt.com