20进制加法问题。
第一次没过,因为输出的时候忘记了考虑 0+0=0 的情况,被测试数据抓个正着
改掉这个bug就AC了
本来就是半路出家的学编程,不停的刷水题,结果每次都是代码写的也比较sloppy...
比单纯高精度稍微麻烦一点,难度系数=1
通过率数据 28.06% (5015/17869)
import java.util.*;
public class Main
{
public static int[] n1=new int[150];
public static int[] n2=new int[150];
public static int[] ans=new int[150];
public static char[] format={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j'};
public static void main(String[] args)
{
int i;
char tmp;
int x;
int ml;
String s1;
String s2;
Scanner in = new Scanner(System.in);
while (in.hasNextLine())
{
for (i=0;i<150;i++)
{
n1[i]=0;
n2[i]=0;
ans[i]=0;
}
s1=in.nextLine();
if (s1.length()==0)
break;
s2=in.nextLine();
if(s2.length()==0)
break;
s1=s1.trim();
s2=s2.trim();
for (i=s1.length()-1;i>=0;i--)
{
tmp=s1.charAt(i);
if((tmp<='9') && (tmp>='0'))
x=(int)(tmp-48);
else
{
if((tmp<='j') && (tmp>='a'))
x=(int)(tmp-97+10);
else
x=0;
}
n1[s1.length()-i-1]=x;
}
for (i=s2.length()-1;i>=0;i--)
{
tmp=s2.charAt(i);
if((tmp<='9') && (tmp>='0'))
x=(int)(tmp-48);
else
{
if((tmp<='j') && (tmp>='a'))
x=(int)(tmp-97+10);
else
x=0;
}
n2[s2.length()-i-1]=x;
}
if(s1.length()>s2.length())
ml=s1.length();
else
ml=s2.length();
for (i=0;i<ml+1;i++)
{
ans[i]+=n1[i]+n2[i];
if (ans[i]>19)
{
ans[i]-=20;
ans[i+1]++;
}
}
i=140;
while ((ans[i]==0) && (i>0)) i--;
while (i>=0)
{
System.out.printf("%s", format[ans[i]]);
i--;
}
System.out.println("");
}
}
}