当前位置:网站首页>华为机试题-20190417
华为机试题-20190417
2022-07-02 06:25:00 【霏霏小雨】
介绍
本次机试题,发生时间:2019年04月17日,19:00。
上一次试题,发生时间:2019年04月03日,19:00。
本次机试3题,均AC。
1
题目描述:
给定一个数组,里面有6个整数,求这个数组能够表示的最大24进制的时间是多少,输出这个时间,无法表示输出invalid。
输入描述:
输入为一个整数数组,数组内有六个整数。
输入整数数组长度为6,不需要考虑其他长度,元素值为0或者正整数,6个数字每个数字只能使用一次。
输出描述:
输出为一个24进制格式的时间,或者字符串“invalid”。
示例1
输入
[0,2,3,0,5,6]
输出
23:56:00
示例2
输入
[9,9,9,9,9,9]
输出
invalid
备注:
输出时间格式为xx:xx:xx格式。
代码
//直接暴力解决。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final String s = "invalid";
final String c = ":";
Scanner in = new Scanner(System.in);
String tempString = in.nextLine();
in.close();
tempString = tempString.substring(1, tempString.length() - 1);
String [] tempInt = tempString.split(",");
tempString = null;
int [] input = new int [6];
for(int i = 0; i < 6; i ++) {
input[i] = Integer.parseInt(tempInt[i]);
}
tempInt = null;
Arrays.sort(input);
ArrayList<Integer> list = new ArrayList<>();
for(int i = 0; i < 6; i ++) {
list.add(input[i]);
}
input = null;
StringBuilder output = new StringBuilder();
int i = getSpeci(list, 3); //第一位
if(i != -1 ) {
output.append(list.remove(i));
} else {
System.out.print(s);
return ;
}
if(output.charAt(0) == '2') {
i = getSpeci(list, 4); //第二位
if(i != -1 ) {
output.append(list.remove(i));
output.append(c);
}else {
System.out.print(s);
return ;
}
} else {
i = getSpeci(list, 10); //第二位
if(i != -1 ) {
output.append(list.remove(i));
output.append(c);
}else {
System.out.print(s);
return ;
}
}
i = getSpeci(list, 6); //第三位
if(i != -1 ) {
output.append(list.remove(i));
}else {
System.out.print(s);
return ;
}
i = getSpeci(list, 10); //第四位
if(i != -1 ) {
output.append(list.remove(i));
output.append(c);
}else {
System.out.print(s);
return ;
}
i = getSpeci(list, 6); //第五位
if(i != -1 ) {
output.append(list.remove(i));
}else {
System.out.print(s);
return ;
}
i = getSpeci(list, 10); //第六位
if(i != -1 ) {
output.append(list.remove(i));
}else {
System.out.print(s);
return ;
}
System.out.print(output);
}
static int getSpeci(ArrayList<Integer> list, int speci) {
for(int i = list.size() - 1; i > -1; i --) {
if(list.get(i) < speci) {
return i;
}
}
return -1;
}
}
2
题目描述:
小王手里有点闲钱,想着做点卖水果的小买卖。给出两个数组m、n,用m[i]代表第i个水果的成本价,n[i]代表第i水果能卖出的价钱,假如现在有本钱k,试问最后最多能赚多少钱?
说明:
1 每种水果只需买一次,只能卖一次
2 数组m、n大小不超过50
3 数组元素为正整数,不超过1000
输入描述:
1 数组m、n
2 本钱k
备注:
1 首行输入逗号分隔的数组m的元素值
2 第二行输入逗号分隔的数组n的元素值
3 第三行输入本钱
输出描述:
最多能赚取多少钱。
示例1
输入
4,2,6,4
5,3,8,7
15
输出
22
说明
样例计算过程:
先买前3种水果,全部卖出,再买第4种水果,再卖出,最后本金变为22。
代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static class B implements Comparable<B> {
int m;
int n;
B(int m, int n) {
this.m = m;
this.n = n;
}
@Override
public int compareTo(B o) {
if(m > o.m) {
return 1;
}
if(m < o.m) {
return -1;
}
if(n > o.n) {
return -1;
}
if(n < o.n) {
return 1;
}
return 0;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String [] mm = in.nextLine().split(",");
String [] nn = in.nextLine().split(",");
int k = in.nextInt();
B [] b = new B [mm.length];
for(int i = 0; i < b.length; i ++) {
b[i] = new B(Integer.parseInt(mm[i]), Integer.parseInt(nn[i]));
}
mm = null;
nn = null;
Arrays.sort(b);
for(int i = 0; i < b.length; i ++) {
if(k >= b[i].m && b[i].n > b[i].m) {
k += (b[i].n - b[i].m);
}
}
System.out.print(k);
in.close();
}
}
3
题目描述
某多处理器多道批处理系统一次允许讲所有作业调入内存,且能并行执行,其并行数等于处理器个数。该系统采用SJF的调度方式(最短作业优先,系统在调度时,总是优先调度执行处理时间最短的作业)。
现给定处理器个数m,作业数n,每个作业的处理时间分别为t1, t2, … tn。
当n>m时,首先处理时间短的m个作业进入处理器处理,其他的进入等待,当某个作业处理完成时,依次从等待队列中取出处理时间最短的作业进入处理。
求系统处理完所有作业的耗时为多少?
注:不考虑作业切换的消耗。
输入描述:
输入2行,第一行为2个整数(采用空格分隔),分别标识处理器个数m个作业数n;第二行输入n个整数(采用空格分隔),标识每个作业的处理时长t1, t2,… tn。0<m, n<100,0<t1, t2,… tn<100。
输出描述:
输出处理总时长
示例1
输入
3 5
8 4 3 1 10
输出
13
备注:
注:不用考虑输入合法性。
代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
static class CPU{
int time;
CPU() {
time = 0;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int [] task = new int [n];
for(int i = 0; i < n; i ++) {
task[i] = in.nextInt();
}
in.close();
ArrayList<CPU> cpu = new ArrayList<>();
for(int i = 0; i < m; i ++) {
cpu.add(new CPU());
}
Arrays.sort(task);
int total = 0;
int index = 0;
while(true) {
int i = getZero(cpu);
if(i == 101) {
break;
}
if(i >= 0) {
if(index == n) {
cpu.remove(i);
} else {
cpu.get(i).time = task[index];
index ++;
}
} else {
total -= i;
}
}
System.out.print(total);
}
static int getZero(ArrayList<CPU> cpu) {
if(cpu.size() == 0) {
return 101;
}
int min = 100;
for(int i = 0; i < cpu.size(); i ++) {
if(cpu.get(i).time == 0) {
return i;
}
min = min > cpu.get(i).time ? cpu.get(i).time : min;
}
for(int i = 0; i < cpu.size(); i ++) {
cpu.get(i).time -= min;
}
return -min;
}
}
边栏推荐
- spark sql任务性能优化(基础)
- Brief analysis of PHP session principle
- Network security -- intrusion detection of emergency response
- Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
- Review of reflection topics
- Check log4j problems using stain analysis
- 一个中年程序员学习中国近代史的小结
- php中的二维数组去重
- Sqli-labs customs clearance (less15-less17)
- Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr
猜你喜欢
TCP攻击
UEditor . Net version arbitrary file upload vulnerability recurrence
Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql
CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現
Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence
Sqli-labs customs clearance (less15-less17)
sparksql数据倾斜那些事儿
ORACLE EBS ADI 开发步骤
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
ssm超市订单管理系统
随机推荐
Oracle rman半自动恢复脚本-restore阶段
ARP攻击
MySQL无order by的排序规则因素
JSP intelligent community property management system
JS create a custom JSON array
Go package name
Sqli Labs clearance summary - page 2
2021-07-17c /cad secondary development creation circle (5)
Oracle segment advisor, how to deal with row link row migration, reduce high water level
CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現
ORACLE 11.2.0.3 不停机处理SYSAUX表空间一直增长问题
Pyspark build temporary report error
oracle EBS标准表的后缀解释说明
SSM实验室设备管理
Sqli labs customs clearance summary-page3
叮咚,Redis OM对象映射框架来了
TCP attack
ORACLE EBS ADI 开发步骤
SQLI-LABS通關(less6-less14)
php中通过集合collect的方法来实现把某个值插入到数组中指定的位置