[Course] 基于Linux的C++(自主模式)-清华大学-乔林 第四章 课后作业

[Course] 基于Linux的C++(自主模式)-清华大学-乔林 第四章 课后作业

4.1 设计算法,将某个大于1的自然数n分解为其素因子的乘积,如6=2*3,7=7,8=2*2*2。

#include <iostream>

using namespace std;

bool IsPrime(int n);
void Factorization(int n);
/*1 step: Determine whether n is a prime number*/
/*2 step: If the n is a Prime, Direct printing.*/
/*3 step: If the n is not a prime,
          Find the first prime number that can be divisible by him.
          Then it loops its quotient until the divisibility is the biggest prime.*/
int main ()
{
    int num;
    cout << "Please enter a positive integer number: ";
    cin  >> num;
    cout << "This number:  " << num << "  Factorization result Test" << endl;
    Factorization(num);
    return 0;
}

bool IsPrime(int n)
{
    if (n <= 3)
    {
        return n > 1;
    }
    else if (n % 2 == 0 || n % 3 == 0)
    {
        return false;
    }
    else
    {
        for (int i = 5; i * i < n; i += 6)
        {
            if (n % i == 0 || n % (i + 2) == 0)
            {
                return false;
            }
        }
        return true;
    }
}

void Factorization(int n)
{
    int i;
    cout << n << " = ";

    while ( n != 1 )
    {
        if ( IsPrime(n) )
        {
            cout << n << endl;
            break;
        }
        else
        {
            if (n % 2 == 0)
            {
                cout << 2 << " * ";
                n /= 2;
            }
            else if (n % 3 == 0)
            {
                cout << 3 << " * ";
                n /= 3;
            }
            else
            {
                for (i = 5; i * i < n; i += 6)
                {
                    if (n % i == 0)
                    {
                        cout << i << " * ";
                        n /= i;
                        break;
                    }
                    else if (n % (i + 2) == 0)
                    {
                        cout << i + 2 << " * ";
                        n /= (i + 2);
                        break;
                    }
                }
            }
        }
    }
}

4.2 设计算法,分别使用循环和递归两种策略求二项式系数C(n,k)。其中,n为自然数,k为不大于n的非负整数。

#include <iostream>

using namespace std;

int PermutCombin(int up_num, int down_num);
int GetFactorial(int n);

int main()
{
    int up_num, down_num, result;

    cout << "||| Calculate the Permutation and Combination coefficient |||" << endl;
    cout << "Please enter the number in the upper right corner: ";
    cin  >> up_num;
    while (up_num < 0 || up_num != int(up_num))
    {
        cout << "Warnning! The up_num must be a positive number!" << endl;
        cout << "Please re-enter a positive integer: ";
        cin  >> up_num;
        cout << endl;
    }
    cout << "Please enter the number in the lower right corner: ";
    cin  >> down_num;
    while (down_num <= 0 || down_num != int(down_num) || up_num > down_num)
    {
        cout << "Warnning! The down_num must be a positive number!" << endl;
        cout << "Please re-enter a positive integer (the down_number must be larger than up_number): ";
        cin >> down_num;
        cout << endl;
    }

    result = PermutCombin(up_num, down_num);

    cout << "The result of the calculation is: " << result << endl;

    return 0;
}

int PermutCombin(int up_num, int down_num)
{
    int i = 0, resultUp = 1, resultDown = 1, result;
    /*C_n^m = n! / (m! * (n-m)!)*/
    /*The first method*/

    // result = GetFactorial(down_num) / (GetFactorial(up_num) * GetFactorial(down_num - up_num));

    /*The second method*/
    /*Use loop*/
    if ( up_num >= down_num - up_num)
    {
        up_num = down_num - up_num;
    }

    while ( ++i <= up_num )
    {
        resultDown *= i;
        resultUp *= down_num;
        down_num -= 1;
    }

    result = resultUp / resultDown;

    return result;
}

int GetFactorial(int n)
{
    int result;
    if ( n == 0 )
        result = 1;
    else
        result = n * GetFactorial(n - 1);

    return result;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值