这题意思是将一个输入的整型阿拉伯数字转化为罗马数字。

思路是将1-10对应的罗马数字放在字符串数组里,然后发现数据变化规律即可,eg:389 = 300 + 89 +9 分别对应的罗马数字。

public static void main(String[] args) {
  String[] strs = {"I","II","III","IV","V","VI","VII","VIII","IX","X"};
  Scanner input = new Scanner(System.in);
  System.out.print("请输入一个1-3999整数:");
  int n = input.nextInt();
  if(n < 1 || n >3999){
    System.out.print("输入超出范围!");
  }else{
    String result = toRomanNumeral(strs,n);
    System.out.println(result);
  }
}
public static String toRomanNumeral(String[] strs, int n) {
  StringBuffer sb = new StringBuffer();
  if(n > 0 && n <= 10){
    sb.append(strs[n-1]);
  }else if(n>10 && n <=100){
    sb.append(zhuanHua(n/10,strs)).append(strs[n%10-1]);
  }else if(n>100 && n <= 1000){
    sb.append(zhuanHua2(n/100,strs)).append(zhuanHua(n%100/10,strs)).append(strs[n%100%10-1]);
  }else if(n>1000 && n <= 3999){
    for(int i = 0; i < n/1000; ++i){
      sb.append("M");
    }
    n%=1000;
    sb.append(zhuanHua2(n/100,strs)).append(zhuanHua(n%100/10,strs)).append(strs[n%100%10-1]);
  }else{
    sb.append("输入超出范围!");
  }
  return sb.toString();
}
public static String zhuanHua2(int i, String[] strs) {
  StringBuffer sb = new StringBuffer();
  for(int j = 0; j < strs[i-1].length(); ++j){
    char temp = strs[i-1].charAt(j);
    if(temp == 'X'){
      temp = 'M';
    }else if(temp == 'I'){
      temp = 'C';
    }else if(temp == 'V'){
      temp = 'D';
    }
    sb.append(temp);
  }
  return sb.toString();
}
public static String zhuanHua(int i, String[] strs) {
  StringBuffer sb = new StringBuffer();
  for(int j = 0; j < strs[i-1].length(); ++j){
    char temp = strs[i-1].charAt(j);
    if(temp == 'X'){
      temp = 'C';
    }else if(temp == 'I'){
      temp = 'X';
    }else if(temp == 'V'){
      temp = 'L';
    }
    sb.append(temp);
  }
  return sb.toString();
}

Intger To Roman的更多相关文章

  1. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  3. 【leetcode】Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  4. [Leetcode] Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  5. Integer to Roman -- LeetCode 012

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  6. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  7. No.013:Roman to Integer

    问题: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from ...

  8. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  9. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

随机推荐

  1. java实现Excel的导入、导出

    一.Excel的导入 导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性.由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正) ...

  2. touchmover手机移动端的拖动

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name= ...

  3. Ionic2+ 环境搭建

    ionic2+官方guide 基础环境安装 nodejs安装 ionic,cordova安装 npm install -g ionic cordova 项目创建 ionic start MyIonic ...

  4. 错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server 是无法给远程连接的用户权限问题

    错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server 是无法给远程连接的用 ...

  5. tcp/ip通信传输流

    利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信,发送端从应用层往下走,接收端则往应用层方向走. 我们用HTTP进行举例 客户端在应用层发出想要看到某个web页面的http请求.HT ...

  6. VB6之CRC32

    翻译篇:http://www.cnblogs.com/duzouzhe/archive/2009/08/05/1539543.html Private Declare Function GetTick ...

  7. CentOS升级Python到2.7版本

    查看python的版本 1 python -V Python 2.4.3 1.先安装GCC 1 yum -y install gcc 2.下载Python-2.7.2 1 wget http://py ...

  8. Postgresql快速写入/读取大量数据(.net)

    环境及测试 使用.net驱动npgsql连接post数据库.配置:win10 x64, i5-4590, 16G DDR3, SSD 850EVO. postgresql 9.6.3,数据库与数据都安 ...

  9. HTML RGB 颜色表 16进制表 颜色对应表

    HTML RGB 颜色表 16进制表 颜色对应表  16 常用颜色表(颜色 + RGB + 名字): Color Value Name   Color Value Name   #00FFFF aqu ...

  10. Quartz的Hello world

    1.准备环境jar包 Your project will need (at least) the Quartz core library, named quartz-x.y.z.jar (where ...