ACM練習問題-java作成
6139 ワード
1. かっこのペアリングの問題:
2. 人民元転換問題
検証結果が正しいかどうかは、オンラインコンバータを参照してください. http://link.fobshanghai.com/rmb.htm
package com.util;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* : , , 。
*
* : N(0 i; j--) {
char ele_op = source.charAt(j);
if ((ele == char1 && ele_op == char2) || (ele == char3 && ele_op == char4)) {
String subStr = source.substring(i + 1, j);
if ((subStr == null || subStr.length() == 0) || isMatchKuohao(subStr)) {
flag = true;
i = j + 1;
break;
}
}
}
if (!flag) {
return false;
}
}
return true;
}
}
2. 人民元転換問題
検証結果が正しいかどうかは、オンラインコンバータを参照してください. http://link.fobshanghai.com/rmb.htm
package com.util;
import java.util.HashMap;
import java.util.Map;
public class TestAmount {
public static final String zero = " ";
public static final String[] nums = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
public static final String[] unit_one = new String[] { "", " ", " ", " " };
public static final String[] unit_two = new String[] { "", " ", " " };
public static void main(String[] args) {
String amount = convertAmount("90001010436.10");
System.out.println(amount);
}
public static String convertAmount(String source) {
StringBuilder sb = new StringBuilder();
sb.append(" ");
if (source.indexOf(".") > 0) {
String[] strArr = source.split("\\.");
if (strArr.length == 2) {
long number1 = Long.parseLong(strArr[0]);
int number2 = Integer.parseInt(strArr[1]);
sb.append(convertIntAmount(number1));
sb.append(" ");
sb.append(convertFloatAmount(number2));
}
} else {
long number = Long.parseLong(source);
sb.append(convertIntAmount(number));
sb.append(" ");
}
return sb.toString();
}
public static StringBuilder convertFloatAmount(int number) {
StringBuilder sb = new StringBuilder();
int jiao = number / 10;
int fen = number % 10;
if (jiao > 0) {
sb.append(nums[jiao]);
sb.append(" ");
}
if (fen > 0) {
sb.append(nums[fen]);
sb.append(" ");
}
return sb;
}
public static String convertIntAmount(long number) {
if((number + "").length() > 11){
throw new IllegalArgumentException(" , 1000 !");
}
StringBuilder sb = null;
int pointer = 0;
boolean isLastZero = false;
do {
int result = (int) (number % 10000);
number = number / 10000;
Map resultMap = calRemainder(result, (number > 0), isLastZero);
StringBuilder remainder = (StringBuilder) resultMap.get("remainderStr");
isLastZero = (resultMap.get("isFirstZero") != null) && (Boolean) resultMap.get("isFirstZero");
if (remainder.length() != 0) {
if (!(remainder.length() == 1 && zero.equals(remainder.toString()))) {
remainder.append(unit_two[pointer]);
}
} else {
if (!isLastZero) {
remainder.append(zero);
}
}
if (sb != null) {
remainder.append(sb);
}
sb = remainder;
pointer++;
} while (number > 0);
return sb.toString();
}
public static Map calRemainder(int number, boolean isContinue, boolean isLastRemainderZero) {
StringBuilder sb = new StringBuilder();
int beginVal = 1000;
int pointer = 3;
int flag;
boolean isLastZero = false;
Map resultMap = new HashMap();
do {
flag = number / beginVal;
// 、 、
if (flag != 0) {
sb.append(nums[flag]);
sb.append(unit_one[pointer]);
isLastZero = false;
} else {
if (isLastRemainderZero && number == 0) {
isLastZero = true;
}
if (!isLastZero && isContinue) {
sb.append(zero);
isLastZero = true;
}
if (pointer == 3) {
resultMap.put("isFirstZero", isLastZero);
}
}
number = number % beginVal;
pointer--;
beginVal /= 10;
} while (number > 0);
resultMap.put("remainderStr", sb);
return resultMap;
}
}