C ++中的主要因素(程序错误)

时间:2015-09-11 22:08:39

标签: c++

  • 当我输入2时,我只会获得91
  • 当我输入7时,我只获得18

然而:

  • 当我输入2 3 3时,我得到2121
  • 当我输入3 7(即3 * 7 * 101)时,我得到#include<iostream> using namespace std; bool is_prime( int fac ) { int i; bool found; found=true; for( i = 2; i < fac; i++ ) { if ( fac % i == 0 && found) { found=false; break; } } return found; } void prime_factors(int x) { int i; for ( i = 2; i < x; i++ ) { if ( is_prime(i) ) { while ( x % i == 0 ) { cout << i << " "; x = x / i; } } } } int main(){ int x; cin>>x; prime_factors(x); }

我似乎无法找到问题所在。有人有什么建议吗?

DWORD dwMode = CRYPT_MODE_CBC;

LPVOID aes_key = NULL;
LPVOID tmp_blk_buff = NULL;
DWORD bytes_read = NULL;

BOOL eof = FALSE;

DWORD tmp_blk_buff_size = TMP_BLOCK_BUFFER_SIZE(context->in_size);

tmp_blk_buff = VirtualAlloc(0, tmp_blk_buff_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

Utils::zero_mem(tmp_blk_buff, tmp_blk_buff_size);

LPVOID iv_ = NULL;
iv_ = VirtualAlloc(0, AES_BLOCK_SIZE_, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
Utils::zero_mem(iv_, AES_BLOCK_SIZE_);

/*BYTE iv[AES_BLOCK_SIZE_] = {
    0xAD, 0xAD, 0xAD, 0xAD,
    0xAD, 0xAD, 0xAD, 0xAD,
    0xAD, 0xAD, 0xAD, 0xAD,
    0xAD, 0xAD, 0xAD, 0xAD
};
*/
//  Utils::copy_mem(iv_, AES_BLOCK_SIZE_, iv, AES_BLOCK_SIZE_);


//CryptSetKeyParam(context->aes_hKey, KP_IV, (BYTE*)&iv_, 0);

CryptSetKeyParam(context->aes_hKey, KP_MODE, (BYTE*)&dwMode, 0);

// Encrypt data
do{
    Utils::zero_mem(tmp_blk_buff, tmp_blk_buff_size);
    bytes_read = NULL;

    ReadFile(hFile_in, tmp_blk_buff, AES_BLOCK_SIZE_, &bytes_read, NULL);

    if (bytes_read < AES_BLOCK_SIZE_)
    {
        eof = TRUE;
    }

    if (!CryptDecrypt(context->aes_hKey, NULL, eof, 0,(LPBYTE)tmp_blk_buff, &bytes_read))
    {
        context->last_error = GetLastError();

        eof = TRUE;
    }

    WriteFile(hFile_out, tmp_blk_buff, bytes_read, &bytes_read, NULL);

} while (!eof);

// ===============
//  Zero and Free Allocated memory.
Utils::zero_mem(tmp_blk_buff, tmp_blk_buff_size);
VirtualFree(tmp_blk_buff, tmp_blk_buff_size, MEM_RELEASE);

return (DWORD)1;

1 个答案:

答案 0 :(得分:1)

除了代码缩进问题(我为你修复了这些问题)之外,这里有两件大事:

  1. 您正在修改x函数中的prime_factors(),并针对x进行循环测试,以了解何时提前退出。您应该复制x,以便不要这样做。
  2. 您没有在循环之间恢复x
  3. 您还可以在is_prime()功能中减少一半的测试次数。

    更正代码清单

    #include<iostream>
    using namespace std;
    
    bool is_prime( int fac )
    {
        int i;
        bool found = true;
    
        for( i = 2; i < fac; i++)
        {
            if ( fac % i == 0 )
            {
                found=false;
                break;
            }
        }
        return found;
    }
    
    void prime_factors( int x )
    {
        int i;
        int test;
        for ( i = 2; i <= x; i++ )
        {
            test = x;
            if ( is_prime(i) )
            {
                while ( test % i == 0 )
                {
                    cout << i << " ";
                    test /= i;
                }
            }
        }
    }
    
    
    int main(){
        int x;
        cin >> x;
        prime_factors(x);
    }
    

    示例输出

    ./a.out 
    100000
    2 2 2 2 2 5 5 5 5 5 
    
    ./a.out 
    6
    2 3 
    
    ./a.out 
    1001
    7 11 13