2015华为机试题

22 篇文章 0 订阅


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重新实现了一遍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值