java 华为机试题_20150910 15:15华为机试题(java版本)

算法林林总总,此处仅供参考

第一题:(rh)

0818b9ca8b590ca3270a3433284dd417.png

import java.util.HashMap;

import java.util.Scanner;

public class Main {

public static int stringCount(String str) {

String[] array = str.split(",");

int lengthArray=Integer.parseInt(array[0]);

HashMap strList = new HashMap();

String regex="([A-Z]|[a-z]|[0-9]|-|_){0,}";

for (int x = 0; x < lengthArray; x++) {

if (!strList.containsKey(array[1].charAt(x))) {

if((String.valueOf(array[1].charAt(x))).matches(regex)){

strList.put(array[1].charAt(x), 1);

}

} else {

int count = strList.get(array[1].charAt(x)) + 1;

strList.put(array[1].charAt(x), count);

}

}

return strList.size();

}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String s = in.nextLine();

System.out.println(stringCount(s));

}

}

第二题:(Felix)

0818b9ca8b590ca3270a3433284dd417.png

import java.util.Scanner;

public class Main {

//思路:逐个匹配log2^N判断是否为整数即可

public static int countLog2N(String str) {

String[] list = str.split(",");

int count = 0;

for (int i = 0; i < list.length; i++) {

int temp = new Integer(list[i]);

if (((Math.log(temp) / Math.log(2)) % 1 == 0))

count++;

}

return count;

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String s = input.nextLine();

System.out.println(countLog2N(s));

}

}

第三题:(Felix)

0818b9ca8b590ca3270a3433284dd417.png

import java.util.Scanner;

public class Main {

// 思路:借鉴了某牛的C版本:数组循环主次:1、剩一个就跳出;2、排队到3就拉出去枪毙;3、重置flag确保不超过3

public static int getLastMan(int n) {

int i;

int[] a = new int[n];

i = 0;

int out = 0;

int num = 0;

while (out != n - 1) {

if (a[i] == 0) {

num++;

}

if (num == 3) {

a[i] = 1;

num = 0;

out++;

}

i++;

if (i == n) {

i = 0;

}

}

for (i = 0; i < n; i++) {

if (a[i] == 0) {

return i + 1;

}

}

return -1;

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int s = input.nextInt();

System.out.println(getLastMan(s));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值