今天就是应对大数类了的time了
代码如下:
package shikutai; import java.io.*; class BigInt { int a[]; int len; BigInt(String str) { { len=str.length(); a=new int[len]; for(int i=0;i<len;i++) { this.a[i]=str.charAt(i)-48; } } } public BigInt bigplus(BigInt b) { BigInt sum; int l; int m=len-1; int n=b.len-1; int max; if(len>=b.len) { l=b.len; max=len-1; sum=this; } else { l=len; sum=b; max=b.len-1; } for(int i=0;i<l;i++) { sum.a[max]=a[m]+b.a[n]; if(max!=0&&sum.a[max]>=10) { sum.a[max]=sum.a[max]-10; sum.a[max-1]++; } m--; n--; max--; } return sum; } public void output() { for(int i=0;i<len;i++) { System.out.print(a[i]); } } } public class Class6 { public static void main(String[] args) throws IOException { String str1; String str2; System.out.println("请输入大数1:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); str1=br.readLine(); System.out.println("请输入大数2:"); BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); str2=br1.readLine(); BigInt a1=new BigInt(str1); BigInt a2=new BigInt(str2); BigInt a3=a1.bigplus(a2); a3.output(); } }
运行结果: