今日头条笔试

来源:互联网 发布:新版香港电视直播软件 编辑:程序博客网 时间:2024/04/28 08:30

题目1:

给定两个长度为 n 的整数数列 A 和 B。再给定 q 组查询,每次查询给出两个整数 x 和 y,求满足 Ai >= x 且 Bi >= y 这样的 i 的数量。
输入
第一行给定两个整数 n 和 q。
第二行给定数列 A,包含 n 个整数。
第三行给定数列 B,包含 n 个整数。
接下来 q 行,每行两个整数 x 和 y,意义如上所述。
输出
对于每组查询,输出所求的下标数量。


样例输入
3 2
3 2 4
6 5 8
1 1
4 8
样例输出
3
1

tips:不难,想,x,y可以看成坐标系内的点,所以问题可以转换为求坐标系目标点右上方点的个数。

import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;/** * Created by changqing on 2017/4/6. */class Point{    int x;    int y;} //模仿c++里面的结构体 class findNumbers {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while (scanner.hasNext()) {            int n = scanner.nextInt();            int q = scanner.nextInt();            Point[] p = new Point[100];            for (int i = 0; i < n; i++) {                p[i] = new Point();                p[i].x = scanner.nextInt();            }            for (int i = 0; i < n; i++) {                p[i].y = scanner.nextInt();            }            Arrays.sort(p, 0, n, new Comparator<Point>() {                @Override                public int compare(Point o1, Point o2) {                    return o1.x - o2.x;                }            });            for (int i = 0; i < q; i++) {                int a = scanner.nextInt();                int b = scanner.nextInt();                int j;                int count = 0;                for (j = 0; j < n && a > p[j].x; j++) ;                for (int k = j; k < n; k++) {                    if (b <= p[k].y)                        count++;                }                System.out.println(count);            }        }    }                             a}




题目2:

找出函数的最宽尖峰
时间限制:C/C++语言 2000MS;其他语言 4000MS
内存限制:C/C++语言 131072KB;其他语言 655360KB
题目描述:
按数组的形式给出函数f(x)的取值,即数组A的A[0]元素为f(0)的取值,数组的取值都为整数,函数在每个点都是严格单调递增或者严格递减(即A[i-1] != A[i] != A[i+1]),要求找出最宽的先上升后下降的区间(这个区间内函数的值必须先上升到一个点然后下降,区间的上升段和下降段长度必须都大于0)。
1. 如果找到符合条件的最大区间输出数组对应的左右下标(有多个最大区间时,输出最左边的那个”)
2. 找不到那么输出-1 -1
输入
n
n长度的整数数组
输出
区间的范围


样例输入
10
1 3 1 2 5 4 3 1 9 10
样例输出
2 7

Hint
数据规模
对于 100% 的数据,1 <=n <=10, 000, 000

import java.util.Scanner;/** * Created by changqing on 2017/4/6. */public class longesCommon {    public static void main(String[] args) {        int a[]=new int [100];        Scanner scanner=new Scanner(System.in);                while(scanner.hasNext())                {                    int n=scanner.nextInt();                    for (int i = 0; i < n; i++)                        a[i]=scanner.nextInt();                    int sum;                    int max = 3;                    int left = -1, right = -1;                    int low, high;                    for (int j = 1; j < n - 1; j++) {                        if (a[j] > a[j - 1] && a[j] > a[j + 1]) {                            sum = 2;                            for (low = j; low > 0; low--) {                                if (a[low] > a[low - 1])                                    sum++;                                else                                    break;                            }                            for (high = j; high < n - 1; high++) {                                if (a[high] >a[high + 1])                                    sum++;                                else                                    break;                            }                            if (sum > max) {                                left = low;                                right = high;                                max=sum;                            }                        }                    }                    System.out.println(left+" "+right);                }    }}

题目3

第一行是两个整数 N 和 M。
接下来的 N+M 行,每行包含一个句子。
前 N 行代表段落中的句子,后 M 行表示查询。
输出
输出 M 行,每行代表查询的结果。


样例输入
6 3
An algorithm is an effective method that can be expressed within a finite amount of space and time
Starting from an initial state and initial input the instructions describe a computation
That when executed proceeds through a finite number of successive states
Eventually producing output and terminating at a final ending state
The transition from one state to the next is not necessarily deterministic
Some algorithms known as randomized algorithms incorporate random input
Next to the transition
Wormhole infinite time and space
The transition from one state to the next is not necessarily deterministic
样例输出
The transition from one state to the next is not necessarily deterministic
An algorithm is an effective method that can be expressed within a finite amount of space and time
The transition from one state to the next is not necessarily deterministic


Hint
数据规模
0 < L[i] < 512
0 < W[i] < 32
对于 30% 的数据,0 < N < 30,0 < M < 30。
对于 100% 的数据,0 < N <= 600,0 < M <= 800。

tips:利用HashMap,key用来存储每个单词,value保存单词对应数量,对于查询句子,利用ContainsKey()函数查看hashmap里面是否目标单词,存在,加上对应的key。






0 0
原创粉丝点击