小程序:因式分解

最近在用CDBN做实验的时候,发现batchsize的大小会影响实验结果,分批的时候就需要考虑到是否能够整除。故写了个因式分解的小程序。

/** * @filename factorization.cc * @Synopsis * @author XIU * @version 1 (centos、G++) * @date 2016-04-20 */
#include<iostream>
#include<cmath>
using namespace std;

/* ============================================================================*/
/** * @Synopsis * * @Param data the data should be factored * @Param temp_data the factor(yin zi) * * @Returns the number of factor(yin zi) */
/* ============================================================================*/
int factor( long int data, int * temp_data )
{
    int count = 0;

    while( data != 1 )
    {
        int i = 2;
        while( data%i != 0 && i <= data )
        {
           ++i ;
        }
        if( data%i == 0 )
        {
            temp_data[count] = i;

            ++count;

            data = data / i;
        }
        else
        {
            return count;
        }
    }

    return count;
}

int main()
{
    long int data;

    while( cin>>data )
     {
        int temp_data[200] = { 0 };

        int count = factor( data,  temp_data );

        if( count == 0 )
        {
            cout << " can not be factored" << endl;
            continue;
         }
        cout << data << " = " ;

        while( count )
          {
            cout << temp_data[count-1] << " " ;

            if( count -1 != 0 )
                cout << " * " ;

            --count;
        }
        cout << endl;
    }
    return 0;
}

你可能感兴趣的:(C++,代码,因式分解)