华为2016实习生上机测试


第一题



本人实现代码:

package com.wl.test;

import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String data = cin.nextLine();
        String[] parts = data.split(",");
        StringBuffer buffer = new StringBuffer();
        
        int n = Integer.parseInt(parts[0]);
        for(int i=0; i<parts[1].length(); i++) {
            char c = parts[1].charAt(i);
            if(c >= 'A' && c <= 'Z') {
                buffer.append(c);
            }
        }
        System.out.println(buffer.toString());
    }
}



第二题




本人实现代码:

package com.wl.test;

import java.util.Scanner;

public class Mainn {
	
	public static void main(String[] args) {
		int[] result;
		
		Scanner cin = new Scanner(System.in);
		String d = cin.nextLine();
		
		String[] parts = d.split(" ");
		StringBuffer buffer = new StringBuffer();
		result = new int[Integer.parseInt(parts[0])];
		
		for (int i = 0; i < result.length; i++) {
			result[i] = Integer.parseInt(parts[i + 1]);
		}
		
		quickSort(result, 0, result.length - 1);
		
		int var = result[0];
		buffer.append(String.valueOf(var) + " ");
		
		for (int i = 1; i < result.length; i++) {
			if (result[i] != var) {
				var = result[i];
				buffer.append(String.valueOf(result[i]) + " ");
			}
		}
		System.out.println(buffer.toString().trim());
	}

	private static void quickSort(int[] array, int low, int high) {
		int pivot;
		if (low < high) {
			pivot = partition(array, low, high);
			quickSort(array, low, pivot - 1);
			quickSort(array, pivot + 1, high);
		}
	}

	private static int partition(int[] array, int low, int high) {
		int pivotkey = array[low];
		while (low < high) {
			while (low < high && array[high] >= pivotkey)
				high--;
			swap(array, low, high);
			while (low < high && array[low] <= pivotkey)
				low++;
			swap(array, low, high);
		}

		return low;
	}

	private static void swap(int[] a, int m, int n) {
		int x = a[m];
		a[m] = a[n];
		a[n] = x;
	}
}



第三题




本人实现代码:

package com.wl.test;

import java.util.Hashtable;
import java.util.Scanner;

public class Main {

        public static void main(String[] args) {

            Hashtable<Character, Integer> table = new Hashtable<>();
            Scanner cin = new Scanner(System.in);
            String tree = cin.nextLine();
            String input = cin.nextLine();
            StringBuffer buffer = new StringBuffer();

            int treeHeight = 0;
            char name = '\0';
            String value = "";
            for(int i=0; i<tree.length(); i++) {
                char c = tree.charAt(i);
                if((c >= 'a' && c<= 'z') || (c>='A' && c<='Z')) {
                    if(name != '\0') {
                        int x = Integer.parseInt(value);
                        if(x > treeHeight) {
                            treeHeight = x;
                        }
                        table.put(name, x);
                        name = '\0';
                        value = "";
                    }
                    name = c;
                }
                if(c >= '0' && c <= '9') {
                    value += c;
                }
            }
            if(name != '\0') {
                int x = Integer.parseInt(value);
                if(x > treeHeight) {
                    treeHeight = x;
                }
                table.put(name, x);
                name = '\0';
                value = "";
            }

            for(int i=0; i<input.length(); i++) {

                if(table.get(input.charAt(i)) != null)
                    buffer.append(String.valueOf(treeHeight - table.get(input.charAt(i)) + 1) + " ");
                else
                    buffer.append("0 ");
            }

            System.out.println(buffer.toString().trim());
        }

}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值