华为机试编程题学习

算法与数据结构
摘要由CSDN通过智能技术生成

入门题(5题)

(1)输入处理(重要)

在这里插入图片描述

import java.util.Scanner;
import java.lang.Math;

public class Main{
   
    public static void main(String[] args){
   
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
   
            String s = in.nextLine();    //读入数字
            
            int count = 0;    //记录转换后的数字
            
            for(int i=0; i < s.length()-2; i++){
   
                //由于前面两位是'0x',故从第三位开始
                char tc = s.charAt(i+2);    
                int t = 0;    //记录字母转换成的数值
                
                //将字母转换为数值
                if(tc>='0' && tc<='9')
                    t = tc - '0';
                //字母'A'/'a'~'F''f'对应数字10~15
                else if(tc>='A' && tc<='F')
                    t = tc - 'A' + 10;
                else if(tc>='a' && tc<='f')
                    t = tc - 'a' +10;
                //计算加和
                count += t * Math.pow(16, s.length()-i-3);
            }
            System.out.println(count);
        }
    }
}
import java.util.Scanner;
public class Main{
   
    public static void main(String[] args){
   
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
   
            String str = in.nextLine();
            String s1 = str.substring(2);
            int a = Integer.valueOf(s1,16);
            System.out.println(a);
        }
        
    }
}

(3)快速排序

在这里插入图片描述

import java.util.*;

public class Test {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        //获取个数
        int num = sc.nextInt();
        //创建TreeSet进行去重排序
        TreeSet set = new TreeSet();
        //输入
        for(int i =0 ; i < num ;i++){
   
            set.add(sc.nextInt());
        }

        //输出
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
   
            System.out.println(iterator.next());
        }
    }
}

(4)哈希表

在这里插入图片描述

import java.util.HashSet;
import java.util.Scanner;

public class Main {
   

    public static void main(String[] args) {
   
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        String str=in.next();
        HashSet<Character> hs=new HashSet<Character>();
        for(int i=0;i<str.length();i++)
            hs.add(str.charAt(i));
        System.out.println(hs.size());
    }

}

(5)递归

在这里插入图片描述

    public static int JumpFloor(int n) {
   
        return Fibonacci(n, 1, 1);
    }

    public static int Fibonacci(int n, int a, int b) {
   
        if (n <= 1)
            return b;
        return Fibonacci(n - 1, b, a + b);
	}
   public static int JumpFloor(int n) {
   
        return Fibonacci(n, 1, 1);
    }

    public static int Fibonacci(int n, int a, int b) {
   
        if (n <= 1)
            return b;
        return Fibonacci(n - 1, b, a + b);
	}
class Solution {
   
public:
    int jumpFloor(int number) {
   
        if (number <= 0) {
   
            return 0;
        }
        if (number == 1) {
   
            return 1;
        }
        if (number == 2) {
   
            return 2;
        }
        int first = 1, second = 2, third = 0;
        for (int i = 3; i <= number; i++) {
   
            third = first + second;
            first = second;
            second = third;
        }
        return third;
    }
};

字符串操作

在这里插入图片描述

import java.util.*;
import java.io.*;

public class Main{
   
    public static void main(String[] args) throws IOException {
   
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] in = bf.readLine().split(";");
        int x = 0;
        int y = 0;
        for(String s : in){
   
            // 不满足题目给定坐标规则
            if(!s.matches("[WASD][0-9]{1,2}")){
   
                continue;
            }
            int val = Integer.valueOf(s.substring(1));
            switch(s.charAt(0)){
   
                case 'W':
                    y += val;
                    break;
                case 'S':
                    y -= val;
                    break;
                case 'A':
                    x -= val;
                    break;
                case 'D':
                    x += val;
                    break;
            }
        }
        System.out.println(x+","+y);
    }
}

在这里插入图片描述

import java.util.*;
import java.util.regex.*;
public class Main{
   
    public static void main(String[] arg){
   
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
   
            String str = sc.next();
            if(str.length() <= 8){
   
                System.out.println("NG");
                continue;
            }
            if(getMatch(str)){
   
                System.out.println("NG");
                continue;
            }
            if(getString(str, 0, 3)){
   
                System
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值