1. 程式人生 > >2018年華為實習生招聘三道程式設計題

2018年華為實習生招聘三道程式設計題

第一題:給定一個字串,輸出字串中連續最長的數字串,並把這個最長數字串的長度輸出來,中間以逗號(,)隔開。如果存在長度一樣的的連續數字串,返回最後一個連續數字串。

樣例:abcd12345ed125ss123058789

output:   123058789,9

分析:新建兩個StringBuffer,一個是用來儲存目前連續數字的字串,另一個用來儲存最長的連續數字字串,不斷比較更新。

java程式碼如下:

/*
 尋找字串的最長數字子串
 */
import java.util.Scanner;
import java.util.ArrayList;
public class Huawei1{
public static void main(String[] args) {
    Scanner in =new Scanner(System.in);
    while(in.hasNext()){
       String str = in.nextLine();
        ArrayList<Integer> list = new ArrayList<Integer>();
        int now ;
        int max =0;
        StringBuffer maxNumBuffer= new StringBuffer();
        StringBuffer nowNumBuffer= null;
        for(int i=0;i<str.length();i++)
        {
            now = 0;
            nowNumBuffer = new StringBuffer();
            //判斷該字元是不是數字
            while (i<str.length() && str.charAt(i)>=48 && str.charAt(i)<=57)
            {
                nowNumBuffer.append(String.valueOf(str.charAt(i)));
                now++;
                i++;
            }
            if(now>0) list.add(now);
            if(now>0 && now>=max){
                maxNumBuffer = nowNumBuffer;
                max = now;
            }
        }
            System.out.println(maxNumBuffer+","+maxNumBuffer.length());


    }
}

}

第二題如下圖:


我自己用的java沒有完全AC,然後借用別人的C++程式碼是可以完全做到的。有時間我再改。先貼出來,大家一起討論。

/*
 * 根據數值佔用BIT數,按順序從輸入位元組流中解析出對應數值,解析順序按輸入陣列astElement索引
 * 升序,比如輸入
 * 3  //代表接下來輸入三個數字
 * OX62 OX80 OX00  //0110 0010  1000 0000 0000 0000
 * 2 //接下來解析出兩個數,就是輸出兩個數
 * 4 // 前面四位,所以第一個輸出為4
 * 5  //之後的五位,所以輸出為00101 所以輸出為6
 * 輸出為 6
 * 5
 * C++程式碼:
 * #include <vector>
#include <string>
#include <iostream>


using namespace std;
int main() {
  int num; cin >> num;
  vector<long long> nums(num);
  for(int i=0; i < num; ++i) {
    cin >> hex >> nums[i];
  }


  int time; cin >> time;
  vector<int> times(time);
  for(int i=0; i < time; ++i) {
    cin >> dec >> times[i];
  }


  vector<bool> bits;
  for(int num: nums) {
    for(int i=0x80; i; i>>=1) {
      bits.push_back(num&i);
    }
  }


  int start = 0;
  long long result = 0;
  for(int time: times) {
    for(int i=0; i<time; ++i) {
      result = 2*result + bits[i+start];
    }
    cout << result << endl;


    result = 0;
    start += time;
  }


  return 0;
}


 */
import java.util.Scanner;


    public class Huawei2 {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){
                int uiIutputLen = Integer.parseInt(sc.nextLine());
                String[] numStrings = sc.nextLine().split(" ");
                StringBuilder sb = new StringBuilder();
                for(int i=0;i<uiIutputLen;i++){
                    int num = (char)Integer.parseInt(numStrings[i].substring(2), 16);
                    int mask = 0x80;
                    while(mask>0){
                        if((num & mask)==mask){
                            sb.append("1");
                        }else {
                            sb.append("0");
                        }
                        mask>>=1;
                    }
                }


                int uiElementNum = Integer.parseInt(sc.nextLine());
                int start = 0;
                for(int i = 0; i<uiElementNum;i++){
                    int uiElementLength = Integer.parseInt(sc.nextLine());




                        System.out.println(Integer.parseInt(sb.substring(start, start+uiElementLength), 2));
                        start += uiElementLength;


                }
            }


        }


}

第三題是一個大整數相乘的形式:

/*
 * 整數相乘,大整數
 * (-12341234)*(43214321)
 * output:-533318047612114
 */
import java.util.Scanner;
public class Huawei3{
    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);
        while(in.hasNext()){
            String st1=in.nextLine();
            String st2=in.nextLine();


            String a  =null;
            String b =null;
            boolean flag = true;
            if(st1.charAt(0)=='-'){
                flag = !flag;
                a = st1.substring(1);
            }
           
            else {
                a = st1;
            }
            if(st2.charAt(0)=='-'){
                flag = !flag;
                b = st2.substring(1);
            }else {
                b = st2;
            }


            String out = multiply(a, b);


            if(flag){
                System.out.println(out);
            }else {
                System.out.println("-"+out);
            }
        }
    }
    public static String multiply(String num1, String num2) {
        int l = num1.length();
        int r = num2.length();
        //用來儲存結果的陣列,可以肯定的是兩數相乘的結果的長度,肯定不會大於兩個數各自長度的和。
        int[] num = new int[l+r];
        //第一個數按位迴圈
        for(int i=0;i<l;i++) {
            //得到最低位的數字
            int n1=num1.charAt(l-1-i)-'0';
            //儲存進位
            int tmp=0;
            //第二個數按位迴圈
            for(int j=0;j<r;j++) {
                int n2=num2.charAt(r-1-j)-'0';
                //拿出此時的結果數組裡存的數+現在計算的結果數+上一個進位數
                tmp=tmp+num[i+j]+n1*n2;
                //得到此時結果位的值
                num[i+j]=tmp%10;
                //此時的進位
                tmp/=10;
            }
            //第一輪結束後,如果有進位,將其放入到更高位
            num[i+r]=tmp;
        }


        int i=l+r-1;
        //計算最終結果值到底是幾位數,
        while(i>0&&num[i]==0){
            i--;
        }
        StringBuilder result=new StringBuilder("");
        //將陣列結果反過來放,符合正常讀的順序,
        //陣列儲存的是:1 2 3 4 5
        //但其表達的是54321,五萬四千三百二十一。
        while(i>=0) {
            result.append(num[i--]);
        }
        String result2=result.toString();


        return result2;
    }

}

網上可以找到題目,我的程式碼也稍微註釋了,歡迎討論!