パズルタイプのWebサイト用のソリューションを書きました。最新の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標準入力から100万の整数を読み取るようになっているが、私はそれをシミュレートするために値を割り当てます。
私はここにコードパッドを入れました:http://codepad.org/RpPFuLzX。ご覧のとおり、出力は-596936
ですが、私のマシンでは656502
(正しい出力です)です。何が起こっているのですか?
ありがとう、私はこれを完全に見落としました。最近Macを手に入れたので、OSのデフォルトを見落としました。 – BlackJack