【真题编程】2017去哪儿网

题目描述

Please create a function to extract the filename extension from the given path,return the extracted filename extension or null if none.

输入描述:

输入数据为一个文件路径

输出描述:

对于每个测试实例,要求输出对应的filename extension
示例1

输入

复制
Abc/file.txt

输出

复制
txt

很简单的一道题,找到点的位置,截取后面字符串

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String path=sc.nextLine();
			int pos=path.indexOf(".");
			if(pos>0){
				System.out.println(path.substring(pos+1, path.length()));
			}else{
				System.out.println("null");
			}
			

		}

	}

}



题目描述(这道题需要再练习,一开始没有看到这么容易的办法,把钱数填到数组中,钱数的下标就是对应的天数)

酒店房间的价格录入是通过时间段来录入的,比如10月1日至10月7日800元,10月8日至10月20日500元,请实现以下函数int[][] merge(int[][] dateRangePrices),输入是某个酒店多个日期段的价格,每个日期段(终止日期大于等于起始日期)和对应的价格使用长度为3的数组来表示,比如[0, 19, 300], [10, 40, 250]分别表示从某天开始第1天到第20天价格都是300,第11天到第41天价格都是250,这些日期端有可能重复,重复的日期的价格以后面的为准, 请以以下规则合并并输出合并结果:
1.相邻两天的价格如果相同,那么这两个日期段应该合并
2.合并的结果应该以起始日期从小到大排序

输入描述:

输入数据包括多行,如样例输入所示。

输出描述:

输出数据为一行,如样例输出所示
示例1

输入

复制
1 1 100
2 3 100
4 5 110

输出

复制
[1, 3, 100],[4, 5, 110]

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// Windows上是 ctrl +z 结束输入,因为这个问题看了两天也是醉醉的
		Scanner sc = new Scanner(System.in);
		int a[] = new int[100000];
		int maxDay = Integer.MIN_VALUE;
		while (sc.hasNext()) {
			String str = sc.nextLine();
			String s[] = str.split(" ");
			int begin = Integer.parseInt(s[0]);
			int end = Integer.parseInt(s[1]);
			int money = Integer.parseInt(s[2]);
			
			for (int i = begin; i <= end; i++) {
				a[i] = money;
			}
			//maxDay记住数组大小
			maxDay = Math.max(end, maxDay);
		}
		
		int begin = -1;
		for (int i = 0; i <= maxDay; i++) {
			if (a[i] != 0) {
				if (begin == -1) {
					begin = i;
				}
				if (a[i] != a[i + 1]) {
					if (i == maxDay) {
						System.out.print("[" + begin + ", " + i + ", " + a[i] + "]");
					} else {
						System.out.print("[" + begin + ", " + i + ", " + a[i] + "],");
					}
					begin = -1;
				}
			}
		}
	}

}


题目描述

18位身份证的编码规则是:
前1、2位数字表示:所在省(直辖市、自治区)的代码
第3、4位数字表示:所在地级市(自治州)的代码
第5、6位数字表示:所在区(县、自治县、县级市)的代码;
第7—14位数字表示:出生年、月、日;
第15、16位数字表示:所在地的派出所的代码;
第17位数字表示性别:奇数表示男性,偶数表示女性;
第18位数字是校检码,用来检验身份证的正确性。
用户在输入身份证的过程中经常会输入错误,为了方便用户正确输入需要在输入过程中对用户的输入按照 6+8+4 的格式进行分组,实现一个方法接收输入过程中的身份证号,返回分组后的字符

输入描述:

输入数据有多行,每一行是一个输入过程中的身份证号

输出描述:

分组后的字符串
示例1

输入

复制
5021
502104 198803
5021041988033084
502104198803308324

输出

复制
5021
502104 198803
502104 19880330 84
502104 19880330 8324

简单

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNextLine()){
			String str=sc.nextLine();
			//首先替换掉里面的空格
			str=str.replaceAll(" ", "");
			
			if(str.length()<6){
				System.out.println(str);
			}else if(str.length()>6&&str.length()<14){
				System.out.println(str.substring(0, 6)+" "+str.substring(6));
			}else{
				System.out.println(str.substring(0, 6)+" "+str.substring(6,14)+" "+str.substring(14));
			}
		}

	}

}


题目描述

给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。

输入描述:

输入数据一个字符串,包括字母,数字等。

输出描述:

输出首先出现三次的那个英文字符
示例1

输入

复制
Have you ever gone shopping and

输出

复制
e

这道题的坑在于首先出现三次,也就是说它可能后来还会出现,但是出现三次你就需要把它打出来,而且是按照出现顺序,不是字典顺序,所以选择用map

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNextLine()) {
			String str = sc.nextLine();
			char[] array = str.toCharArray();
			Map<Character, Integer> map = new HashMap<>();
			for (int i = 0; i < array.length; i++) {
				//判断是字母
				if (((int) array[i] >= 65 && (int) array[i] <= 90) || ((int) array[i] >= 97 && (int) array[i] <= 122)) {
					//map中是否包含,包含了就加一,不包含就加入
					if (map.containsKey(array[i])) {
						map.put(array[i], map.get(array[i]) + 1);
						if (map.get(array[i]) == 3) {
							System.out.println(array[i]);
							return;
						}
					} else {
						map.put(array[i], 1);
					}
				}
			}
		}

	}

}


猜你喜欢

转载自blog.csdn.net/wenyimutouren/article/details/80537668