4.15日華爲筆試

參考:

  1. 華爲筆試,大家做的怎麼樣?
  2. 華爲4.15筆試前兩題AC - 正則表達式
  3. 華爲筆試2.7 4-15 - C++
  4. 華爲4.15筆試 - C++

1. 第一題代碼:

1. 代碼一:(通過率65%)

import java.util.*;

public class Main_1 {
    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        if(sc.hasNextLine())
        {
            String str = sc.nextLine();
            String s[] = str.split(",");
            Arrays.sort(s);
            if(s.length == 1)
            {
                System.out.println(s[0]);
                return;
            }
            // for(int i = 0; i < s.length; i++)
            // {
            //     System.out.print(s[i] + " ");
            // }
            // System.out.println();

            int count = 1;
            int max = 0;
            int index = 0;

            for(int i = 0; i < s.length - 1; i++)
            {
                if((s[i]).equals(s[i + 1]))
                {
                    count++;                  
                }
                else
                {
                    count = 1;
                }    
                if(count > max)
                {
                    max = count;
                    index = i;
                }
            }
            // System.out.println(max);
            // System.out.println(index);
            if(count == 1)
            {
                System.out.println(s[0]);
                return;
            }
            else
            {
                System.out.println(s[index]);
            }
        }
        else
        {
            System.out.println("error.0001");
            // System.exit(0);
        }        
    }
}

// 輸入
// 輸出

// Tom,Lily,Tom,Lucy,Lucy,Jack
// Lucy

// Tom,Lily,Tom,Lucy,Lucy,Tom,Jack
// Tom

// Tom,Lily,Tom,Lucy,Lucy,Jack,Tom,Tomy,Tomy,Tomy
// Tom

// Lily,Lily,Tom,Tom
// Lily

2. 代碼二:(AC)

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

bool cmp(pair<string, int> a, pair<string, int>b)
{
    if(a.second != b.second)
    {
        return a.second > b.second;
    }
    else
    {
        int len  = min(a.first.size(), b.first.size());
        for(int i = 0; i < len; i++)
        {
            if(a.first[i] == b.first[i])
            {
                continue;
            }
            else
            {
                return a.first[i] < b.first[i];
            }
        }
        return a.first.size() < b.first.size();
    }
}

int main()
{
    string s;
    cin >> s;
    bool legal = true;
    string tmp = "";
    unordered_map<string, int> cnt;
    for(int i = 0;i < s.length(); i++)
    {
        if(s[i] == ',')
        {
            cnt[tmp]++;
            tmp.clear();
        }
        else if(s[i] >= 'a' && s[i] <= 'z')
        {
            if(tmp.size() != 0)
            {
                tmp += s[i];
            }
            else
            {
                legal = false;
                break;
            }
        }
        else if(s[i] >= 'A' && s[i] <= 'Z')
        {
            if(tmp.size() == 0)
            {
                tmp += s[i];
            }
            else
            {
                legal = false;
                break;
            }
        }
        else
        {
            legal = false;
            break;
        }
    }
    if(legal == false)
    {
        cout << "error.0001";
    }
    else
    {
        cnt[tmp]++;
        vector< pair<string, int> > buf(cnt.begin(), cnt.end());
        sort(buf.begin(), buf.end(), cmp);
        cout << buf[0].first << endl;
    }
    return 0;
}

2. 第二題代碼:

1 代碼一:

import java.util.*;

// 輸入1:
// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]

// 輸出1:
// 0x17 0xff 0x7
// 0xf0 0xff 0x80

// 輸入2:
// read read_his[addr=0xff,mask=0xff,val=0x1]

// 輸出2:
// FAIL

// 思路:
// 先比對最前面那個字符串,然後是三個屬性,順序不能錯,然後是數值,必須0x或者0X開頭,然後還得有數,以上全通過才行。
// 切分的時候容易弄混,多輸出,弄一步輸出一下就行。(需要注意檢驗數值是否十六進制)

public class Main_2_1 {

    public static boolean isHexNumber(String str)   // 判斷字符串是否爲十六進制數字
    {
        boolean flag = true;
        for(int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
            {
                continue;                
            }
            else
            {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine())
        {
            String string = sc.nextLine();
            // System.out.println();
            string += ',';
            String a[] = string.split(" ");
            String key = a[0];
            String s = a[1];
            // System.out.println(key + " " + s);
            String str[] = s.split("],");
            // for(int i = 0; i < str.length; i++)
            // {
            //     System.out.println(str[i]);
            // }
            boolean flag = false;
            for(int i = 0; i < str.length; i++)
            {
                String sub[] = str[i].split("\\[");
                // for(int j = 0; j < sub.length; j++)
                // {
                //     System.out.print(sub[j] + " ");
                // }
                // System.out.println();
                for(int j = 0; j < sub.length - 1; j++)
                {
                    if((sub[j]).equals(key))
                    {
                        // System.out.println(sub[j + 1]);
                        flag = true;
                        String res[] = sub[j + 1].split("addr=|,mask=|,val=");
                        // System.out.println(res.length);
                        boolean mark = true;
                        for(int k = 1; k < res.length; k++)
                        {
                            if((res[k].startsWith("0x") && isHexNumber(res[k].substring(2))) || (res[k].startsWith("0X") && isHexNumber(res[k].substring(2))))
                            {
                                continue;
                            }
                            else
                            {
                                mark = false;
                            }
                        }
                        if(mark == true)
                        {
                            for(int k = 1; k < res.length; k++)// k == 0時,res[0]輸出的是空格:" ",所以要從k == 1開始輸出
                            {
                                if(k != res.length - 1)
                                {
                                    System.out.print(res[k] + " ");
                                }
                                else
                                {
                                    System.out.println(res[k]);
                                }
                            }
                        }
                        else if(mark == false)  //  三個地址中只要有一個不合法,就打印 FAIL
                        {
                            System.out.println("FAIL");
                        }
                    }
                }
            }
            if(flag == false)
            {
                System.out.println("FAIL");
            }
        }
        sc.close();
    }
}

// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]

// read read_his[addr=0xff,mask=0xff,val=0x1]

// read read[addr=0x17,mask=0xff,val=0z7]

// read read[addr=0x17,mask=0xff,val=0xh]

// read read[addr=0x17,mask=0xff,val=0xH]

2 代碼二:

import java.util.*;
import java.util.regex.*;
// import java.util.regex.Matcher;
// import java.util.regex.Pattern;

// 輸入1:
// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]

// 輸出1:
// 0x17 0xff 0x7
// 0xf0 0xff 0x80

// 輸入2:
// read read_his[addr=0xff,mask=0xff,val=0x1]

// 輸出2:
// FAIL

// 思路:
// 先比對最前面那個字符串,然後是三個屬性,順序不能錯,然後是數值,必須0x或者0X開頭,然後還得有數,以上全通過才行。
// 切分的時候容易弄混,多輸出,弄一步輸出一下就行。(需要注意檢驗數值是否十六進制)

public class Main_2_2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext())
        {
            String str = sc.next();
            Pattern pattern = Pattern.compile(str + "\\[addr=(0[xX][0-9a-fA-F]+),mask=(0[xX][0-9a-fA-F]+),val=(0[xX][0-9a-fA-F]+)\\]?");
            String[] strings = sc.next().split("],");
            boolean flag = false;
            for (String s : strings) {
                // System.out.println(s);
                Matcher matcher = pattern.matcher(s);
                if (matcher.matches()) {
                    flag = true;
                    System.out.println(matcher.group(1) + " " + matcher.group(2) + " " + matcher.group(3));
                }
            }
            if(flag == false)
            {
                System.out.println("FAIL");
            }    
        }
        sc.close();
    }
}

// read read[addr=0x17,mask=0xff,val=0x7],read_his[addr=0xff,mask=0xff,val=0x1],read[addr=0xf0,mask=0xff,val=0x80]

// read read_his[addr=0xff,mask=0xff,val=0x1]

// read read[addr=0x17,mask=0xff,val=0z7]

// read read[addr=0x17,mask=0xff,val=0xh]

// read read[addr=0x17,mask=0xff,val=0xH]

知識點:

  1. 計算機的十六進制數如何表示?
    十六進制數
    數的表示方法:十進制、二進制、八進制、十六進制等,以及二進制數的運算
  2. java中startsWith與endsWith的用法
    java中startsWith與endsWith的用法
    Java startsWith() 方法
    Java endsWith() 方法
  3. Java substring() 方法
    Java substring() 方法
    java關於substring(a)與substring(a,b)的用法
  4. java 判斷字符串是否爲數字 十進制 十六進制
    java 判斷字符串是否爲數字 十進制 十六進制
  5. java分割字符串 Unclosed character class near index 錯誤
    java分割字符串 Unclosed character class near index 錯誤
    Java中的坑之方括號
    Unclosed Character Class Error?
    java.util.regex.PatternSyntaxException: Unclosed character class near index 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章