Rust编写一些实用的算法

每一个方法的作用我在代码上面用英文注释出来了。

/**
    Returns the sum 1 + 2 + ... + n
    If n is less than 0, return -1
**/
pub fn gauss(n: i32) -> i32 {
    if n < 0{
        return -1;
    } else if n == 0{
        return 0;
    }else{
        return n  + gauss(n-1);
    }
}

/**
 * Adds one to the referenced integer.
 */
pub fn add_one(n: &mut i32) {
    *n += 1;
}

/**
 * Modifies the input string so that every character is replaced with 'a'.
 * See https://doc.rust-lang.org/std/string/struct.String.html.
 */
pub fn rewrite_string(s: &mut String) {
    let mut temp = String::new();
    let mut i = 0;
    while i < s.len() {
        temp.push('a');
        i += 1;
    }
    let mut r = s;
    *r = temp;
}

/**
 * Returns a new string whose contents is the concatenation of the two input slices.
 */
pub fn concatenate_strings(s1: &str, s2: &str) -> String {
    let s1 = String::from(s1);
    let s2 = String::from(s2);
    let s3 = format!("{}{}", s1, s2);
    return s3;
}

/** 
    Takes all of the elements of the given slice and creates a new vector.
    The new vector takes all the elements of the original and rotates them, 
    so the first becomes the last, the second becomes first, and so on.
    
    EX: rotate [1,2,3,4] returns [2,3,4,1]
**/
pub fn rotate(lst: &[i32]) -> Vec<i32> {
    let mut v = Vec::new();
    if lst == [] {
        return v;
    }
    for i in 0..lst.len()-1{
        v.push(lst[i+1]);
    }
    v.push(lst[0]);
    return v;
}

/**
    Converts a binary number to decimal, where each bit is stored in order in the array.
    Index 0 stores the high-order bit.

    Conversion is unsigned (all place values are positive).
    
    Ex: to_decimal of [true, false, true, false] returns 10
**/
pub fn to_decimal(ls: [bool; 32]) -> u32 {
    let mut i = ls.len()-1;
    let mut temp = 1;
    let mut decimal =0;
    let mut time = 0;
    while i > 0 {
        if ls[i] == true {
            for _ in 0..time {
                temp *= 2;
            }
            decimal += temp;
        } else {
            decimal += 0;
        }
        temp = 1;
        time += 1;
        i -= 1;
    }
    
    if i == 0{
        if ls[i] == true {
            for _ in 0..time {
                temp *= 2;
            }
            decimal += temp;
        } else {
            decimal += 0;
        }
    }
    return decimal;
}

/**
 * The Collatz conjecture (https://en.wikipedia.org/wiki/Collatz_conjecture) is that
 * when one repeatedly applies a function 'f' to a positive integer,
 * the output will eventually be 1. 'f' is defined as follows:
 * 
 * f(n) = n/2 if n is even
 * f(n) = 3n + 1 if n is odd
 * 
 * The function collatz(n) returns the number of times one must apply f to n to get 1.
 * For example, f(4) = 2, f(2) = 1. That is, it took 2 applications of f to get from 4 to 1.
 * Therefore, collatz(4) = 2.
*/
pub fn collatz(mut n: u64) -> u64 {
    let mut count = 0;
    let mut temp = n;
    while temp > 1 {
        if temp % 2 == 0 {
            temp /= 2;
        } else {
            temp *= 3;
            temp +=1;
        }
        count += 1;
    }
    return count;
}

/**
 * Returns a vector containing collatz(1), collatz(2), ..., collatz(n).
 */
pub fn collatz_times(n: u64) -> Vec<u64> {
    let mut v = Vec::new();
    for i in 1..n+1 {
    v.push(collatz(i));
    }
    return v;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值