大数相减
发布时间:2020-12-14 01:37:35 所属栏目:大数据 来源:网络整理
导读:public static String sub(String str1,String str2){int m=str1.length();int n=str2.length();if(m!=n){byte[] buff=new byte[mn?m-n:n-m];for(int k=0;kbuff.length;k++){buff[k]='0';}String buf=new String(buff);str2=buf+str2;}byte[] a=str1.getByte
public static String sub(String str1,String str2){ int m=str1.length(); int n=str2.length(); if(m!=n){ byte[] buff=new byte[m>n?m-n:n-m]; for(int k=0;k<buff.length;k++){ buff[k]='0'; } String buf=new String(buff); str2=buf+str2; } byte[] a=str1.getBytes(); byte[] b=str2.getBytes(); int len=m; byte[] c=new byte[len]; int sub=0; for(len=len-1;len>=0;len--){ int temp= (a[len]-b[len]-sub+'0'); if(a[len]-sub>=b[len]) c[len]=(byte)temp; else{ sub=1; c[len]=(byte) (temp+10); } } String res=new String(c); return res; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |