在线编程笔试练习2(京东)

野路子 野路子     2022-09-17     705

关键词:

时间限制:1秒 空间限制:32768K 热度指数:9801

题目描述

给你两个集合,要求{A} + {B}。 注:同一个集合中不会有两个相同的元素。

输入描述:

每组输入数据分为三行,第一行有两个数字n,m(0 ≤ n,m ≤ 10000),分别表示集合A和集合B的元素个数。后两行分别表示集合A和集合B。每个元素为不超过int范围的整数,每个元素之间有个空格隔开。

输出描述:

针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开,行末无空格。
示例1

输入

3 3
1 3 5
2 4 6

输出

1 2 3 4 5 6

自己的low方法(没通过)
技术分享
 1 import java.util.Scanner;
 2 
 3 /*
 4  * To change this license header, choose License Headers in Project Properties.
 5  * To change this template file, choose Tools | Templates
 6  * and open the template in the editor.
 7  */
 8 
 9 /**
10  *
11  * @author zhangtao
12  */
13 public class Test2 {
14     public static void main(String[] args)
15     {
16         int m,n;
17         int[] A;
18         int[] B;
19         Scanner scanner=new Scanner(System.in);
20         m=scanner.nextInt();
21         n=scanner.nextInt();
22         A=new int[m];
23         B=new int[n];
24         //录入数据
25         for(int i=0;i<m&&scanner.hasNext();i++)
26         {
27             A[i]=scanner.nextInt();
28         }for(int j=0;j<m&&scanner.hasNext();j++)
29         {
30             B[j]=scanner.nextInt();
31         }
32         //按要求排序输出
33         sortAandB(A,B);
34     }
35     static void sortAandB(int[] A,int[] B)
36     {
37         int totallong=A.length+B.length;
38         int[]C=new int[totallong];
39         //将A与B合并
40         int i=0;
41         while(i<A.length)
42         {
43             C[i]=A[i];
44             i++;
45         }
46         while(i>=A.length&&i<totallong)
47         {
48             C[i]=B[i-A.length];
49             i++;
50         }
51        quickSort(C,0,totallong-1) ;
52        for(int j=0;j<totallong;j++) 
53        {
54            if(j!=totallong-1)
55            {
56                System.out.print(C[j]+" ");
57            }
58            else
59            {
60                 System.out.print(C[j]+"");
61            }
62        }
63     }
64     //快速排序
65     static int partition(int a[], int low, int high) {
66          int privotKey = a[low];                                 //基准元素  
67          while (low < high) {                                    //从表的两端交替地向中间扫描  
68              while (low < high && a[high] >= privotKey) //从high 所指位置向前搜索,至多到low+1 位置。将比基准元素小的交换到低端  
69              {
70                  --high;                                         //从右找比基准元小的
71              }
72              a[low] = a[high];                                    //如果比基准元素小,交换
73              a[high] = privotKey;
74  
75              while (low < high && a[low] <= privotKey) {
76                  ++low;                                          //从右找比基准元大的
77              }
78              a[high] = a[low];                                    //如果比基准元素,交换
79              a[low] = privotKey;
80  
81          }
82          return low;
83      }
84      static void quickSort(int a[], int low, int high) {
85          if (low < high) {
86              int privotLoc = partition(a, low, high);  //将表一分为二  
87              quickSort(a, low, privotLoc - 1);          //递归对低子表递归排序  
88              quickSort(a, privotLoc + 1, high);        //递归对高子表递归排序  
89          }
90      }
91 }
View Code

大神方案一

技术分享
 1 import java.util.Iterator;
 2 import java.util.Scanner;
 3 import java.util.Set;
 4 import java.util.TreeSet;
 5 //集合合并
 6 public class Test2 {
 7  
 8     public static void main(String[] args) {
 9         Scanner scan=new Scanner(System.in);
10         Set<Integer>set=new TreeSet<Integer>();
11         while(scan.hasNext()){
12             String str1=scan.nextLine();
13             String result1[]=str1.split(" ");
14             int n=Integer.parseInt(result1[0]);
15             int m=Integer.parseInt(result1[1]);
16             String str2=scan.nextLine();
17             String result2[]=str2.split(" ");
18             for(int i=0;i<result2.length;i++){
19                 set.add(Integer.parseInt(result2[i]));
20             }
21             String str3=scan.nextLine();
22             String result3[]=str3.split(" ");
23             for(int x=0;x<result3.length;x++){
24                 set.add(Integer.parseInt(result3[x]));
25             }
26             Iterator<Integer>iter=set.iterator();
27             StringBuffer sub=new StringBuffer();
28             while(iter.hasNext()){
29                 sub.append(iter.next()).append(" ");
30             }
31             sub.delete(sub.length()-1, sub.length());
32             System.out.println(sub.toString());
33         }
34  
35     }
36  
37 }
View Code

 大神方案二

技术分享
 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 import java.util.Iterator;
 4 import java.util.Set;
 5 import java.util.TreeSet;
 6     
 7 public class Main {
 8     public static void main(String[] args) throws Exception {
 9         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10         String line = null;
11         while((line = br.readLine()) != null){
12             String[] s = line.split(" ");
13             int n = Integer.parseInt(s[0]);
14             int m = Integer.parseInt(s[1]);
15             Set<Integer> set = new TreeSet<Integer>();
16             line = br.readLine();
17             String[] s1 = line.split(" ");
18             for(int i=0;i<n;i++){
19                 set.add(Integer.parseInt(s1[i]));
20             }
21             line = br.readLine();
22             String[] s2 = line.split(" ");
23             for(int i=0;i<m;i++){
24                 set.add(Integer.parseInt(s2[i]));
25             }
26             Iterator<Integer> it = set.iterator();
27             StringBuffer sb = new StringBuffer();
28             while(it.hasNext()){
29                 sb.append(it.next());
30                 sb.append(" ");
31             }
32             sb.delete(sb.length()-1, sb.length());
33             System.out.println(sb.toString());
34         }
35     }
36 }
View Code

 



京东2019暑期实习生在线笔试(原创)(代码片段)

第一题:题目描述:现有红绿两种颜色的石头,现在我们需要用这两种石头搭建一个塔,塔需要满足如下三个条件:1,第一层应该包含1块石头,第二层应该包含两块,第i块需要包含i块石头;2,同一层的石头应该是同一个颜色... 查看详情

京东笔试编程题:采购单+保卫方案

采购单时间限制:C/C++语言1000MS;其他语言3000MS内存限制:C/C++语言65536KB;其他语言589824KB题目描述:过年啦!小B高兴的不行了,她收到了很多红包,可以实现好多的愿望呢。小B可是对商店货架上心仪的货物红眼好久了,只因囊... 查看详情

微软2017年预科生计划在线编程笔试

LegendaryItems答案是每一件物品需要的期望步数和1#include<bits/stdc++.h>2#definelllonglong3#defineullunsignedlonglong4#definestfirst5#definendsecond6#definepiipair<int,int>7#definepilpair<int,ll>8#def 查看详情

京东笔试(小帆)

2017/04/07京东笔试一个半小时包括100分的单选和0分的综合题哭晕单选包括行测和专业题综合题又遇到了共享单车可见最近这个是有多火问题:1、单车破损严重,针对这个问题,从设计或者运营角度,来解决2、共享单车的“未来... 查看详情

2018京东笔试编程:完善javascript,实现删除一行,增加一行,计算总量。不能改动给出的html。

已给出的代码:<style>body,html{padding:0;margin:0;font-size:14px;color:#000000;}table{border-collapse:collapse;width:100%;table-layout:fixed;}thead{background:#3d444c;color:#ffffff;}td,th{border:1pxsol 查看详情

京东笔试

题目:4和7是两个幸运数字,我们定义,十进制表示中,每一位只有4和7两个数的正整数都是幸运数字。前几个幸运数字是:4,7,44,47,74,77......输入:数字k输出:第k个幸运数样例输入:3510010000000样例输出:74744747447744474474774744444471... 查看详情

京东笔试

N个数进栈,出栈序列有多少个?这个问题属于卡特兰数(h(n)=C(2n,n)/(n+1)(n=1,2,3,...))的应用某系统中有3个并发进程,都需要同类资源4个,试问该系统保证不会发生死锁的最少资源数是______。A.9个B.10个C.4个D.12个3*3+1 查看详情

京东笔试-交易清单

...很小,因此需要考虑算法的时间复杂度。题目如下:京东笔试-交易清单(京东2016实习生真题)题目描述金融证券行业超好的 查看详情

python3牛客网:oj在线编程常见输入输出练习(acm模式)(代码片段)

 牛客网:校招笔试真题_C++工程师、golang工程师_牛客网其他语言输入输出见链接1.输入两个数,输入数据包括多组。whileTrue:try:a=list(map(int,input().split()))print(a[0]+a[1])except:breakwhileTrue:try:a,b=map(int,input( 查看详情

京东2021校园招聘笔试(8.27编程部分)——数据开发工程师(数列变换a了9%)(代码片段)

目录前言一、滚球游戏代码:DP二、数列变换(调试了一个小时A了9%,盖了帽了print(34)就是9%)代码:常规前言如果你从本文中学习到丝毫知识,那么请您点点关注、点赞、评论和收藏大家好࿰... 查看详情

在线verilog编程学习(代码片段)

在线Verilog编程学习一、门电路学习1.非门2.与门或非门二、组合逻辑相关练习:1.二对一多路复用2.全加器3.卡诺地图三、时序逻辑相关练习1.D触发器2.D锁存器3.1~12的计数器参考资料一、门电路学习1.非门问题描述:htt... 查看详情

第五届字节青训营笔试后端编程练习题解(代码片段)

文章目录前言T1.36进制加法(模拟)题面思路代码T2.电影院选座(DFS)题面思路代码T3.IP地址(DFS)题面思路代码前言前段时间🐏了,今天简单写了一下,不知道如何提交代码进行评测,题... 查看详情

京东笔试——秋招笔试题(代码片段)

#include<iostream>#include<cstdio>#include<algorithm>#include<set>usingnamespacestd;/*题解: a^b=c^d, 底数相等的情况: 1)底数为1,即1^b=1^d的情况:n*n 2)底数不为1,即a^b=a^b的情况:(n-1)*n 底数不相等的情况: 拆成(i 查看详情

京东研发类-编程题-买水果

笔试的时候,没有考虑到购买多样水果的时候,每样水果的重复次数,因此没能AC。现改正,测试了几个用例。publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);while(sc.hasNext()){String[]s1=sc.nextLine().split("");//价格数目intn=Integer.value... 查看详情

微软2017年预科生计划在线编程笔试第二场elsueno

树上背包。简单的树形$dp$,计算出摧毁每一个节点所需的最小费用,背包即可。#include<bits/stdc++.h>usingnamespacestd;structX{intfa;intin;intip;intc;}s[2010];intdp[2010][20010];intcost[2010];intf[20010];vector<int>g[2010];intn, 查看详情

微软2017年预科生计划在线编程笔试第二场diligentrobots

模拟。不断分裂,然后计算时间,取个最小值。我也不知道这做法对不对的,读完题猜了一下,抱着$WA$的心态$submit$了,然后跳出一个$AC$。#include<bits/stdc++.h>usingnamespacestd;longlongn;intq;intmain(){scanf("%lld%d",&n,&q);longlongnow=1;... 查看详情

微软2017年预科生计划在线编程笔试第二场queenattack

排序。分别按照$x$,$y$以及对角线排序,统计一下方案数就可以了。#include<bits/stdc++.h>usingnamespacestd;intn;structX{intx,y;}s[100010];boolcmp1(Xa,Xb){returna.x<b.x;}boolcmp2(Xa,Xb){returna.y<b.y;}boolcmp3(Xa,Xb){retu 查看详情

今日头条2018aicamp5月26日在线笔试编程题第一道——最佳路径(代码片段)

题目给定一个n*m的矩阵A,矩阵中每一个元素为一个十六进制数。寻找一条从左上角都右下角的路径,每次只能向右或者向下移动, 使得路径上所有数字之积在16进制下的后缀0最少。输入描述:第一行:n,m(2<=n,m<=1000) ... 查看详情