九度online judge-A + B
来源:互联网 发布:java io 装饰器模式 编辑:程序博客网 时间:2024/04/20 17:55
题目来源: http://ac.jobdu.com/problem.php?pid=1010
我的代码:
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.util.HashMap;
import
java.util.Map;
public
class
Main{
public
static
void
main (String[] args){
BufferedReader br =
new
BufferedReader(
new
InputStreamReader(System.in));
String ss =
null
;
String[] sa,sa1,sa2;
int
num1,num2;
try
{
while
((ss = br.readLine()) !=
null
){
sa = ss.split(
"\\+"
);
sa1 = sa[
0
].split(
" "
);
sa2 = sa[
1
].split(
" "
);
num1 = trans(sa1);
num2 = trans(sa2);
if
((num1 ==
0
) && (num2 ==
0
)){
break
;
}
System.out.println(num1 + num2);
}
}
catch
(IOException e){
e.printStackTrace();
}
}
public
static
int
trans(String[] sa){
Map<String,Integer> map=
new
HashMap<String, Integer>();
map.put(
"nine"
,
9
);
map.put(
"eight"
,
8
);
map.put(
"seven"
,
7
);
map.put(
"six"
,
6
);
map.put(
"five"
,
5
);
map.put(
"four"
,
4
);
map.put(
"three"
,
3
);
map.put(
"two"
,
2
);
map.put(
"one"
,
1
);
map.put(
"zero"
,
0
);
int
num =
0
;
int
i;
for
(i =
0
;i < sa.length; i ++){
sa[i] = sa[i].toLowerCase();
if
(map.get(sa[i]) ==
null
){
continue
;
}
switch
(map.get(sa[i])){
case
9
: num =
10
* num +
9
;
break
;
case
8
: num =
10
* num +
8
;
break
;
case
7
: num =
10
* num +
7
;
break
;
case
6
: num =
10
* num +
6
;
break
;
case
5
: num =
10
* num +
5
;
break
;
case
4
: num =
10
* num +
4
;
break
;
case
3
: num =
10
* num +
3
;
break
;
case
2
: num =
10
* num +
2
;
break
;
case
1
: num =
10
* num +
1
;
break
;
case
0
: num =
10
* num +
0
;
break
;
default
:
break
;
}
}
return
num;
}
};
注意:
1.String .split() 是java.lang函数
http://www.cnblogs.com/mingforyou/archive/2013/09/03/3299569.html
(1).如果用“.”作为分隔的话,必须是如下写法,String.split("\\."),这样才能正确的分隔开,不能用String.split(".");
(2).如果用“|”作为分隔的话,必须是如下写法,String.split("\\|"),这样才能正确的分隔开,不能用String.split("|");
“.”和“|”都是转义字符,必须得加"\\";
(3).如果有多个分隔符,可以用|连接,如,“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split("and|or");(4).对于一些用特殊符号分隔的必须用转义字符,不然会抛出异常,或者得不到我们预期的结果
split的参数regex是一个 regular-expression的匹配模式而不是一个简单的String,他对一些特殊的字符可能会出现你预想不到的结果
常见的 "|" -> "\\|" "*" -> "\\*" "+" -> "\\+" "\" ->"\\\"
2. switch-case语句
switch语句的判断条件可以接受int,byte,char,short, enum不能接受其他类型
case后面必须是常量表达式constant expressions
一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break,利用这一特性可以让好几个case执行统一语句
switch在java7才开始支持判断字符串,针对switch不能支持字符串这个问题,可以使用map容器
map<int,string> (或 map<string,int> ),用字符串对应的索引值(键值)来进行匹配
顺便普及一下枚举类型:
java.lang.Enum.ordinal()方法返回此枚举常量的序数(其枚举声明中的位置,其中初始常量分配的序数为零)。
例子:
package com.yiibai;
import java.lang.*;
// enum showing Mobile prices
enum Mobile {
Samsung(400), Nokia(250),Motorola(325);
int price;
Mobile(int p) {
price = p;
}
int showPrice() {
return price;
}
}
public class EnumDemo {
public static void main(String args[]) {
System.out.println("CellPhone List:");
for(Mobile m : Mobile.values()) {
System.out.println(m + " costs " + m.showPrice() + " dollars");
}
Mobile ret = Mobile.Samsung;
System.out.println("The ordinal is = " + ret.ordinal());
System.out.println("MobileName = " + ret.name());
}
}
输出:
CellPhone List:
Samsung costs 400 dollars
Nokia costs 250 dollars
Motorola costs 325 dollars
The ordinal is = 0
MobileName = Samsung
看完这个例子就比较了解枚举了。
3.switch null http://www.ylzx8.cn/kaifayuyan/program/301058.html
Java中null表示一个不确定的对象,所以它是没有类型的,null本身不是对象,也不是Objcet的实例
所以null更不属于int,byte,char,short, enum中的任一类型,所以传入null的话会报错
- 九度online judge-A + B
- 1003 A+B 九度Online Judge
- 1000 计算a+b 九度Online Judge
- 1001 A+B for Matrices 九度Online Judge
- 题目 1001 A+B for Matrices 九度Online Judge
- 题目 1010 A + B 九度Online Judge
- 题目 1003 A+B 九度Online Judge
- 题目 1026 又一版 A+B 九度Online Judge
- 九度Online Judge:题目1000:计算a+b
- 九度Online Judge
- 九度 Online Judge 算法 刷题 题目1015:还是A+B
- HelloWorldForU ---九度Online Judge
- 九度 online judge 1202
- 九度 online judge 1601
- 九度OnLine Judge-1553
- 九度online:题目1003:A+B
- 九度online judge-二叉搜索树
- 九度online judge-开门人和关门人
- java调用R出错
- C/C++编译链接过程详解
- 用户手机客户端登录Login页面跳转到手机MobileLogin页面问题总结
- Android Studio运行项目报错: Failed to complete Gradle execution
- InnoDB列压缩,提升DB性能
- 九度online judge-A + B
- Map集合的基本操作
- POJ_2151_Check the difficulty of problems_概率DP
- Map集合的两种取出方式
- Linux GPIO操作分析 - Exynos 5260
- OV手机无线快门测评
- Map练习(基本自定义类型练习)
- 对于form.show() 和Application.run(form)的理解
- 1002