1.
按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
//M代表输入的字符串的个数
//N代表输出字符串的位数,不够0补齐
Scanner scan = new Scanner(System.in);
System.out.println("输入字符串的个数M:");
int M = scan.nextInt();
System.out.println("输入字符串的位数N:");
int N = scan.nextInt();
for(int i=0;i<M;i++){
System.out.println("输入字符串:");
String str = scan.next();
char[] arr = str.toCharArray();
System.out.print(str);
int diff = N-arr.length;
for(int j=0;j<diff;j++){
System.out.print("0");
}
System.out.println();
}
}
}
2. 拼音转数字
输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下:
描述: 拼音 yi er san si wu liu qi ba jiu
阿拉伯数字 1 2 3 4 5 6 7 8 9
输入字符只包含小写字母,所有字符都可以正好匹配
运行时间限制:无限制
内存限制: 无限制
输入: 一行字符串,长度小于1000
输出: 一行字符(数字)串
样例输入: yiersansi
样例输出: 1234
import java.util.ArrayList;
import java.util.Scanner;
public class Demo5 {
//拼音转数字
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("输入要识别的字符串:");
String str = scan.nextLine();
char[] arr = str.toCharArray();
if(arr.length>=1000){
return;
}
StringBuffer sb = new StringBuffer();
for(int i=0;i<arr.length;i++){
sb.append(arr[i]);
if(sb.toString().matches("yi")){
System.out.print("1");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("er")){
System.out.print("2");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("san")){
System.out.print("3");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("si")){
System.out.print("4");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("wu")){
System.out.print("5");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("liu")){
System.out.print("6");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("qi")){
System.out.print("7");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("ba")){
System.out.print("8");
sb = null;
sb = new StringBuffer();
}//if
if(sb.toString().matches("jiu")){
System.out.print("9");
sb = null;
sb = new StringBuffer();
}//if
}
}
}
3. 去除重复字符并排序
运行时间限制:无限制
内容限制: 无限制
输入: 字符串
输出: 去除重复字符并排序的字符串
样例输入: aabcdefff
样例输出: abcdef
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Demo6 {
//去除重复字符串并排序
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("输入带排序的字符串:");
String str = scan.next();
int j;
char[] arr1 = str.toCharArray();
ArrayList<Character> list = new ArrayList<Character>();
list.add(arr1[0]);
//将不同的字符放入一个新的数组之中
for(int i=0;i<arr1.length;i++){
for(j=0;j<i;j++){
if(arr1[j]==arr1[i])break;
if(j==i-1){
list.add(arr1[i]);
}
}//for
}//for
//排序,按从小到大的顺序排列
Collections.sort(list);
for(char c:list){
System.out.print(c);
}
}
}
注://题目来源 http://blog.csdn.net/hackbuteer1/article/details/39253767,自己用java重新实现了一遍。