广联达 2019校园招聘内推笔试-8.30

版权声明:本人ZZU在校学生,文章均为个人心得,有不足之处请不吝赐教! https://blog.csdn.net/whl_program/article/details/82431330

广联达的题没有测试选项,所以也不知道自己是否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;
}

猜你喜欢

转载自blog.csdn.net/whl_program/article/details/82431330