大数相减

    xiaoxiao2024-04-21  2

    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; }
    转载请注明原文地址: https://ju.6miu.com/read-1288216.html
    最新回复(0)