2014-02-05 16:33:10 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-05 23:11:25 -05:00
|
|
|
// xfail-test reading from os::args()[1] - bogus!
|
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::from_str::FromStr;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::os;
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::vec::MutableVector;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::vec;
|
2013-04-17 16:19:25 -05:00
|
|
|
|
|
|
|
fn max(a: i32, b: i32) -> i32 {
|
|
|
|
if a > b {
|
|
|
|
a
|
|
|
|
} else {
|
|
|
|
b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)]
|
|
|
|
fn fannkuch_redux(n: i32) -> i32 {
|
|
|
|
let mut perm = vec::from_elem(n as uint, 0i32);
|
|
|
|
let mut perm1 = vec::from_fn(n as uint, |i| i as i32);
|
|
|
|
let mut count = vec::from_elem(n as uint, 0i32);
|
2013-06-04 23:43:41 -05:00
|
|
|
let mut max_flips_count = 0i32;
|
|
|
|
let mut perm_count = 0i32;
|
|
|
|
let mut checksum = 0i32;
|
2013-04-17 16:19:25 -05:00
|
|
|
|
|
|
|
let mut r = n;
|
|
|
|
loop {
|
|
|
|
unsafe {
|
|
|
|
while r != 1 {
|
|
|
|
count.unsafe_set((r-1) as uint, r);
|
|
|
|
r -= 1;
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for (perm_i, perm1_i) in perm.mut_iter().zip(perm1.iter()) {
|
2013-06-07 10:20:47 -05:00
|
|
|
*perm_i = *perm1_i;
|
2013-04-17 16:19:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut flips_count: i32 = 0;
|
|
|
|
let mut k: i32;
|
|
|
|
loop {
|
2013-12-15 05:44:51 -06:00
|
|
|
k = *perm.unsafe_ref(0);
|
2013-04-17 16:19:25 -05:00
|
|
|
if k == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let k2 = (k+1) >> 1;
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0i32, k2) {
|
2013-04-17 16:19:25 -05:00
|
|
|
let (perm_i, perm_k_i) = {
|
2013-12-15 05:44:51 -06:00
|
|
|
(*perm.unsafe_ref(i as uint),
|
|
|
|
*perm.unsafe_ref((k-i) as uint))
|
2013-04-17 16:19:25 -05:00
|
|
|
};
|
|
|
|
perm.unsafe_set(i as uint, perm_k_i);
|
|
|
|
perm.unsafe_set((k-i) as uint, perm_i);
|
|
|
|
}
|
|
|
|
flips_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_flips_count = max(max_flips_count, flips_count);
|
|
|
|
checksum += if perm_count % 2 == 0 {
|
|
|
|
flips_count
|
|
|
|
} else {
|
|
|
|
-flips_count
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use incremental change to generate another permutation.
|
|
|
|
loop {
|
|
|
|
if r == n {
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("{}", checksum);
|
2013-04-17 16:19:25 -05:00
|
|
|
return max_flips_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
let perm0 = perm1[0];
|
|
|
|
let mut i: i32 = 0;
|
|
|
|
while i < r {
|
|
|
|
let j = i + 1;
|
2013-12-15 05:44:51 -06:00
|
|
|
let perm1_j = { *perm1.unsafe_ref(j as uint) };
|
2013-04-17 16:19:25 -05:00
|
|
|
perm1.unsafe_set(i as uint, perm1_j);
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
perm1.unsafe_set(r as uint, perm0);
|
|
|
|
|
2013-12-15 05:44:51 -06:00
|
|
|
let count_r = { *count.unsafe_ref(r as uint) };
|
2013-04-17 16:19:25 -05:00
|
|
|
count.unsafe_set(r as uint, count_r - 1);
|
2013-12-15 05:44:51 -06:00
|
|
|
if *count.unsafe_ref(r as uint) > 0 {
|
2013-04-17 16:19:25 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
r += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
perm_count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2013-08-03 18:59:24 -05:00
|
|
|
let n: i32 = FromStr::from_str(os::args()[1]).unwrap();
|
2013-09-25 00:16:43 -05:00
|
|
|
println!("Pfannkuchen({}) = {}", n as int, fannkuch_redux(n) as int);
|
2013-04-17 16:19:25 -05:00
|
|
|
}
|