我正在尝试在项目euler上完成第8项,并且出于某种原因,如果我在countAndMult函数中使用(int,long或long long),我得到2091059712作为最大数字,这是不对的。
我已阅读其他帖子,我尝试更改为以下内容:
type ----> output
int ----> 2091059712
long ----> 2091059712
long long ----> 2091059712
unsigned long ----> 3780710640
这是我的代码:http://pastebin.com/KBsXxPjB
#include <iostream>
#include <vector>
using namespace std;
void sliceString(string str, char arrChar[], int arrInt[], const int x){
for(int i = 0; i<x; i++){
arrChar[i] = {' '};
arrInt[i] = {0};
}
for(int i=0;i<x;i++){
arrChar[i] = {str.at(i)};
}
}
void charToInt(char arrChar[], int arrInt[], const int x){
for(int i=0; i<x; i++){
int doMath = arrChar[i]-48;
switch (doMath){
case 0:
arrInt[i] = {doMath};
break;
case 1:
arrInt[i] = {doMath};
break;
case 2:
arrInt[i] = {doMath};
break;
case 3:
arrInt[i] = {doMath};
break;
case 4:
arrInt[i] = {doMath};
break;
case 5:
arrInt[i] = {doMath};
break;
case 6:
arrInt[i] = {doMath};
break;
case 7:
arrInt[i] = {doMath};
break;
case 8:
arrInt[i] = {doMath};
break;
case 9:
arrInt[i] = {doMath};
break;
}
}
}
unsigned long countAndMult(int arrInt[], const int x){
unsigned long biggestSum = 0;
unsigned long tempSum = 0;
for(int i=0; i<x; i++){
tempSum = arrInt[i]*arrInt[i+1]*arrInt[i+2]*arrInt[i+3]*arrInt[i+4]*arrInt[i+5]*arrInt[i+6]*arrInt[i+7]*arrInt[i+8]*arrInt[i+9]*arrInt[i+10]*arrInt[i+11]*arrInt[i+12];
if(tempSum > biggestSum){
biggestSum = tempSum;
cout << "arrInt[" << i << "] :: " << tempSum << endl;
}
}
return biggestSum;
}
int main(){
//0123456789012
string number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
string test = "7316717653133";
const int SIZE = 1000;
char digits[SIZE];
int actualDigits[SIZE];
sliceString(number, digits, actualDigits, SIZE);
charToInt(digits, actualDigits, SIZE);
cout << countAndMult(actualDigits, SIZE) << endl;
return 0;
}
答案 0 :(得分:0)
当您使用long long tempSum;
时,您执行得太晚了。数学已经用较小的类型(int
完成,因为arrInt
是什么),而且高位已经很久了。
您需要在操作之前执行转换,否则会溢出。类似的东西:
tempSum = arrInt[i]*arrInt[i+1]*arrInt[i+2]*arrInt[i+3]*arrInt[i+4]*arrInt[i+5]*
(long long)(arrInt[i+6]*arrInt[i+7]*arrInt[i+8]*arrInt[i+9]*arrInt[i+10]*arrInt[i+11]*arrInt[i+12]);