杭电1002解答疑问求解(附对和错的C++代码)

这里是ACM机器编译不通过的代码,求指教为何错误了??VC++6.0运行成功
<pre class="cpp" name="code">#include <iostream>
#include<string>
using namespace std;
int main()
{
	
	int t,s[1002]={0};
	string a,b;
	int L1,L2,LS,LS1=0,n,m,k;
	/*
	*t储存实例的样例数1-20
	*s[1002]储存数组
	*a,b分别储存输入的数字
	*L1储存A数字的长度最后一个元素的下标
	*L2储存B数字的长度最后一个元素的下标
	*LS储存结果数字的最后一个元素的下标
        *LS1储存结果长度,如果进位的话,除了第一位没包括
	*n,m是分别对应位数的数字用于相加
	*k储存数字结果
	*/
	
	cin>>t;
	bool flag=false;//判断是否进位
	for(int i=1;i<=t;i++)
	{
        cin>>a>>b;

		L1 = a.length()-1;
		L2 = b.length()-1;
		if(L1 == L2)
		{
		   if((a[0]+b[0]-'0'-'0') >= 10)
		   {
			LS = (L1>L2)?L1:L2;
			LS++;
		   }
		   else
		   {
			LS = (L1>L2)?L1:L2;
		   }
		}
		else if(L1 > L2)
		{
			LS = L1;
		}
		else
		{
             LS = L2;
		}


		while(L1 >= 0 && L2 >=0)
		{
			n=a[L1]-'0';
			m=b[L2]-'0';
			if((n+m) >= 10)
                flag=true;
			else
				flag=false;

			  if(flag)
			  {
				k=(n+m)-10;
				s[LS]+=k;			
                s[--LS]=1;
				LS1++;
				if((L1==L2) && L1==0)LS1++;
				L1--;
			    L2--;
			  }
			  else
			  {
				k=(n+m);
				s[LS]+=k;
                 if(s[LS] == 10)
				 {
				   s[LS]=0;
				   s[--LS]=1;
				 }
			     else
				 {
				   s[--LS]=0;
				 }
				L1--;
			    L2--;
				LS1++;
			  }
		}
			int temp=0;
			if(L1 < 0)
			{
                while(L2 >= 0)
				{
					s[LS]+=b[L2]-'0';
                    if(s[LS] >=10)
					{
						if(LS == 0)
						{
							temp=1;
							s[LS]=0;
						}
						else
						{
							temp=0;
							s[LS] = 0;
							s[--LS] = 1;
							LS++;
						}
					}
					LS--;
					L2--;
					LS1++;
				}
			}
				if(L2 < 0)
			{
                while(L1 >= 0)
				{
					s[LS]+=a[L1]-'0';
					if(s[LS] >=10)
					{
						if(LS == 0)
						{
							temp=1;
							s[LS]=0;
						}
						else
						{
							temp=0;
							s[LS] = 0;
							s[--LS] = 1;
							LS++;
						}
					}
					LS1++;
					LS--;
					L1--;

				}
			}
		
		
		cout<<"Case "<<i<<":"<<endl;
		cout<<a<<" + "<<b<<" = ";
		if(temp==1)
		{
			cout<<temp;
		}
		for(int e=0;e<LS1;e++)
		{
			cout<<s[e];
			s[e]=0;
		}
		cout<<"\n";
		if(i == t){}
		else cout<<"\n";
		LS1=0;
	}
	
	return 0;
}

 



这里是编译通过的代码

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string a,b;
	int num,sum[1000];
    cin>>num;
	for(int l=0;l<num;l++)
	{
		cin>>a>>b;
		int m=0,n=0,temp=0,i,k;
		int	q=0;//用于记录sum数组的长度!!
	    i=a.length();
	    k=b.length();
		i=i-1;
		k=k-1;
		while(i>=0 && k>=0)
		{
			m=a[i]-'0';
			n=b[k]-'0';
			sum[q++]=(temp+m+n)%10;
			temp=(temp+m+n)/10;
			i--;
			k--;
		}
	  if(i>k)
	  {
		  while(i>=0)
		  {
		     
			 m=a[i]-'0';
			 sum[q++]=(temp+m)%10;
		     temp=(temp+m)/10;

			 i--;
		  }
	  }
	  if(k>i)
	  {
		 while(k>=0)
		  {
		     m=b[k]-'0';
			 sum[q++]=(temp+m)%10;
		     temp=(temp+m)/10;
			 k--;
		  }
	  }
	  sum[q]=temp;
	  cout<<"Case "<<l+1<<":"<<endl;
	 cout<<a<<" + "<<b<<" = ";
	  if(sum[q]!=0) cout<<sum[q];
		  for(--q;q>=0;q--)
          cout<<sum[q];
	 cout<<endl;
	 if(l<num-1) cout<<endl;
	}
 return 0;
}
</pre><pre class="cpp" name="code" snippet_file_name="blog_20141120_5_1564528" code_snippet_id="525913">
求告诉第一个代码为何不能AC
而第二个代码可以AC
两张都运行正确,而且我运行时结果也都没见错误。



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YuYunTan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值