我寫了一個解謎型網站的解決方案。在帶有最新g ++的XCode上,我的代碼編譯得很好。在他們的網站(和鍵盤)上,我的輸出是負面的。有人能幫我理解爲什麼,因爲我誠實地被難住了。輸出負值的C++代碼
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
vector<int> getAllPrimes(vector<int> primesArray, int n)
{
vector<int> numArray (n+1, 1);
for (int i = 2; i <= n; i++)
{
if (numArray[i] == 1)
{
primesArray.push_back(i);
for (int k = i; k <= n; k+= i)
{
numArray[k] = 0;
}
}
}
return primesArray;
}
int main()
{
long n = 32327;
if (n == 1)
{
printf("%ld\n", n);
return EXIT_SUCCESS;
}
map <int, int> primeMap;
map <int, int>::iterator itr;
vector<int> primesArray;
primesArray = getAllPrimes(primesArray, n);
while(!primesArray.empty())
{
long currPrime = primesArray.back(), curr = currPrime;
while (currPrime <= n)
{
primeMap[curr] += (int)floor(n/currPrime);
currPrime *= curr; //multiply currPrime to add another factor of curr.
}
primesArray.pop_back();
}
//get the number of divisors of n!
long numDivisors = 1;
for (itr=primeMap.begin(); itr != primeMap.end(); itr++)
{
numDivisors *= ((*itr).second*2)+1; //power of each prime + 1, * 2 because you need the number of divisors of the square of n!
numDivisors = numDivisors % 1000007;
}
printf("%ld\n", numDivisors);
return 0;
}
通常「長N」應該讀1和從標準輸入百萬之間的整數,但我只分配一個值來模擬它。
我已將代碼放在codepad中:http://codepad.org/RpPFuLzX。正如你所看到的,輸出是-596936
,而在我的機器上是656502
(這是正確的輸出)。究竟發生了什麼?
謝謝,我完全忽略了這一點。最近得到了一個mac,所以我忽略了操作系統的默認設置。 – BlackJack