我的解題情形:
WA (line:8)
答案不正確
您的答案為: 20111130424327180111043 正確答案為: 20111130424327180111041
後來我稍微用一點小技巧獲得了第八題測資 (221222434667598981221454 / 11)
然後我再回去用Dev C++5.1.1編譯後執行,結果竟然就是正確答案(20111130424327180111041)
不知道是哪邊出了問題,有高人能指點嗎?
我的原始碼:
#include <iostream>
#include <string>
using namespace std;
bool check(int[],int[],int ,int,int);
int main(int argc, char *argv[])
{
string sNum1="",sNum2="";
char op;
int num1[500]={0},num2[500]={0},num3[500];
for(int i=0;i<500;i++)
{
num1[i]=0;
num2[i]=0;
num3[i]=0;
}
bool start = false;
bool neg = false;
int count =0;
while(cin>>sNum1>>op>>sNum2)
{
int num1Length = sNum1.length();
int num2Length = sNum2.length();
for(int i=0;i<500;i++)
{
num1[i]=0;
num2[i]=0;
}
start = false;
neg = false;
count ++;
switch(op)
{
case '+':
start = false;
for(int i=0;i<num1Length;i++)
{
num1[i]+= (int) sNum1[num1Length-i-1]-48;
}
for(int i=0;i<num2Length;i++)
{
num1[i]+= (int) sNum2[num2Length-i-1]-48;
}
for(int i=0;i<500-1;i++)
{
if(num1[i]>9)
{
num1[i+1]+=num1[i]/10;
num1[i]%=10;
}
}
for(int i=499;i>=0;i--)
{
if(i==0 && num1[i] == 0)
{
cout << "0";
}
else if( !start && num1[i]!=0 )
{
start = true;
cout << num1[i];
}
else if(start)
{
cout << num1[i];
}
}cout<< endl;
break;
case '-':
if(num1Length < num2Length)
{
neg = true;
string tmp = sNum1;
sNum1 = sNum2;
sNum2 = tmp;
int ntmp = num1Length;
num1Length = num2Length;
num2Length = ntmp;
}
else if(num1Length > num2Length)
{
}
else if(num1Length == num2Length)
{
if(sNum1>sNum2)
{
}
else
{
neg = true;
string tmp = sNum1;
sNum1 = sNum2;
sNum2 = tmp;
int ntmp = num1Length;
num1Length = num2Length;
num2Length = ntmp;
}
}
for(int i=0;i<num1Length;i++)
{
num1[i] += (int) sNum1[num1Length-i-1]-48;
}
for(int i=0;i<num2Length;i++)
{
num1[i] -= sNum2[num2Length-i-1]-48;
}
start = false;
for(int i=0;i<500-1;i++)
{
if(num1[i]<0)
{
num1[i+1] -= 1;
num1[i] += 10;
}
}
if(neg)
{
cout << "-";
}
for(int i=499;i>=0;i--)
{
if(i==0 && num1[i] == 0 )
{
cout << "0";
}
else if( !start && num1[i]!=0 )
{
start = true;
cout << num1[i];
}
else if(start)
{
cout << num1[i];
}
}cout << endl;
break;
case '*' :
for(int a=0,i=num1Length-1;a<500&&i>=0;i--,a++)
{
for(int b=0,j=num2Length-1;b<500&&j>=0;j--,b++)
{
num1[a+b] += (sNum1[i]-48) * (sNum2[j]-48);
}
}
for(int i=0;i<500-1;i++)
{
if(num1[i]>9)
{
num1[i+1]+=num1[i]/10;
num1[i]%=10;
}
}
for(int i=499;i>=0;i--)
{
if(i==0 && num1[i] == 0)
{
cout << "0";
}
else if( !start && num1[i]!=0 )
{
start = true;
cout << num1[i];
}
else if(start)
{
cout << num1[i];
}
}cout << endl;
break;
case '/':
start = false;
{
for(int i=0;i<num1Length;i++) num1[i] += sNum1[num1Length-i-1]-48;
for(int i=0;i<num2Length;i++) num2[i] += sNum2[num2Length-i-1]-48;
int i=0,b;
if(num2Length>num1Length){cout << "0" << endl;break;}
for(i=num1Length-num2Length,b=num1Length-1;i>=0 ; i--)
{
while(check(num1,num2,i,b,num2Length))
{
if( ((b==0)||(b==i)) &&num2Length>=2)
{
break;
}
for(int j=i;j<=b;j++)
{
num1[j] -= num2[j-i];
}
for(int j=i;j<=b;j++)
{
if(num1[j]<0)
{
num1[j+1]--;
num1[j]+=10;
}
}
num3[i]++;
if(num1[b]==0)
{
b--;
if(b<i)
{
break;
}
}
else if(num1[b]<0)
{
cout << "num1[b]<0"<<endl;
}
}
}
for(int i=499;i>=0;i--)
{
if(i==0 && num3[i] == 0 )
{
cout << "0";
}
else if( !start && num3[i]!=0 )
{
start = true;
cout << num3[i];
}
else if(start)
{
cout << num3[i];
}
}cout << endl;
break;
}
}
}
return 0;
}
bool check(int num1[],int num2[],int seat,int head,int num2Len)
{
for(int i=head ; i>=seat ;i--)
{
if(num1[i]>num2[i-seat])
{
return true;
}
if(num1[i]<num2[i-seat])
{
return false;
}
}
return true;
}