Imitation NetEase one-dollar capture treasure algorithm, wide-length data calculation, string implementation

Source: Internet
Author: User

/**

* @company

* @version 1.0

* @author fangjianming

* @email [email protected]

* @date December 10, 2014

*/

public class Cal {


public static void Main (string[] args) {

String value = "581999974";

String value2 = "46658";

Long bet = 100;

String value3 = "10000001";

//Calculate sum

int []eleven = addstring (value,value2);

//calculation to find remainder

Long result = Divideint (Eleven,bet);

//Calculation results

int []intvalue = addstring (value3,string.valueof (result));

//int[] turn char[]

Char [] charvalue = Transintarraytochararray (intvalue);

System.out.print (charvalue);

}

//int[] turn char[]

private static char[] Transintarraytochararray (int[] intvalue) {

if (intvalue = = NULL | | intvalue.length ==0)

throw new NullPointerException ("Eleven must bot be null");

char[] charvalue = new Char[intvalue.length];

int i = 0;

for (int value:intvalue)

{

charvalue[i++] = (char) (value +48);

}

return charvalue;

}

//calculation to find remainder

private static Long Divideint (int[] Eleven, long divide) {

if (eleven = = NULL | | eleven.length ==0)

throw new NullPointerException ("Eleven must bot be null");

if (divide = = 0)

throw new NullPointerException ("Divide must bot be zero");

Long result = 0;

int len = eleven.length;

for (int i = 0;i<len; i++)

{

int tmp = eleven[i];

result = Tmp+10*result;

if (result >= divide)

{

result%= divide;

}

}

return result;

}

//Calculate sum

public static int [] AddString (String value1,string value2)

{

if (value1 = = NULL | | value2 = = NULL | | value1 = = "" "| | value2 = = "")

throw new NullPointerException ("value1 and value2 must bot be null");

String max = (value1.length () > Value2.length ())? Value1:value2;

String min = (value1.length () <= value2.length ())? Value1:value2;

int len = Max.length ();

int len2 = Min.length ();

int ten = 0;

int ge = 0;

int []eleven = new Int[len];

for (int i = max.length () -1,j = Min.length ()-1; I >= 0; I--, j--)

{

int a1 = 0;

int a2 = 0;

a1 = Max.charat (i)-48;

if (j>=0)

A2 = Min.charat (I-(Max.length ()-min.length ()))-48;

ge = (a1+a2+ten)% 10;

ten = (A1+A2)/10;

eleven[i] = GE;

}

if (Ten >0)

{

int []eleven2 = new int[len+1];

eleven2[0] = ten;

for (int j = 0;j <len; j + +)

{

eleven2[j+1] = eleven[j];

}

return eleven2;

}

return eleven;

}


}


Imitation NetEase one-dollar capture treasure algorithm, wide-length data calculation, string implementation

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.