还是用java吧:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int cnt = 0;
while (n > 0) {
String str1 = cin.next();
String str2 = cin.next();
BigInteger num1 = new BigInteger(str1, 10), num2 = new BigInteger(str2, 10);
System.out.println("Case " + (++cnt) + ":");
System.out.print(str1 + " + " + str2 + " = ");
if (n == 1)
System.out.println(num1.add(num2));
else
System.out.println(num1.add(num2) + "\n\n");
n--;
}
}
}
一下子就好了(-_-;),当年自学一下BigInteger类多好啊,当时这题还卡了很久啊,字符串处理还是挺考验人的,到最后可以让你抓狂,对于字符串,解法越简单越好,复杂了会自己把自己套进去,简单的方法还可以帮你快速解决问题的。
就是A不了的代码!!! :(
大叔我知道错了(╥_╥)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
void zhuan(char a[])
{
for(int i=0; i<strlen(a)/2; i++)
{
char temp=a[i];
a[i]=a[strlen(a)-i-1];
a[strlen(a)-i-1]=temp;
}
}
void swap(char **a,char **b)
{
char *c;
c=*a;
*a=*b;
*b=c;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,cnt=1;
cin>>n;
while(n--)
{
char s1[1005],s2[1005];
int scope;
memset(s1,0,sizeof(s1));memset(s2,0,sizeof(s2));
cin>>s1>>s2;
cout<<"Case "<<cnt++<<":"<<endl;
cout<<s1<<" "<<"+"<<" "<<s2<<" "<<"="<<" ";
zhuan(s1);
zhuan(s2);
if (strlen(s1)<strlen(s2))
swap(s1,s2);
scope=strlen(s2);
;
for (int i=0; i<scope; i++)
{
int pan;
s1[i]+=(s2[i]-'0');
pan=s1[i]-'0';
if(pan>=10)
{
s1[i]-=10;
if(i+1==strlen(s1))
s1[strlen(s1)]='1';
else s1[i+1]++;
}
if(s1[i+1]-'0'==10)
{
scope++;
s2[i+1]='0';
}
}
;
int flag;
zhuan(s1);
if (n==0)
{
flag=1;
for(int i=0; i<strlen(s1); i++)
if(s1[i]-'0'==0&&flag) continue;
else
{
cout<<s1[i];
flag=0;
}
if(flag) cout<<0;
}
else
{
flag=1;
for(int i=0; i<strlen(s1); i++)
if(s1[i]-'0'==0&&flag) continue;
else
{
cout<<s1[i];
flag=0;
}
if(flag) cout<<0;
cout<<endl<<endl;
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
做了两三天了,还是没过,不过也学会了许多东西:
(1).函数zhuan():一个反转字符串的函数
void zhuan(char a[])
{
for(int i=0; i<strlen(a)/2; i++)
{
char temp=a[i];
a[i]=a[strlen(a)-i-1];
a[strlen(a)-i-1]=temp;
}
}
(2).函数swap():一个字符串交换函数
void swap(char **a,char **b)
{
char *c;
c=*a;
*a=*b;
*b=c;
}
(3).生成随机数的函数srand()
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#define MAX 1000
using namespace std;
int main()
{
freopen("data.txt","w",stdout);
srand((unsigned)time(NULL));
cout<<MAX/2<<endl;
for(int i=0;i<MAX;i++)
cout<<rand()<<endl;
fclose(stdout);
return 0;
}
可以生成data.txt,用来测试上面那串代码
(4)后续,,,,,,,,
终究是过了啊 !! :)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
void zhuan(char a[])
{
for(int i=0; i<strlen(a)/2; i++)
{
char temp=a[i];
a[i]=a[strlen(a)-i-1];
a[strlen(a)-i-1]=temp;
}
}
void swap(char **a,char **b)
{
char *c;
c=*a;
*a=*b;
*b=c;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,cnt=1;
cin>>n;
while(n--)
{
char s1[1005],s2[1005];
int scope;
memset(s1,0,sizeof(s1));memset(s2,0,sizeof(s2));
cin>>s1>>s2;
cout<<"Case "<<cnt++<<":"<<endl;
cout<<s1<<" "<<"+"<<" "<<s2<<" "<<"="<<" ";
zhuan(s1);
zhuan(s2);
if (strlen(s1)<strlen(s2))
swap(s1,s2);
;
for (int i=0; i<(scope=strlen(s2)); i++)
{
int pan;
s1[i]+=(s2[i]-'0');
pan=s1[i]-'0';
if(pan>=10)
{
s1[i]-=10;
if(i+1==strlen(s1))
s1[strlen(s1)]='1';
else s1[i+1]++;
}
if(!(scope-i-1)&&s1[i+1]-'0'>=10)
{
scope++;
s2[i+1]='0';
}
}
;
int flag;
zhuan(s1);
if (n==0)
{
flag=1;
for(int i=0; i<strlen(s1); i++)
if(s1[i]-'0'==0&&flag) continue;
else
{
cout<<s1[i];
flag=0;
}
if(flag) cout<<0;
cout<<endl;
}
else
{
flag=1;
for(int i=0; i<strlen(s1); i++)
if(s1[i]-'0'==0&&flag) continue;
else
{
cout<<s1[i];
flag=0;
}
if(flag) cout<<0;
cout<<endl<<endl;
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
一开始就觉得自己的代码很完美了,其实并没有,WA就是WA,这说明代码还是有bug的。后面慢慢改的,这里和之前那串就是改动了一处,也就是
if(!(scope-i-1)&&s1[i+1]-'0'>=10)
了。
if(!(scope-i-1)&&s1[i+1]-'0'>=10)
{
scope++;
s2[i+1]='0';
}
这个if是用来应付99999 + 1和20981 + 930这两种情况的,之前代码的这一段是存在bug的,虽然它能处理99999 + 1这种情况,但是20981 + 930会等于21011,而正确答案是21911。怎么讲呢,我的处理部分为:
for (int i=0; i<(scope=strlen(s2)); i++)
{
int pan;
s1[i]+=(s2[i]-'0');
pan=s1[i]-'0';
if(pan>=10)
{
s1[i]-=10;
if(i+1==strlen(s1))
s1[strlen(s1)]='1';
else s1[i+1]++;
}
if(!(scope-i-1)&&s1[i+1]-'0'>=10)
{
scope++;
s2[i+1]='0';
}
}
两个";"前后分别是做加法处理前的准备阶段和最后的输出处理,做加法处理的代码也就在这两个分号中间了。现在主要来讲一下if(!(scope-i-1)&&s1[i+1]-'0'>=10)吧,一开始我用的是if(s1[i+1]-'0'==10),这是因为我用这个来应对9999 + 1这种情况,唉~还是目光短浅啊,改成if(!(scope-i-1)&&s1[i+1]-'0'>=10)就能应对9999 + 1和20981 + 930以及它们的类似情况了(例如13969 + 4455和99988 +12),先用!(scope-i-1)判断是不是到了s2最高位了,如果到了scope-i-1也就会等于0,!0也就是真了,随后scope++;i也++,所以最高位之后,!(scope-i-1)也就一直是真了,而这也是我们想要的,随后不断的操作,最终完成相加。
函数swap()是我从网上copy别人的。其实
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
void swap(char *a,char *b)
{
char *c;
c=a;
a=b;
b=c;
}
int main()
{
string str1,str2;
cin>>str1>>str2;
swap(str1,str2);
cout<<str1<<endl<<str2;
return 0;
}
这样可以把两个对象str1和str2互换了。
既然是对象,刚刚想了另一种互换的,哈哈,我还是太笨了 : )
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string str1,str2,temp;
cin>>str1>>str2;
temp=str1;
str1=str2;
str2=temp;
cout<<str1<<endl<<str2;
return 0;
}