用字符串模拟除法运算

import java.util.Scanner;
/*
 * 17636684150141093474 3
 * 17636684150141093474 3
 */
public class Main{
    public static void main(String []args){
        Scanner s=new Scanner(System.in);
        String a=s.nextLine();
        int b;
        char []sa=a.substring(0, a.length()-2).toCharArray();
        b=a.substring(a.length()-1).toCharArray()[0]-'0';
        int []cnt=new int[sa.length];
        int t=0;
        for(int i=0;i<sa.length;++i)
        {
            t=t*10+sa[i]-'0';
            if(t>=b)
            {
                cnt[i]=t/b;
                t=t%b;
            }
            else
                cnt[i]=0;
        }
        boolean f=true;
        if(cnt[0]!=0)f=false;
        for(int i=0;i<cnt.length;++i)
        {
            if(f && cnt[i]==0)
            {
                f=false;
                continue;
            }
            System.out.print(cnt[i]);
        }
        System.out.println(" "+t);
        s.close();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_29215513/article/details/78464938