我是靠谱客的博主 贪玩音响,最近开发中收集的这篇文章主要介绍9.13---华为机试,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1---整数倒序

import java.util.Scanner;


public class Test1 {

	 public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);
	        while (sc.hasNext()) {
	            String input = sc.nextLine();
	            int flag = 1;
	            int len = input.length();
	            int start = 0;
	            int resLen = len;
	            if (input.charAt(0) == '-') {
	                flag = -1;
	                resLen = len - 1;
	                start = 1;
	            }
	            System.out.println(resLen);
	            if (flag == -1)
	                System.out.print("-");
	            for (int i = start; i < len; i++) {
	                System.out.print(input.charAt(i));
	                if (i == len - 1)
	                    System.out.println();
	               else
	                    System.out.print(" ");
	            }
	            if (flag == -1)
	                System.out.print("-");
	            for (int i = len - 1; i >= start; i--) {
	                System.out.print(input.charAt(i));
	            }
	        
	        }

	 }
}	
2---第二题是ip重合

import java.util.Scanner;

/**
 * Created by lab on 2017/9/13.
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in 

);
        while (sc.hasNext()) {
            String[] ip1 = sc.nextLine().split("\.");
            String[] ip2 = sc.nextLine().split("\.");
            String[] ip3 = sc.nextLine().split("\.");
            String[] ip4 = sc.nextLine().split("\.");
            long start1 = 0;
            long end1 = 0;
            long start2 = 0;
            long end2 = 0;
            for (int i = 0, j = 3; i < ip1.length; i++, j--) {
                start1 += Math.pow(256, j) * Integer.parseInt(ip1[i]);
            }
            for (int i = 0, j = 3; i < ip2.length; i++, j--) {
                end1 += Math.pow(256, j) * Integer.parseInt(ip2[i]);
            }
            for (int i = 0, j = 3; i < ip3.length; i++, j--) {
                start2 += Math.pow(256, j) * Integer.parseInt(ip3[i]);
            }
            for (int i = 0, j = 3; i < ip4.length; i++, j--) {
                end2 += Math.pow(256, j) * Integer.parseInt(ip4[i]);
            }
            if ((start1 >= start2 && start1 <= end2) || (start2 >= start1 && start2 <= end1))
                System.out.println("Overlap IP");
            else
                System.out.println("No Overlap IP");
        }
    }
}

3----第三题是排序 
public class Test6 {
		public static void main(String[] args) {
			 Scanner in = new Scanner(System.in);
		     String[] str = in.nextLine().split(" ");
		     int n = Integer.parseInt(in.nextLine());

			//故此采用插入排序算法实现
		     int j;
		    for(int i = 1;i < str.length; i++){
		    	String tempStr = str[i];
		    	for( j= i; j> 0 && compare(tempStr,str[j-1]);j--){
		    		str[j] = str[j-1];
					
		    	}
		    	str[j] = tempStr;
		    }
		    
		    for(int i =0 ; i < str.length; i++){
		    	System.out.print(str[i] + " ");
		    }
		    System.out.println();
		     System.out.println(str[n-1]);
		}
		
		
		public static boolean compare(String str1,String str2){
			int a1,a2;
			if(str1.length() >=4){
				a1 = Integer.parseInt(str1.substring(str1.length() - 3)); 
			}else{
				a1 = Integer.parseInt(str1);
			}
			if(str2.length() >=4){
				a2 = Integer.parseInt(str2.substring(str2.length() - 3)); 
			}else{
				a2 = Integer.parseInt(str2);
			}
			if(a1<a2){
				return true;
			}
			return false;
		}
	}




import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * Created by lab on 2017/9/13.
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in 

);
        while (sc.hasNext()) {
            String[] arr = sc.nextLine().split(" ");
            int k = Integer.parseInt(sc.nextLine());
            List<IntegerNum> list = new ArrayList<IntegerNum>();
            for (int i = 0; i < arr.length; i++) {
                list.add(new IntegerNum(i, arr[i]));
            }
            Collections.sort(list);
            System.out.println(list.get(k - 1).value);
        }
    }

    public static class IntegerNum implements Comparable<IntegerNum> {
        public int orderId;
        public String value;
        public int end;
        public int start;

        public IntegerNum(int oderId, String value) {
            this.orderId = oderId;
            this.value = value;
            this.end = this.value.length();
            if (this.end < 3)
                this.start = 0;
            else
                this.start = end - 3;
        }

        @Override
        public int compareTo(IntegerNum o) {
            if (this.value.substring(start, end).equals(o.value.substring(o.start, o.end)))
                return this.orderId - o.orderId;
            return Integer.parseInt(this.value.substring(start, end)) - Integer.parseInt(o.value.substring(o.start, o.end));
        }
    }
}



最后

以上就是贪玩音响为你收集整理的9.13---华为机试的全部内容,希望文章能够帮你解决9.13---华为机试所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部