Today, we continue to look at the fun of algorithms and learn the conversion of Arabic numerals and Chinese numerals.
Chinese characters with 0123456789 as the basic count, and Arabic numerals by the position of the number of positions of the throne is not the same, Chinese numbers are only "digital +" the way to form a number, such as hundred, Thousand, million.
Chinese numerals each digit will keep up with a throne, and the throne is the number of digits, equivalent to the digits of the Arabic numerals.
The Chinese count takes million as the subsection, under million does not have the section right, above million is the festival right.
Another feature of Chinese is the changeable "0", which is roughly summed up as three rules:
1. In the 10000 subsection, the end of the section is 0, and no "0" is used.
2. Use "0" between two non-0 digits in the subsection.
3. When the "thousand" bit of the section is 0 o'clock, if there is no other number in the previous section of this section, then "0" is not necessary, otherwise the "0" will be used.
Algorithm design:
First, define three string arrays, holding digits, nodes, Positions of Thrones
Then the implementation of two methods, a realization of the right to the operation of the processing, there is an implementation of the section of the operation.
The second program is to achieve the conversion of Chinese numerals to Arabic numerals, this I do not understand a small part of the book Code, but I read the author's thoughts, so I wrote a book on a different algorithm out, but the same idea.
Are all through the section of the right to punish, and then calculate one by one, and then merge to get the results.
Here's My Code:
</pre><pre name= "code" class= "java" >package Arabic numerals and Chinese numerals; public class Main {public static void main (string[] args) {main ma = new Main (); Tool to = new tool (); Ma.initmain ();} public void Initmain () {Testnumbertochinese (); System.out.println ("————————————————————————————————————————"); Testchinesetonumber ();} public void Testnumbertochinese () {Numberchangetochinese Numtochinese = new Numberchangetochinese (); System.out.println ("0:" +numtochinese.numbertochinese (0)); System.out.println ("1:" +numtochinese.numbertochinese (1)); System.out.println ("2:" +numtochinese.numbertochinese (2)); System.out.println ("3:" +numtochinese.numbertochinese (3)); System.out.println ("4:" +numtochinese.numbertochinese (4)); System.out.println ("5:" +numtochinese.numbertochinese (5)); System.out.println ("6:" +numtochinese.numbertochinese (6)); System.out.println ("7:" +numtochinese.numbertochinese (7)); System.out.println ("8:" +numtochinese.numbertochinese (8)); System.out.println ("9:" +numtochinese.numbertochinese (9)); System.out.printlN ("Ten:" +numtochinese.numbertochinese (10)); System.out.println ("One:" +numtochinese.numbertochinese (11)); System.out.println ("+numtochinese.numbertochinese:" (110)); System.out.println ("111:" +numtochinese.numbertochinese (111)); System.out.println (":" +numtochinese.numbertochinese (100)); System.out.println ("102:" +numtochinese.numbertochinese (102)); System.out.println ("1020:" +numtochinese.numbertochinese (1020)); System.out.println ("1001:" +numtochinese.numbertochinese (1001)); System.out.println ("1015:" +numtochinese.numbertochinese (1015)); System.out.println ("+numtochinese.numbertochinese:" (1000)); System.out.println ("10000:" +numtochinese.numbertochinese (10000)); System.out.println ("20010" +numtochinese.numbertochinese (20010)); System.out.println ("20001" +numtochinese.numbertochinese (20001)); System.out.println ("100000:" +numtochinese.numbertochinese (100000)); System.out.println ("1000000:" +numtochinese.numbertochinese (1000000)); System.out.println ("10000000" +numtochinese.numbertochinese (10000000)); SystEm.out.println ("100000000:" +numtochinese.numbertochinese (100000000)); System.out.println ("1000000000" +numtochinese.numbertochinese (1000000000)); System.out.println ("2000105" +numtochinese.numbertochinese (2000105)); System.out.println ("20001007:" +numtochinese.numbertochinese (20001007)); System.out.println ("2005010010:" +numtochinese.numbertochinese (2005010010));} public void Testchinesetonumber () {Chinesechangetonumber chinesetonumber = new Chinesechangetonumber (); System.out.println ("2,055,014,010:" +chinesetonumber.chinesetonumber ("2,055,014,010")); System.out.println ("20,001,007:" +chinesetonumber.chinesetonumber ("20,001,007")); System.out.println ("20,001:" +chinesetonumber.chinesetonumber ("20,001")); System.out.println ("20,010:" +chinesetonumber.chinesetonumber ("20,010")); System.out.println ("10,000:" +chinesetonumber.chinesetonumber ("10,000")); System.out.println ("1015:" +chinesetonumber.chinesetonumber ("1015")); System.out.println ("1000:" +chinesetonumber.chinesetonumber ("1000")); SYSTEM.OUT.PRINTLN ("100 Million:" +chinesetonuMber. Chinesetonumber ("100 million"));}}
This is the main class, which is used to start programs and tests.
Package Arabic numerals and Chinese numerals; public class Numberchangetochinese {public string numbertochinese (int num) {//Convert an Arabic numeral to Chinese string if (num = = 0) {return "0";} int unitpos = 0;//section of Thrones identifies String all = new String (); String chinesenum = new string ();//Chinese numeric string Boolean Needzero = false;//the next summary needs to be 0 string strins = new string (); while (num> 0) {int section = num%10000;//the last bar if (Needzero) {//To determine if the previous bar thousand is zero, zero to add 0 all = tool.chnnumchar[0] + all;} Chinesenum = Sectiontochinese (section,chinesenum);//Handle the number of the current bar, and then use Chinesenum to record the current bar number if (section!=0) {//here with if else Select the statement to perform the Strins = tool.chnunitsection[unitpos];//when the subsection is not 0, add the node to the throne Chinesenum = Chinesenum + strins;} Else{strins = tool.chnunitsection[0];//Otherwise do not add Chinesenum = Strins + chinesenum;} all = Chinesenum+all;chinesenum = ""; Needzero = (section<1000) && (section>0); num = num/10000;unitpos++;} return all;} Public String sectiontochinese (int section,string chinesenum) {String Setionchinese = new String ();//section section with independent function int Unitpos = The weight counter inside the 0;//bar, Boolean zero = true;//inside the barIt is judged that only one 0 while (section>0) {int v = section%10;//takes the current lowest value if (v = = 0) {if (!zero) {zero = true;//The operation that needs to be zeroed) in each bar. Make sure that the number of consecutive 0 is output only one chinesenum = tool.chnnumchar[0] + chinesenum;}} Else{zero = false;//has nonzero digits, turn the zero switch on Setionchinese = tool.chnnumchar[v];//corresponds to the chinese digit bit Setionchinese = Setionchinese + tool.chnunitchar[unitpos];//corresponds to the Chinese throne chinesenum = Setionchinese + chinesenum;} Unitpos++;section = SECTION/10;} return chinesenum;}}
The second is the class of Arabic numerals translated into Chinese numerals, and the process is roughly the same as in the book.
Here is the third class that converts Chinese into an Arabic array:
Package Arabic numerals and Chinese numerals; public class Chinesechangetonumber {public int chinesetonumber (string str) {string str1 = new String (); String str2 = new string (); String str3 = new string (), int k = 0;boolean Dealflag = true;for (int i=0;i<str.length (); i++) {//First remove "0" from the string if (' 0 ' = = (s Tr.charat (i)) {str = str.substring (0, I) + str.substring (i+1);}} String chinesenum = str;for (int i=0;i<chinesenum.length (); i++) {if (Chinesenum.charat (i) = = ' billion ') {str1 = Chinesenum.substring (0,i);//intercept the number in front of billions, control the table one by one, then convert k = I+1;dealflag = false;//has processed}if (Chinesenum.charat (i) = = ' million ') {str2 = Chinesenum.substring (k,i); str3 = str.substring (i+1);d Ealflag = false;//already processed}}if (Dealflag) {//if not processed STR3 = Chinesenum ;} int result = Sectionchinese (str1) * 100000000 +sectionchinese (STR2) * 10000 + Sectionchinese (STR3); return result;} public int Sectionchinese (String str) {int value = 0;int Sectionnum = 0;for (int i=0;i<str.length (), i++) {int v = (int) to Ol.intList.get (Str.charat (i)); if (v = = Ten | | v = = | | v = = 1000) {//If the value is a throne, multiply the sectionNum = v * sectionnum;value = value + sectionnum;} else if (i = = Str.length ()-1) {value = value + V;} Else{sectionnum = V;}} return value;}}
This really is my own words written, are through the thinking of the results, may be some not rigorous, but it can be achieved.
The fourth one is the tool class, which holds the string data.
Package Arabic numerals and Chinese numerals; import java.util.arraylist;import Java.util.hashmap;import Java.util.set;public class Tool {// Digital bit public static string[] Chnnumchar = {"0", "one", "two", "three", "four", "five", "six", "seven", "eight", "Nine"};p ublic static char[] Chnnumchinese = {' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' Nine '};//section of public static string[] Chnunitsection = {"", "Million", "billion", "trillions"};//the throne public Static string[] Chnunitchar = {"", "ten", "Hundred", "thousand"};p ublic static HashMap intlist = new HashMap (); static{for (int i=0;i<chnn umchar.length;i++) {intlist.put (chnnumchinese[i], i);} Intlist.put (' Ten ', '), Intlist.put (' Hundred ', ' 1000 '); Intlist.put (' thousand ');}}
The following picture is the result of my test:
The first picture is the conversion of Arabic numerals into Chinese:
The second image is translated into Arabic numerals by Chinese numerals:
This digital conversion algorithm, I read a few hours to understand a little, and then in the programming, encountered a lot of problems, but I ended up alone to solve, did not ask others, encountered some basic data types of problems, such as int can not exceed 2.1 billion, "" "two points is the string type," " A point is the character type, the problem I passed in the HashMap queue when I am very upset, the string type passed in, and then use the String Charat () to call, the result is of course an exception error, cannot compile.
Also useful for an unnecessary double cycle to detect, in fact, there is no need, the queue has a ready-made way for me to use, the double cycle also appeared in the death cycle, in the second in the wrong to write the i++ (in fact, it should be J + +), leading to the cycle.
Algorithm learning path is not easy, but you insist, I in programming these hours often can't think out solution, encountered a bug can not solve, but no one asked.
But it doesn't matter, I insisted on down, solve all the problems I encountered, in the algorithm on this road and then a small step forward, I believe that my future it road will be able to go further and farther, more and more wide.
Conversion----------between Arabic numerals and Chinese numerals