Subtract large numbers

Source: Internet
Author: User

Topic Description:

input: Two positive large numbers

output: Two difference in large numbers

Code implementation:

Import java.util.ArrayList;
Import java.util.List;

Import Java.util.Scanner; /** * @author Njupt * Two number of Zhengda */public class Bignumsub {public static void main (string[] args) {Scanner cin=new
		Scanner (system.in);
			String minuend;//is a meiosis string meiosis;//the difference;//difference while (Cin.hasnext ()) {minuend=cin.next ();
			Meiosis=cin.next (); if (!valid (minuend) | |!
			Valid (meiosis))//legality verification return;
			Difference=bignumsub (minuend,meiosis);
		SYSTEM.OUT.PRINTLN (difference); }/** * @param minuend Bing * @param meiosis meiosis * @return Difference difference * * 1. Determining the size of meiosis and meiosis * (1): meiosis minu The number of digits of end is greater than the number of digits of meiosis, then the direct subtraction * (2): The number of bits in the meiotic minuend is less than that of meiosis, and the result plus-(minus) * (3): The number of digits in the meiotic minuend equals the number of meiosis meiosis, Determine the size of the two: * a:minuend>=meiosis, then directly subtract * b:minuend<meiosis, swap subtract, the result plus-(minus) */private static String Bignu Msub (String minuend, string meiosis) {int minlen=minuend.length ();//Bing length int meilen=meiosis.length ();/length of meiosis bo Olean addsign=false;//Whether to exchange the meiosis and meiosis String difference;//results if (minlen<meilen) {addsign=true;
			String Temp=minuend;
			Minuend=meiosis;
		Meiosis=temp;
			}else if (Minlen==meilen) {//two digits equal//Judge size int Re=judge (minuend,meiosis);
				if (re==-1)//less than, to exchange {addsign=true;
				String Temp=minuend;
				Minuend=meiosis;
			Meiosis=temp;
			}else if (re==0) {return "0";
		}//Subtract difference=sub (minuend,meiosis);
		
		if (addsign) return "-" +difference;
	return difference;
	/** * @param minuend * @param meiosis * @return Difference * Two number of large numbers, here according to the mathematical size has been meiosis minuend>=meiosis * * private static string sub (string minuend, string meiosis) {int minlen=minuend.length ();//Bing length int meilen=meiosis.le Ngth ()//meiosis length int dif=minlen-meilen;//Two number of Boolean borrow=false;//borrow list<integer> list=new arraylist<
		Integer> ()//place each phase minus the result int i=meilen-1;
		int j=i+dif;
		int min=0;
		int mei=0; for (; I >=0; J--, i--) {min=integer.valueof (Minuend.cha)RAt (j))-48;
			Mei=integer.valueof (Meiosis.charat (i))-48;
			
			int result=0;
			if (borrow) {//Borrow condition min=min-1;
			} if (Min>=mei) {Result=min-mei;
				}else{//need borrow Result=min+10-mei;
			Borrow=true;
		} list.add (Result);
			Log the remaining Bing bits for (; j>=0; j--) {min=integer.valueof (Minuend.charat (j))-48;
				if (borrow) {//borrow min=min-1 last time;
			Borrow=false;
		} list.add (min);
		} StringBuilder sb=new StringBuilder ();
		Note here to remove the preceding 0-bit boolean remove=true;
			for (int k=list.size () -1;k>=0;k--) {int re=list.get (k); if (re!=0| |
				Remove==false) {sb.append (re);
			Remove=false;
	} return sb.tostring (); /** * @param minuend * @param meiosis * @return result * To determine the mathematical significance of the large number of equal digits in relation to the size/private static int judge (
		String minuend, String meiosis) {int result=0;//default Bing greater than the meiotic int len=minuend.length ();
		int i=0;
				for (; i<len;i++) {if (Minuend.charat (i) >meiosis.charat (i)) {//greater than result=1;
			return result; }elsE if (Minuend.charat (i) <meiosis.charat (i)) {//less than result=-1;
			return result;
	} return result; }//input legality validation private static Boolean valid (String number) {if (null==number&& "". Equals (number)) return Fals
		E
		Char[] Chararr=number.tochararray ();
		if (chararr[0]== ' 0 ')//The first character cannot be 0 return false;
			for (int i = 1; i < chararr.length i++) {//each character is between 0 and 9 Char temp=chararr[i]; if (temp< ' 0 ' | |
		Temp> ' 9 ') return false;
	return true;
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.