VS2010 c++ を使用して 3n+1 問題を解決しようとしていますが、小さな入力ではうまく機能しますが、113383 に達するとオーバーフローします。
ここに問題のリンクがあります。
これは、この問題を解決するために使用しているコードです:
#include <iostream>
using namespace std;
int main(void) {
while (!cin.eof()) {
int i, j, maxCycle = 0, tmaxCycle = 0;
cin >> i >> j;
for (int x = i; x <= j; x++) {
int n = x;
tmaxCycle = 0;
while (n != 1) {
if ((float)(n/2) != (n/2.0)) {
n = 3*n + 1;
}
else {
n /= 2;
}
tmaxCycle += 1;
if (n < 0) {
int blah = 0; //just for the breakpoint
}
}
tmaxCycle += 1;
if (tmaxCycle > maxCycle) {
maxCycle = tmaxCycle;
}
}
cout << i << "\t" << j << "\t" << maxCycle << endl;
}
system("pause");
}
15 行目にブレークポイントを作成しましたが、この時点で値がオーバーフローします
n=-1812855948