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));
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值