コンテストの試験問題(数字を含む中国語の大文字に変換)
6616 ワード
これらのコードはすべて自分で書いたもので、修正されていません.
第一題(この答えはまだ考えていないところがあるかもしれませんが、例えば1000,000の時に結果はゼロになります):
2番目の問題:
第一題(この答えはまだ考えていないところがあるかもしれませんが、例えば1000,000の時に結果はゼロになります):
package com.shengshiyuan.competition;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DigitalConversion {
public void conversion(String str) {
//
if (this.isNumeric(str)) {
// 8
if (str.length() <= 8) {
String result = this.getNumberToRMB(str);
System.out.println(result);
} else {
System.out.println(" 8 !");
}
} else {
System.out.println(" , !");
}
}
/**
*
* : isNumeric <br>
* : TODO <br>
* :
* : Nov 4, 2013 11:08:50 AM
* @param str
* @return
*/
public boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
/**
*
* : getNumberToRMB <br>
* : , Bug 1、 2、 0
* :
* : Nov 4, 2013 11:31:48 AM
* @param rmb
* @return
*/
public String getNumberToRMB(String rmb) {
/**//*
* (num) (dw)
*/
// String num = " ";
String num = " ";
String dw = " ";
// , “0”
// rmb += rmb.indexOf(".") == -1 ? ".00" : "00";
// String mm[] = rmb.split("//.");
// System.out.println("mm" + mm);
// String money = mm[0];
String money = rmb;
System.out.println(money);
// String money = rmb;
/**//*
*
*/
// String result = num.charAt(Integer.parseInt("" + mm[1].charAt(0)))
// + " " + num.charAt(Integer.parseInt("" + mm[1].charAt(1)))
// + " ";
String result = "";
// String result = "";
if (money.startsWith("0")) {
while (money.startsWith("0")) {
money = money.substring(1, money.length());
}
}
/**//*
* ,
*/
for (int i = 0; i < money.length(); i++) {// i= ,
String str = "";
int n = Integer.parseInt(money.substring(money.length() - i - 1,
money.length() - i));// =n
System.out.println("n" + n);
str = str + num.charAt(n);//
System.out.println("str" + str);
//
if (i == 0) {
str += dw.charAt(i);//
} else if ((i + 4) % 8 == 0) {
str += dw.charAt(4);//
} else if (i % 8 == 0) {
str += dw.charAt(5);//
} else {
str += dw.charAt(i % 4);// ( )
}
result = str + result;// ( )
}
System.out.println("result:" + result);
result = result.substring(0, result.length() - 1);
result = result.replaceAll(" ([^ ])", " ");
result = result.replaceAll(" + ", " ");
result = result.replaceAll(" +", " ");
result = result.replaceAll(" ([ ])", "$1");
result = result.replaceAll(" ", " ");
// result = result.replaceAll("^ ", "");
// result = result.replaceAll(" ", " ");
// result = result.replaceAll(" ", " ");
if (result.endsWith(" ")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
public static void main(String[] args) {
new DigitalConversion().conversion("10000100");
}
}
2番目の問題:
package com.shengshiyuan.competition;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* : Test <br>
* : TODO <br>
* :
* : Nov 1, 2013 10:51:18 AM
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test {
}
package com.shengshiyuan.competition;
/**
*
* : MyClass <br>
* : TODO <br>
* :
* : Nov 1, 2013 10:49:02 AM
*/
public class MyClass {
public void method1() {
System.out.println("method1");
}
@Test
public void method2() {
System.out.println("method2");
}
@Test
public int add(int a, int b) {
return a + b;
}
@Test
public void doSomething(String str) {
System.out.println(str);
}
@Test
public void doSomething2() {
System.out.println("doSomething2()");
}
}
package com.shengshiyuan.competition;
import java.lang.reflect.Method;
/**
* run ,run @Test public void
* : ApplicationRun <br>
* : TODO <br>
* :
* : Nov 1, 2013 10:54:07 AM
*/
public class ApplicationRun {
public void run(String className) throws Exception {
Class<?> targetClass = Class.forName(className);
Object object = targetClass.newInstance();
Method[] methods = targetClass.getDeclaredMethods();
for (Method method : methods) {
// Test
if (null != method.getAnnotation(Test.class)) {
Class<?>[] parameClass = method.getParameterTypes();
if (0 == parameClass.length) {
// method
method.invoke(object, new Object[] {});
}
}
}
}
public static void main(String[] args) throws Exception {
ApplicationRun application = new ApplicationRun();
application.run("com.shengshiyuan.competition.MyClass");
}
}