HW2018校招研发笔试编程题

1. 数字处理

题目描述:给出一个不多于5位的整数,进行反序处理,要求

(1)求出它是几位数

(2)分别输出每一个数字(空格隔开)

(3)按逆序输出各位数字(仅数字间以空格间隔,负号与数字之间不需要间隔)

输入描述:位数不大于5的整数

输出描述:1.整数位数 2.空格间隔输出结果 3.逆序整数

思路描述:直接进行正负数判断,分别进行处理

【code】

import java.util.Scanner;

/**
 * Created by Administrator on 2018/3/21.
 */
public class NumberProcess {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            //输入
            int n = in.nextInt();

            //转换
            String s = String.valueOf(n);
            char[] array = s.toCharArray();

            if (n >= 0) {
                //输出长度
                System.out.println(array.length);

                //分别输出每一个数字(空格隔开)
                for (int i = 0; i < array.length; i++) {
                    System.out.print(array[i] + " ");
                }

                System.out.println();

                //按逆序输出各位数字
                for (int i = array.length - 1; i >= 0; i--) {
                    System.out.print(array[i] + " ");
                }
            } else {
                //输出长度
                System.out.println(array.length-1);

                //分别输出每一个数字(空格隔开)
                for (int i = 0; i < array.length; i++) {
                    System.out.print(array[i] + " ");
                }

                System.out.println();

                //按逆序输出各位数字,负号与数字之间不需要间隔
                System.out.print(array[0]);
                for (int i = array.length - 1; i > 0; i--) {
                    System.out.print(array[i]+" ");
                }
            }
        }
        in.close();
    }
}

【Result】

12345
5
1 2 3 4 5 
5 4 3 2 1 
-12345
5
- 1 2 3 4 5 
-5 4 3 2 1 

  

2. IP地址交集判断

题目描述:输入四个IP端,前两个为第一个IP段的起始和终止地址,后两个是第二个IP段的起始和终止地址,判断这两个IP段是否存在交集

输入描述:输入4个IP

输出描述:如果存在交集,输出 Overlap IP ; 如果不存在交集,输出 No Overlap IP 

思路描述:开始看题目感觉不是很理解,看到IP地址觉得很复杂。其实真正了解了题意之后发现原来是想多了。将IP地址化成区间,判断两个区间是否存在交集就行了,高中数学。

 【code】

import java.util.Scanner;

/**
 * Created by Administrator on 2018/3/12.
 */
public class isMixed {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            //输入 ['1.1.1.1','255.255.255.255','2.2.2.2','3.3.3.3']
            String[] array = new String[4];
            array[0] = sc.nextLine();
            array[1] = sc.nextLine();
            array[2] = sc.nextLine();
            array[3] = sc.nextLine();

            Long[] newArray = new Long[array.length]; //使用Long,Int会溢出

            //函数
            //分割,去掉.,组合
            for (int i = 0; i < array.length; i++) {
                newArray[i] =Long.parseLong(joinChar(array[i]));
            }

            //判断
            if(newArray[0]<=newArray[1] && newArray[2]<=newArray[3]){
                if(newArray[3]<newArray[0] || newArray[2]>newArray[1]){
                    System.out.println("No Overlap IP");
                }else{
                    System.out.println("Overlap IP");
                }
            }

        }
        sc.close();
    }

    public static String joinChar(String arr) {
        String newArr = "";
        String[] s = arr.split("\\.");
        for (int i = 0; i < s.length; i++) {
            newArr += s[i];
        }
        return newArr;
    }

}

  

【result】

1.1.1.1
255.255.255.255
2.2.2.2
3.3.3.3
Overlap IP

  

3. 数字排序

题目描述: 给定字符串内有很多正整数,要求对这些正整数进行排序,然后返回排序后指定位置的正整数,排序要求:按照每一个正整数的后三位数字组成的整数进行从小到大排序

(1)如果不足三位,则按照实际位数组成的整数进行比较

(2)如果相等,则按照输入字符中的原始顺序排序

说明:(1)字符串以‘\0’结尾,仅包含数字、空格(2)字符串内正整数之间以单个空格分隔,字符串首尾没有空格(3)正整数格式为十进制,大小1~1000000,正整数的数字非零开始

输入描述:第一行为一个整数字符串,里面包含若干个整数,以空格分割,第二行为一个整数,即指定的位置

输出描述:输出指定位置的整数

思路:将每个数字按数字的后三位数的大小比较进行排序,不足三位的保持原数字比较。然后输出。

【code】

import java.util.*;


public class Sort {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {

            //输入  '1223 22 3232 2016', '3'
            String str = sc.nextLine();
            int position = sc.nextInt();


            //按照空格分割
            String[] strArr = str.split(" ");

            //添加元素进集合
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < strArr.length; i++) {
                list.add(Integer.valueOf(strArr[i]));
            }

            //使用Collections的sort方法,并且重写compare方法
            Collections.sort(list, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    int a1;
                    int a2;

                    if (o1 > 1000) {
                        a1 = o1 % 1000;
                    } else {
                        a1 = o1;
                    }
                    if (o2 > 1000) {
                        a2 = o2 % 1000;
                    } else {
                        a2 = o2;
                    }

                    return a1 - a2;
                }
            });

            System.out.println(list.get(position - 1));

        }
        sc.close();
    }

}

  

【result】

1223 22 3232 2016
3
1223

 

-------------------------------------------------------

参考链接: https://www.cnblogs.com/guorange/p/7519222.html  (JS版本的解答)

转载于:https://www.cnblogs.com/hezhiyao/p/8617913.html

108题中有部分题目重合,因此么有收录在压缩文件中。 华为机试 ├─001 字符串最后一个单词长度 │ └─Source ├─002 计算字符个数 │ └─Source ├─003 明明的随机数 │ └─Source ├─004 字符串分隔 │ └─Source ├─005 进制转换 │ └─Source ├─006 质数因子 │ └─Source ├─007 取近似值 │ └─Source ├─008 合并表记录 │ └─Source ├─009 提取不重复的整数 │ └─Source ├─010 字符个数统计 │ └─Source ├─011 数字颠倒 │ └─Source ├─012 字符串反转 │ └─Source ├─013 句子逆序 │ └─Source ├─014 字典序排序 │ └─Source ├─015 求int型正整数在内存中存储是1的个数 │ └─Source ├─016 购物单 │ ├─Debug │ ├─Source │ │ └─Debug │ ├─Source - 时间优先 │ │ └─Debug │ └─Source - 空间优先 │ ├─Debug │ └─Release ├─017 坐标移动 ├─018 识别IP地址分类统计 │ └─Source │ └─Debug ├─019 错误记录 ├─020 密码验证合格程序 ├─021 密码破解 ├─023 删除字符串中出现次数最少字符 │ └─Source │ └─Debug ├─024 合唱队 │ └─Source │ ├─Debug │ └─Release ├─025 数据分类处理 │ └─Source │ └─Debug ├─026 查找兄弟单词 │ └─Source │ └─Debug ├─027 素数伴侣 │ └─Source │ └─Debug ├─028 字符串合并处理 │ └─Source │ └─Debug ├─030 密码截取(查找最长回文字符串) ├─031 蛇形矩阵 │ └─Source │ └─Debug ├─033 判断IP是否属于同一子网 │ └─Source │ └─Debug ├─034 称砝码 │ └─Source │ └─Debug ├─035 学英语 │ └─Source │ └─Debug ├─036 迷宫问题 │ └─Source │ └─Debug ├─037 数独问题 │ └─Debug ├─038 名字漂亮度 │ └─Source │ └─Debug ├─039 字符串截取 │ └─Source │ └─Debug ├─040 单链表删除数据 │ └─Source │ └─Debug ├─041 多线程 │ └─Source │ ├─Backup │ ├─Debug │ │ └─041.tlog │ └─Release │ └─041.tlog ├─042 表达式计算 │ └─Source │ └─Debug ├─043 计算字符串距离 │ └─Source │ └─Debug ├─044 杨辉三角形变形 ├─046 挑7 ├─047 完全数 │ └─Debug ├─048 高精度加法 ├─049 输出n个数中最小的k个 │ └─Debug ├─050 找出字符串只出现一次的字符 │ └─Debug ├─051 组成一个偶数最接近的2个质数 │ └─Debug ├─052 M个苹果放入N个盘子 ├─053 查找整数二进制中1的个数 ├─054 DNA子串 ├─055 MP3光标位置 │ └─Source │ └─Debug ├─056 查找2个字符串最大相同子串 │ └─Debug ├─057 配置文件恢复 │ └─Source │ └─Debug ├─058 24点计算 │ └─Debug ├─059 成绩排序 ├─060 矩阵相乘 ├─061 矩阵乘法次数计算 ├─062 字符串通配符 │ └─Debug ├─066 命令行解析 │ └─Source │ └─Debug ├─067 最大相同子串长度 │ └─Debug ├─068 火车编号进站 │ └─Debug ├─072 数组合并 ├─074 埃及分数 │ └─Source │ └─Debug ├─076 密码截取 │ └─Source ├─077 求最大连续bit数 ├─078 密码强度 ├─079 扑克牌大小 │ └─Source │ └─Debug ├─081 合法IP ├─082 棋盘格子走法 ├─083 在字符串中找出连续最长数字串 ├─084 int数组分组,两组和相等 │ └─Source │ └─Debug ├─086 人民币转换 │ └─Source │ └─Debug ├─087 表示数字 ├─090 自动售货系统 │ └─Source │ └─Debug └─091 24点输出 └─Debug
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值