Wednesday, September 29, 2010

找因數 with C++


#include <iostream>
#include <sstream>
#include "math.h"
#include <stack>
#include <windows.h>


using namespace std;
void brute(int test);
void sqrts(int test);

int main()
{
    int input;


    cout << "Input a integer" << endl;

    cin>>input;


 LARGE_INTEGER m_liPerfFreq={0};
 QueryPerformanceFrequency(&m_liPerfFreq);
 LARGE_INTEGER m_liPerfStart={0};
 QueryPerformanceCounter(&m_liPerfStart);

    brute(input);

 LARGE_INTEGER liPerfNow={0};
 QueryPerformanceCounter(&liPerfNow);
 long decodeDulation=( ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000000)/m_liPerfFreq.QuadPart);



 LARGE_INTEGER m_liPerfFreq2={0};
 QueryPerformanceFrequency(&m_liPerfFreq2);
 LARGE_INTEGER m_liPerfStart2={0};
 QueryPerformanceCounter(&m_liPerfStart2);

    sqrts(input);

 LARGE_INTEGER liPerfNow2={0};
 QueryPerformanceCounter(&liPerfNow2);
 long decodeDulation2=( ((liPerfNow2.QuadPart - m_liPerfStart2.QuadPart) * 1000000)/m_liPerfFreq2.QuadPart);



 cout.setf(ios::showpoint, ios::fixed);
 cout.precision (10);

    cout<<decodeDulation<<"  "<<decodeDulation2<<endl;

    return 0;
}


void brute(int test)
{
    string str = "";
    stringstream ss(str);
    for(int i = 1 ; i <= test ; i++)
    {
        if(test%i == 0)
        {
            ss<<i<<"|";
        }
    }
    cout<<ss.str()<<endl;
}



void sqrts(int test)
{
    string str = "";
    stringstream ss(str);
    stack<int> first;

    int temp;
    double sq;

    sq = (int) sqrt(test);

    for(int i = 1 ; i <= sq ; i++)
    {
        if(test%i == 0)
        {
            ss<<i<<"|";
            if(test/i != i)
            {
                first.push(test/i);
            }

        }
    }

    while(!first.empty())
    {
        temp = first.top();
        first.pop();
        ss<<temp<<"|";
    }

    cout<<ss.str()<<endl;
}