题目链接
下面的题是从简单到难
1.潜在好友
Description
小X在搬砖写一个论坛,这个时候老板突然想到一个功能,让小X今天赶快实现。大概就是如果某个人是你好友的好友那么他的头像上面会有特殊的标志。小X想不到较好的办法来解决如何验证两个人是不是好友的好友,现在向你求助。Input
第一行是一个整数T(1<=T<=100),代表数据组数。 每组数据第一行是两个整数n,m(1<=n,m<=10000),代表这两个人的好友的数量。 之后n行是第一个人的好友id 再之后m行是第二个人的好友id (1<=id<=1e9)Output
如果第二个人是第一个人的好友的好友输出Yes否则NoSample Input
2
1 1
1
2
2 3
1
2
2
3
4- Sample Output
No
Yes - 题目分析
这个题目就是求输入的两组数字中有没有相同的数字
方法一
将第一个数组中的数字放入set集合中,第二个数组中的每一个数字去判断集合中是否包含它,如果包含,最后输出Yes,不包含输出No。
Java语言
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Set<Integer> set = new HashSet<Integer>();
int T,n,m,x;
T = in.nextInt();
for(int j=0; j<T; j++){
n = in.nextInt();
m = in.nextInt();
boolean flag = false;
set.clear(); //注意每次都要clear
for(int i=0; i<n; i++){
x = in.nextInt();
set.add(x);
}
for(int i=0; i<m; i++){
x = in.nextInt();
if(set.contains(x)){
flag = true;
}
}
if(flag){
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}
}
方法二
先把两个数组排序,然后两个数组从小到大比较,依次判断后移,找到相同的数说明有共同好友。
C++语言代码
#include<iostream>
#include<algorithm>
using namespace std;
const int maxnm = 10000;
int a[maxnm],b[maxnm];
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--){
int flag = 0;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
for(int i=0; i<m; i++){
scanf("%d",&b[i]);
}
sort(b,b+m);
int cur1=0,cur2=0;
while(cur1<n && cur2<m){
if(a[cur1] < b[cur2]){
cur1++;
}else if(a[cur1] > b[cur2]){
cur2++;
}else{
flag = 1;
break;
}
}
printf("%s\n",flag?"Yes":"No");
}
}
java代码描述
import java.util.*;
public class Main {
public static void main(String[] args) {
final int maxnm = 10001;
Scanner in = new Scanner(System.in);
int[] a = new int[maxnm];
int[] b = new int[maxnm];
int T,n,m;
T = in.nextInt();
for(int j=0; j<T; j++){
n = in.nextInt();
m = in.nextInt();
boolean flag = false;
for(int i=0; i<n; i++){
a[i] = in.nextInt();
}
Arrays.sort(a,0,n);
for(int i=0; i<m; i++){
b[i] = in.nextInt();
}
Arrays.sort(b,0,m);
int cur1 = 0, cur2 = 0;
while(cur1<n && cur2<m){
if(a[cur1] < b[cur2]){
cur1++;
}else if (a[cur1] > b[cur2]) {
cur2++;
}else{
flag = true;
break;
}
}
if(flag){
System.out.println("Yes");
}else {
System.out.println("No");
}
}
in.close();
}
}
拓展
今天今日头条的笔试题也有一道类似的,但是必须要求要按照第二个数组输入的顺序输出所有相同的数字,运用方法一可以。
2.John and Health rate
Description
The cold and flu season is here.John is worried about his cow. In order to monitor the situation of his cow,he do some inspecting everyday,and record the “health rate” for each cow.The “health rate” is a integer which can show the cow’s health condition.The higher a cow’s health rate is ,the healthier the cow is.What’s more,the doctor told John that the k-th small health rate is dangerous.Because the doctor thought there are at most k healthy cows. So John has to find out which number is the k-th small.Can you help him?Input
Input contains multiple test cases.
The first line contains two integers n,k (1 ≤ n ≤ 1000000,1<=k<=n) — the number of cows and dangerous number. The second line contains n integers,and the i-th integer ai(-10000<=ai<=10000) is the i-th cow’s health rateOutput
Output a single line with the k-th small health rate.Sample Input
2 1
3 2
5 2
-1 0 -2 5 3- Sample Output
2
-1
-题目分析
这个题说白了就是求一个数组中第k大的数字。
方法一
直接排序,然后输出第k个数。时间复杂度是O(N*logN),复杂度还是比较高
C++代码
#include<iostream>
#include<algorithm>
using namespace std;
int num[1000000];
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k)){
for(int i=0; i<n; i++){
scanf("%d",&num[i]);
}
sort(num,num+n);
printf("%d\n",num[k-1]);
}
return 0;
}
方法二
其实更好的方法是通排序,因为这儿输入的数字范围是-10000到10000
C语言
#include<stdio.h>
#include<string.h>
#define MAXN 20001
int a[MAXN];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k) != EOF){
memset(a,0,sizeof(a));
int tmp;
for(int i=0; i<n; i++){
scanf("%d",&tmp);
a[tmp+10000]++;
}
int cnt = 0;
for(int i=0; i<=20000; i++){
cnt += a[i];
if(cnt>=k){
printf("%d\n",i-10000);
break;
}
}
}
return 0;
}