【笔试记录】2020.8.29 科大讯飞 c++

本文深入探讨了棋盘最大价值问题的动态规划解决方案,以及一种高效的排序算法实现。通过具体实例,详细解析了算法的设计思路与代码实现,为读者提供了一个全面理解算法原理与应用的视角。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

棋盘最大价值

在这里插入图片描述

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;


int main(int argc, const char * argv[]) {
    int nrows,ncols;
    //    cin >> nrows >> ncols;
    string str;
    cin >> str;
    string word = "";
    for(int i=0; i<str.length(); i++){
        if( str[i] == ',' ){
            nrows = stoi(word);
            word = "";
        }else{
            word += str[i];
        }
    }
    ncols = stoi(word);
//    cout << nrows << " " << ncols << endl;

    int map[nrows][ncols];
    for(int i=0; i<nrows; i++){
        for(int j=0; j<ncols; j++){
            cin >> map[i][j];
        }
    }
    //动态规划dp[i][j]表示到达(i,j)位置时能拿到的最大价值,向右或者向下走状态转移dp[i][j] = max(dp[i-1][j], dp[i][j-1])+map[i][j]。注意边界
    int dp[nrows][ncols];
    //初始化
    int sum = 0;
    for(int i=0; i<nrows; i++){
        sum += map[i][0];
        dp[i][0] = sum;
    }
    sum = 0;
    for(int j=0; j<ncols; j++){
        sum += map[0][j];
        dp[0][j] = sum;
    }
    for(int i=1; i<nrows; i++){
        for(int j=1; j<ncols; j++){
            dp[i][j] = map[i][j] + max(dp[i-1][j], dp[i][j-1]);
        }
    }
    cout << dp[nrows-1][ncols-1] << endl;
    return 0;
}

判断排序算法

在这里插入图片描述

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;

int main(int argc, const char * argv[]) {
    int n;
    cin >> n;
    string str;
    cin >> str;
    vector<int> nums;
    string word = "";
    for(int i=0; i<str.length(); i++){
        if( str[i] == ',' ){
            nums.push_back(stoi(word));
            word = "";
        }else{
            word += str[i];
        }
    }
    nums.push_back(stoi(word));
    
    //选择排序
    for(int i=0; i<nums.size()-1; i++){
        int min = i;
        for(int j=i+1; j<nums.size(); j++){
            if( nums[j] < nums[min] ){
                min = j;
            }
        }
        swap(nums[i], nums[min]);
    }
    //output
    for(int i=0; i<n-1; i++){
        cout << nums[i] << ",";
    }
    cout << nums[n-1] << endl;
    return 0;
}

消除多余的下划线(不开辟新空间)

在这里插入图片描述

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;

int main(int argc, const char * argv[]) {
    string str;
    cin >> str;

    //除去开头的下划线
    int start;
    for(start=0; start<str.length(); start++){
        if( str[start] != '_' ){
            break;
        }
    }
    //是否全是下划线
    if( start == str.length() ){
        cout << "" << endl;
        return 0;
    }
    //除去末尾的下划线
    int end;
    for( end=str.length()-1; end >= 0; end-- ){
        if( str[end] != '_' ){
            break;
        }
    }
    //只剩一个字符
    if( start==end ){
        cout << str[start] << endl;
        return 0;
    }
    //除去(start,end)中间的多余的下划线
    cout << str[start];
    bool flag = true;//允许出现一次下划线
    for(int i=start+1; i<end; i++){
        if( str[i] != '_' ){
            cout << str[i];
            flag = true;
        }else{
            if( flag ){
                cout << '_';
                flag = false;
            }
        }
    }
    cout << str[end] << endl;

    return 0;
}

分解质因数

在这里插入图片描述

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;

string result;

void factor(int n){
    for(int i=2; i<=n; ++i){
        if( !(n%i) ){
            result += to_string(i);
            result += "*";
            factor( n/i );
            break;
        }
    }
}

int main(int argc, const char * argv[]) {
    int num;
    cin >> num;
    factor(num);
    if( result[result.size()-1] == '*' ){
        result.erase( result.end() - 1 );
    }
    cout << result << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值