廣聯達 2019校園招聘內推筆試-2018.08.30

廣聯達的題沒有測試選項,所以也不知道自己是否AC,但是自己的測試樣例都通過了

1
列出公式 暴力算
2.png
另外給出一個相似題求最小跳躍步數,難度稍微增加的題解,點擊鏈接

//給定非負數組,每個元素代表能跳躍的最大距離
//當前位置在數組首位,判斷是否能跳到數組末尾
#include<iostream>
#include<vector>
using namespace std;

/*
bool isJumpToLast(vector<int> ivec,int n){
    if(n==1)//只有一個元素, 返回true
        return true;
    int i=0;
    while(i<n-1){
        i += ivec[i];
        if(ivec[i]==0 && i!=n-1)//當某一元素爲0,並且它不是最後一個元素時,一定跳不到最後一個下標
            return false;
        if(i >= n-1)
            return true;
    }
}
*/
/*
7
2 3 0 3 2 0 0
上面方法判斷爲false
下面方法判斷爲true
*/
bool isJumpToLast(vector<int> ivec, int n, vector<int> arr){
    int i, j;
    arr[0] = 1;
    for(i=0; i<n; i++){
        if(arr[i]){//如果沒有斷路就繼續
            for(j=i; j<=i+ivec[i]; j++)
                arr[j]=1;
        }else{   //如果出現斷路 就證明根本跳不到
            return false;
        }
    }
    return true;
}
int main()
{
    vector<int> ivec;
    int num, temp;
    cin >> num;
    vector<int> arr(num, 0);//判斷是否斷路
    for(int i=0; i<num; i++){
        cin >> temp;
        ivec.push_back(temp);
    }
    if(isJumpToLast(ivec, num, arr))
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
    return 0;

}

3.png

//判斷二進制中1的個數
#include <iostream>

using namespace std;
int numberOf1(int n){
    int res = 0;
    while(n){
        n &= (n-1);
        res++;
    }
    return res;
}
int main()
{
    int n;
    cin >> n;
    cout << numberOf1(n) << endl;
    return 0;
}

4.png
暫時沒想好 回頭補
5.png

//求數組中最大升序子序列長度
#include <iostream>
#include <vector>
using namespace std;
int maxIS(vector<int> arr, int n){
    int i, j, max=0;
    int dp[n];
    for(i=0; i<n; i++)
        dp[i] = 1;
    for(i=1; i<n; i++){
        for(j=0; j<i; j++){
            if(arr[i]>arr[j] && dp[i]<dp[j]+1)
                dp[i] = dp[j] + 1;
        }
    }
    for(i=0; i<n; i++){
        if(max < dp[i])
            max = dp[i];
    }
    return max;
}
int main()
{
    int temp=0, n=0;
    vector<int> arr;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> temp;
        arr.push_back(temp);
    }
    cout << maxIS(arr, n) << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章