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

思路是将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. 【LeetCode】160. Intersection of Two Linked Lists

    题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...

  2. 【Android Developers Training】 59. 管理图片存储

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. ReactJS基础(续)

    前边的ReactJS基础,我们可以了解到,对于React,可以说是万物皆组件 React的组件应该具有 可组合(Composeable)可重用(Reusable)可维护(Maintainable)的特 ...

  4. AddNewsServlet -servlet处理响应请求

    package com.pb.news.web.servlet; import java.io.File;import java.io.IOException;import java.util.Dat ...

  5. Spring Security @PreAuthorize 拦截无效

    1. 在使用spring security的时候使用注解,@PreAuthorize("hasAnyRole('ROLE_Admin')") 放在对方法的访问权限进行控制失效,其中 ...

  6. 基于springmvc的hessian调用原理浅析

    一.客户端 1.构造(初始化) 由客户端的配置文件随容器的启动而进行初始化,配置文件如下: <?xml version="1.0" encoding="UTF-8& ...

  7. 终于等到你!MobileTest免费公测,华为带你走出安卓适配大坑

    一.安卓适配之痛真的无解吗? Android平台的诞生对智能手机的普及功不可没,但设备繁多.品牌众多.版本各异,芯片.摄像头.分辨率不统一等等,这些都逐渐成为Android系统发展的障碍,碎片化严重不 ...

  8. 集合用法笔记-Map用法

    一.Map遍历 Map<String, String> map = new HashMap<String, String>(); map.put("1", ...

  9. 面向对象设计模式——观察者(OBSERVER)模式

    定义 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.  Observer模式描述了如何建立这种关系.这一模式中的关键对象是目标(subject ...

  10. 猜年龄---while循环

    #!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Andy Chen age_of_oldboy = 56 count = 0while True ...