C++ 输出错误:两个不同编译器上的不同输出:Prime Cryptarithm USACO
以下密码算法是一个乘法问题,可以通过将指定的N个数字集合中的数字替换到标有*的位置来解决。如果选择了素数数字集{2,3,5,7},则该密码称为素数密码。例如:C++ 输出错误:两个不同编译器上的不同输出:Prime Cryptarithm USACO,c++,C++,以下密码算法是一个乘法问题,可以通过将指定的N个数字集合中的数字替换到标有*的位置来解决。如果选择了素数数字集{2,3,5,7},则该密码称为素数密码。例如: * * * x * * ------- * * * <-- partial product 1 * * * <-- partial product 2 ------- * * * * *** x** ------- ***
* * *
x * *
-------
* * * <-- partial product 1
* * * <-- partial product 2
-------
* * * *
***
x**
-------
****通常,当一个程序的结果在不同的编译器中不同时,这通常意味着在程序中的某个地方,您正在运行未定义的行为
在您的例子中,j的比较正是问题所在。我不知道我怎么会错过它。谢谢
4 /* <-- This is the given size of set N from which the multiplicand and multiplier is taken */
2 3 5 7 /* <-- Set N */
#include <iostream>
#include <fstream>
using namespace std;
int *sortInput(int arrSetN[], int numofDigs)
{
for(int ictr=0;ictr<numofDigs;ictr++)
{
for(int jctr=ictr+1;jctr<numofDigs;jctr++)
{
if(arrSetN[ictr]>arrSetN[jctr])
{
swap(arrSetN[ictr],arrSetN[jctr]);
}
}
}
return arrSetN;
}
bool checkNum(int num,int arrSetN[],int numofDigs)
{
int countDigits=0,tempNum=num,ictr=0,jctr=0;
int *arrDigits,*digitTally;
/*
Counting the number of digits in num.
*/
for(;tempNum>0;)
{
tempNum=tempNum/10;
countDigits++;
}
arrDigits=new int[countDigits];
digitTally=new int[countDigits];
/*
making digitTally = 0
*/
for(ictr=0;ictr<countDigits;ictr++)
digitTally[ictr]=0;
/*
retrieving the digits of num.
*/
for(ictr=0;ictr<countDigits;ictr++)
{
arrDigits[ictr]=num%10;
num=num/10;
}
/*
checking if all digits of num are part of set N.
*/
for(ictr=0;ictr<countDigits;ictr++)
{
for(jctr=0;jctr<=numofDigs;jctr++)
{
if(arrDigits[ictr]==arrSetN[jctr])
digitTally[ictr]=1;
}
}
for(int cfc=0;cfc<countDigits;cfc++)
{
if(digitTally[cfc]==0)
return false;
}
return true;
}
int main()
{
ofstream fout("crypt1.out");
ifstream fin("crypt1.in");
int numofDigs,ictr=0,a,b,c,d, e,p1,p2,sum,p2test;
int actr=0,bctr=0,cctr=0,dctr=0,ectr=0,tally=0;
bool sumCheck,p1Check,p2Check;
fin>>numofDigs;
int *arrSetN;
arrSetN = new int[numofDigs];
for(ictr=0;ictr<numofDigs;ictr++)
{
fin>>arrSetN[ictr];
}
arrSetN=sortInput(arrSetN,numofDigs);
for(actr=0;actr<numofDigs;actr++)
{
a=arrSetN[actr];
for(bctr=0;bctr<numofDigs;bctr++)
{
b=arrSetN[bctr];
for(cctr=0;cctr<numofDigs;cctr++)
{
c=arrSetN[cctr];
for(dctr=0;dctr<numofDigs;dctr++)
{
d=arrSetN[dctr];
for(ectr=0;ectr<numofDigs;ectr++)
{
e=arrSetN[ectr];
p1=((a*100)+(b*10)+c)*e;
p2=((a*1000)+(b*100)+(c*10))*d;
p2test=((a*100)+(b*10)+c)*d;
p1Check=checkNum(p1,arrSetN,numofDigs);
p2Check=checkNum(p2test,arrSetN,numofDigs);
if(p1>999 || p2test>999 || p1<100 || p2test<100)
{
continue;
}
sum=p1+p2;
sumCheck=checkNum(sum,arrSetN,numofDigs);
if(sumCheck==true && p1Check==true && p2Check==true)
{
tally++;
//fout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e;
//fout<<"\t"<<p1<<" "<<p2<<" "<<sum<<"\n";
}
}
}
}
}
}
fout<<tally<<"\n";
}