我做的三道题分别是”字符串识别和过滤“、“完美数字”、“拼音翻译为阿拉伯数字”。
1、字符串识别和过滤
要删除字符串中出现的连续重复的字符,例如aaaaa,输出a;
输入格式:6,aaaaaa
输出格式:a
import java.util.Scanner;
public class Main {
public static void Print(String str) {
char s = str.charAt(2);
System.out.print(s);
for(int i = 3; i < str.length(); i++) {
if(s != str.charAt(i)) {
s = str.charAt(i);
System.out.print(s);
}
}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String str = null;
if(in.hasNext())
str = in.nextLine();
Print(str);
}
}
2、完美数字
找出一个界限中能同时被2,3,5整除的数,输出个数,例如【30,60】,输出2。
输入格式:30 60
输出格式:2
import java.util.Scanner;
public class Main {
public static void Print(int low, int high) {
int num = 0;
for(int i = low; i <= high; i++) {
if(i % 2 == 0) {
if(i % 3 == 0) {
if(i % 5 == 0) {
num ++;
}
}
}
}
System.out.print(num);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int low = 0;
int high = 0;
if(in.hasNextInt()) {
low = in.nextInt();
high = in.nextInt();
}
Print(low,high);
}
}
显然这一题是有规律的,但是时间复杂度依然是O(n)
3、拼音翻译为阿拉伯数字
就是把JiuQian这样的数字拼音变成9000这样的阿拉伯数字数字,每个数字拼音首字母必须大写,且Shi,Bai,Qian,Wan这样的单位前面必定有数字,大小不超过10万。
import java.util.Scanner;
public class Main {
public static void Print(String str) {
boolean flag = false;
int num = 0;
int count = 0;
int i = 0;
while(i < str.length()){
if(str.charAt(i) == 'B') {
i = i + 2;
if(i >= str.length()) {
if(flag == true)
count = count + num;
i--;
num = 8;
flag = true;
}else if(str.charAt(i) == 'i') {
count = count + num*100;
flag = false;
}else {
if(flag == true)
count = count + num;
i--;
num = 8;
flag = true;
}
}else if(str.charAt(i) == 'E') {
if(flag == true)
count = count + num;
num = 2;
flag = true;
}else if(str.charAt(i) == 'J') {
if(flag == true)
count = count + num;
num = 9;
flag = true;
}else if(str.charAt(i) == 'L') {
i = i + 2;
if(str.charAt(i) == 'n') {
if(flag == true)
count = count + num;
num = 0;
flag = true;
}else if(str.charAt(i) == 'u'){
if(flag == true)
count = count + num;
num = 6;
flag = true;
}
}else if(str.charAt(i) == 'Q') {
i = i + 2;
if(i >= str.length()) {
if(flag == true)
count = count + num;
i--;
num = 7;
flag = true;
}else if(str.charAt(i) == 'a') {
count = count + num*1000;
flag = false;
}else {
if(flag == true)
count = count + num;
i--;
num = 7;
flag = true;
}
}else if(str.charAt(i) == 'S') {
i++;
if(str.charAt(i) == 'a') {
if(flag == true)
count = count + num;
num = 3;
flag = true;
}else if(str.charAt(i) == 'i'){
if(flag == true)
count = count + num;
num = 4;
flag = true;
}else if(str.charAt(i) == 'h'){
count = count + num*10;
flag = false;
}
}else if(str.charAt(i) == 'W') {
i++;
if(str.charAt(i) == 'a') {
count = count + num*10000;
flag = false;
}else {
if(flag == true)
count = count + num;
num = 5;
flag = true;
}
}else if(str.charAt(i) == 'Y') {
if(flag == true)
count = count + num;
num = 1;
flag = true;
}
i++;
}
if(flag == true) {
count = count + num;
}
System.out.println(count);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String str = null;
if(in.hasNext())
str = in.nextLine();
Print(str);
}
}
这部分代码估计写的不太好,但是是正确的,这个思路有个问题要注意就是在这个中类似Bai和Ba中当他们在字符结尾,直接判断i的时候会有超出数组范围的异常,这里要注意一下。
Bai Ba
Er、
Jiu、
Ling、Liu、
Qi、、Qian
San、
Si、Shi
Wu
、Wan。
Yi、