Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Java Roman character conversion plastic number
Java Roman character conversion plastic number
I looked up the notation of decimal and Roman numerals. ?

The first is binary:?

Roman? Numerals? (? Roman numerals? )? :?

Me? =? 1?

v? =? 5?

x? = 10?

l? =? 50?

c? =? 100?

d? =? 500?

m? =? 1000?

Law:?

Three? =? 1+ 1+ 1=3?

Four? =? 5- 1=4?

VI? =? 5+ 1=6?

IX? =? 10- 1=9?

XI? =? 10+ 1= 1 1?

XXI? =? 10+ 10+ 1=2 1?

LXX=50+ 10+ 10=70?

LXXX=80?

XC= 100- 10=90?

,,,,,,,,,,,,?

So, to sum up, numbers represent numbers in descending order of weight from left to right, similar to Arabic numerals. The difference is that the same number can only appear three times in a row. When representing 4 or 9, you need to put the number with small weight, such as X( 10), before the number with large weight, and do subtraction.

//////////////////////////////////////////////////////////////////////

Import? Java . util . *;

Class? Testing? {

Public? Static electricity Invalid? Conversion (string? Rome)? {

String? Map? =? “I 1v5x 10l 50 c 100d 500m 1000”;

int[]? arr? =? New? int[luoma . length()];

For what? (int? Me? =? 0; ? Me? & lt? luoma . length(); ? i++)? {

String? Reggie? =? "^.*("? +? Luoma.charAt (1)? +? ”)(\\d+)。 *$";

arr[i]? =? integer . parse int(map . replace all(reg,"$2"));

}

String? First of all? =? arrays . tostring(arr);

int[]? Clone 1? =? arr . clone();

//Descending judgment, not starting with 4 or 9.

For what? (int? Me? =? 0; ? Me? & lt? Array length; ? i++)? {

For what? (int? j? =? Me? +? 1; ? j? & lt? Array length; ? j++)? {

What if? (arr[i]? & lt? arr[j])? {

int? tmp? =? arr[I];

arr[i]? =? arr[j];

arr[j]? =? tmp

}

}

}

//? If it is falling, add.

What if? (first . equals(arrays . tostring(arr)))? {

int? sum? =? 0;

String? The result? =? "";

For what? (int? Me? =? 0; ? Me? & lt? Array length; ? i++)? {

sum? +=? arr[I];

The result? +=? arr[I];

What if? (me? & lt? What is the length? -? 1)? {

The result? +=? "+";

}

}

System.out.format("%s=%s=%d%n ",? Rome? And the result? sum);

}? Or what? {

//Ascending order, starting from 4 and 9.

For what? (int? Me? =? 0; ? Me? & lt? Array length; ? i++)? {

For what? (int? j? =? Me? +? 1; ? j? & lt? Array length; ? j++)? {

What if? (arr[i]? & gt? arr[j])? {

int? tmp? =? arr[I];

arr[i]? =? arr[j];

arr[j]? =? tmp

}

}

}

//If it is up or down, you need to judge whether it starts with 4 or 9 after you get the result, otherwise you will enter an error.

What if? (first . equals(arrays . tostring(arr)))? {

int? sum? =? arr[arr.length? -? 1];

String? The result? =? sum? +? "-";

For what? (int? Me? =? What is the length? -? 2; ? Me? & gt=? 0; ? I-)? {

sum? -=? arr[I];

The result? +=? arr[I];

What if? (me? & gt? 0)? {

The result? +=? "-";

}

}

What if? (! (sum? +? "").matches("^[94].*$"))? {

//Input error

System.out.println ("input error");

}? Or what? {

The result? =? result.replaceAll("-$ ",? "");

System.out.format("%s=%s=%d%n ",? Rome? And the result? sum);

}

}? Or what? {

//Input error

System.out.println ("input error");

}

}

}

Public? Static electricity Invalid? main(String[]? args)? {

Scanner? sc? =? New? Scanner (system. ? Are you online? );

What time? (really)? {

System.out.print ("Enter Roman characters:");

String? Rome? =? sc.nextLine()。 trim();

What if? (! luoma.matches("^[IVXLCDM]+$”)? ||

//The same number can only appear three times in a line.

luoma.matches("^[ivxlcdm]*([ivxlcdm])\\ 1{3,}[ivxlcdm]*$”)? {

System.out.print ("Input error, exit mercilessly!" );

sc . close();

Break;

}? Or what? {

Convert to (Rome);

}

}

}

}