美文网首页
算法:作业1

算法:作业1

作者: Magicknight | 来源:发表于2018-08-04 00:03 被阅读13次

题目:设计算法,将某个大于1的自然数n分解为其素因子的乘机,如6=23,8=22*2####

分析:分解n时,根据其余数是否为0来判断是否除尽,如未除尽,除数继续递增,除尽,除数复原。
代码如下:

#include<iostream>

using namespace std;

int main()
{
    int remain,quotient;
    int n;
    cin >> n;
    quotient = n;
    remain = 2;
    while (remain != n)
    {
        quotient = n % remain;
        if (quotient == 0) {
            n = n /remain;
            cout << remain;
            cout << "*";
            remain = 2;
        } else {
            remain++;
        }
        
    }
    cout << remain <<endl;
    return 0;
}

相关文章

网友评论

      本文标题:算法:作业1

      本文链接:https://www.haomeiwen.com/subject/ndlpvftx.html