华为机试2021-1-29
1、给定一个字符串,统计最长的连续元音字符串。长度小于655535,难度1星
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = null;
while ((s=reader.readLine())!=null){
int count=0;
int max = 0;
if (null==s){
continue;
}
char[] chars = s.toLowerCase().toCharArray();
for (int i=0;i<chars.length;i++){
if (chars[i]=='a' || chars[i]=='e' ||chars[i]=='i' ||chars[i]=='o' ||chars[i]=='u' ){
count++;
}else{
if (count>=max){
max=count;
}
count=0;
}
}
//全部都是元音的情况
if (count>=max){
max=count;
}
System.out.println(max);
}
}
}
2、打车打表器没有数字4,遇到数字4跳过。给定一个打表器上的数字推算实际金额。打表器遇到4,14,24,34,40,41,42.。。。49都不在记录内。例如打表显示5实际金额是4,打表显示25实际是22。难度1星
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main2 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = null;
while ((s=reader.readLine())!=null) {
int num = Integer.valueOf(s);
int length = s.length();
int realNum = 0;
for (int i=1;i<=num;i++){
String a = i+"";
if(a.endsWith("400000000")){
i=i+100000000-1;
realNum =realNum+ 100000000;
continue;
}
if(a.endsWith("40000000")){
i=i+10000000-1;
realNum =realNum+ 10000000;
continue;
}
if(a.endsWith("4000000")){
i=i+1000000-1;
realNum =realNum+ 1000000;
continue;
}
if(a.endsWith("400000")){
i=i+100000-1;
realNum =realNum+ 100000;
continue;
}
if(a.endsWith("40000")){
i=i+10000-1;
realNum =realNum+ 10000;
continue;
}
if(a.endsWith("4000")){
i=i+1000-1;
realNum =realNum+ 1000;
continue;
}
if(a.endsWith("400")){
i=i+100-1;
realNum =realNum+ 100;
continue;
}
if(a.endsWith("40")){
i=i+10-1;
realNum =realNum+ 10;
continue;
}
if (a.contains("4")){
realNum++;
}
}
System.out.println(num-realNum);
}
}
public static int countNum(int num){
int sum = 1;
for (int i=0;i<num;i++){
sum = sum * 10;
}
return sum;
}
}
3、solo和koko是两兄弟,分一堆积木,每块积木有自己的重量。现在他们将积木分成两堆,弟弟koko还小只会将数字转化为二进制进行计算,而且总是忘记进位。弟弟koko按照他计算的方式要求两堆积木重量相等,要不然就会哭。哥哥solo进行分积木希望尽量给弟弟少分,并且不让弟弟哭。eg:弟弟计算方式(难度3星)
1100 十进制 --12
+0101 十进制--5
———
1001 十进制--9
输入
输入的第一行是一个整数N(2 ≤ N ≤ 15),表示有袋中多少块糖。第二行包含N个用空格分开的整数Ci (1 ≤ Ci ≤ 106),表示第i块糖的重量。
输出
如果能让koko不哭,输出Solo所能获得的糖的总重量,否则输出“NO”。
import java.io.IOException;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
int num = Integer.valueOf(scanner.nextLine().trim());
int[] numArr = new int[num];
String[] numStrArr = scanner.nextLine().trim().split(" ");
for (int i=0;i<num;i++){
numArr[i] = Integer.valueOf(numStrArr[i]);
}
if (numArr.length==0 || numArr.length==1){
System.out.println("NO");
return;
}
if (numArr.length==2 && numArr[0] != numArr[1]){
System.out.println("NO");
return;
}
int min = numArr[0];
int sum = min;
int t = min;
for (int i = 1;i<numArr.length;i++){
sum = sum + numArr[i];
if (min>numArr[i]){
min = numArr[i];
}
t ^=numArr[i];
}
if (t!=0){
System.out.println("NO");
}else{
System.out.println(sum-min);
}
}
}
}