AlgoPTA


A神经典电音之作《Waiting for Love》,歌词温暖励志,激励有梦想的人

这家伙没对象啊,哪个小姐姐行行好把他领走啊😏
@Li Sihan qq:1809606186

6-1 旅途加油
int Fill(int stations[], int n, int k){
    
    int current = n;
    int count = 0;
    for (int i =0; i <= k; ++i) {

        if (current < stations[i]) {
            current = n;
            count++;
        }
        
         current -= stations[i];
        
    }
    if (current >= 0){
        return count;
    } else{ return 0; }

}
6-2 杨辉三角-递归
int Yang(int i, int j) {
    if (j ==1 || j == i) {
        return 1;
    } else {
        return Yang(i-1, j-1) + Yang(i-1, j);
    }
}
6-3求编辑距离问题(动态规划)
void solve() {
    //初始化第一行和第一列
    for (int i=0; i <= a.length(); i++) {
        dp[i][0] = i;
    }
    for (int j=0; j<=b.length(); j++) {
        dp[0][j] = j;
    }

    //填充数组
    for (int i = 1; i <= a.length(); i++) {
        for (int j = 1; j <= b.length(); j++) {
            if (a[i-1] == b[j-1]) {
                dp[i][j] = dp[i-1][j-1];
            } else {
                dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
            }
        }
    }
}
6-4电影安排
// sort 排序  贪心算法
int Schedule(TIME a[], int n) {
    //按结束时间升序排序
    sort(a, a+n, cmp);

    int count = 1; //第一场肯定能看
    int endTime = a[0].end_time;

    for (int i = 1; i < n; i++) {
        if (a[i].start_time >= endTime) {
            count++;
            endTime = a[i].end_time;
        }
    }
    return count;
}
6-5 0/1背包问题 (优先队列式分支限界)
void bound(NodeType &e) {
    int j;

    e.ub = e.v;

    for(j = e.i+1; j < n; j++){
        if (e.w+w[j] <= W){
            e.ub += v[j];
        }
    }
}

void EnQueue(NodeType e,priority_queue<NodeType> &qu) {
    if(e.w <= W && e.v > maxv){
        maxv = e.v;
        bestx = e.x;
    }
    if(e.ub > maxv){
        qu.push(e);
    }
}

void bfs() {
    NodeType temp;
    priority_queue<NodeType> qu;

    temp.i = -1;
    temp.w = 0;
    temp.v = 0;
    temp.ub = 0;
    for(int i = 0; i<n; i++) {
        temp.x.push_back(0);
    }
    bound(temp);
    EnQueue(temp,qu);

    while(!qu.empty()){
        temp = qu.top();
        qu.pop();
        if(temp.i+1 < n){
            NodeType e1, e2;
            e1 = temp; e1.i++;
            e1.w += w[e1.i];
            e1.v += v[e1.i];
            e1.x[e1.i] = 1;
            bound(e1);
            EnQueue(e1,qu);

            e2 = temp; e2.i++;
            e2.x[e2.i] = 0;
            bound(e2);
            EnQueue(e2,qu);
        }
    }
}

下面这些题注意用的哪种编程语言

7-1 全排列(分治)
def swap(list, i, j):
    temp = list[i]
    list[i] = list[j]
    list[j] = temp

def permute(list, l, r):
    if l == r:
        for i in range(len(list)):
            print(list[i], end=" ")
        print()
    else:
        for i in range(l, r+1):
            swap(list, l, i)
            permute(list, l+1, r)
            swap(list, l, i)
m = int(input())
list = [i for i in range(1, m+1)]
n = len(list)
permute(list, 0, n-1)

7-2 第k小元素
#include<iostream>
#include<queue>

using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    priority_queue<int,vector<int>,greater<int>> q;
    while(n--)
    {
        int tmp = 0;
        cin >> tmp;
        q.push(tmp);
    }
    while(--k)
    {
        q.pop();
    }
    cout << q.top()<< endl;
    return 0;
}
7-3 二分搜索
#include <stdio.h>

int binary_search(int arr[], int n, int key){
    int low = 0;
    int high = n-1;

    while (low <= high){
        int mid = (low+high)/2;

        if (arr[mid] == key) {
            return mid;
        } else if (arr[mid] < key) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return -1;
}

int main() {
    int n;
    scanf("%d",&n);

    int arr[n];
    for (int i=0; i<n; i++) {
        scanf("%d", &arr[i]);
    }

    int key;
    scanf("%d", &key);

    int result = binary_search(arr, n, key);
    printf("%d\n", result);

    return 0;
}
7-4 快速幂运算
def last_three_digits(x, y):
    result = pow(x, y, 1000) 
    return result
x, y = map(int , input().split())

result = last_three_digits(x,y)
print(result)
7-5 加勒比海盗船-最优装载问题
#include <stdio.h>
#include <stdlib.h>

//比较函数,排序
int compare(const void *a, const void *b){
    return (*(int*)a-*(int*)b);
}
//计算最大数量的古董
int maxAntiques(int weights[], int n, int c){
    qsort(weights, n, sizeof(int), compare); //堆古董重量进行排序
    int count = 0;
    int totalWeight = 0;
    for (int i=0; i<n;i++){
        if(totalWeight + weights[i] <= c){
            totalWeight += weights[i];
            count++;
        } else{
            break;  //载重量达到上限,停止
        }
    }
    return count;
}
int main(){
    int T;
    scanf("%d", &T);
    while (T--) {
        int c,n;
        scanf("%d %d", &c, &n);
        int weights[n];
        for (int i=0;i<n;i++){
            scanf("%d",&weights[i]);
        }
        int result = maxAntiques(weights,n,c);
        printf("%d\n",result);
    }
    return 0;
}
7-6 最小生成树-prim
#include <bits/stdc++.h>
using namespace std;

const int MAX_NODES = 1e4+10;
const int MAX_EDGES = 2e6 + 10;

struct Edge{
    int to, weight;
    bool operator < (const Edge &other)
        const{
        return weight > other.weight;
    }
};

vector<Edge> adjList[MAX_NODES];  // 邻接表存储每个节点的边
bool visited[MAX_NODES];        //记录节点是否被访问
int minEdgeWeight[MAX_NODES];   //存储到达每个节点的最小边权值
int n,m;                       //节点数/边数

//prime
void prim(){
    memset(minEdgeWeight, 0x3f, sizeof(minEdgeWeight));  //初始化边权值为极大值
    minEdgeWeight[1] = 0;
    int totalWeight = 0;
    priority_queue<Edge> pq;
    pq.push({1,0});

    while (!pq.empty()){
        int currentNode=pq.top().to;
        pq.pop();
        if(visited[currentNode]) continue;
        visited[currentNode] = true;
        totalWeight += minEdgeWeight[currentNode];
        for(auto&edge : adjList[currentNode]){
            int nextNode = edge.to, weight=edge.weight;
            if(minEdgeWeight[nextNode]>weight){
                minEdgeWeight[nextNode]=weight;
                pq.push({nextNode, weight});
            }
        }
    }
    printf("%d",totalWeight);
}

int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);

        adjList[u].push_back({v,w});
        adjList[v].push_back({u,w});
    }
    prim();
    return 0;
}
7-7 最大字段和问题
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);

    int arr[n];
    for (int i=0; i<n; i++) {
        scanf("%d", &arr[i]);
    }
    int maxSum = 0, currentSum = 0;
    int start = 0, end = 0, tempStart= 0;

    for (int i=0; i<n; i++){
        currentSum += arr[i];

        if (currentSum < 0) {
            currentSum = 0;
            tempStart = i+1;
        }

        if (currentSum > maxSum) {
            maxSum = currentSum;
            start = tempStart;
            end = i;
        }
    }
    printf("%d\n", maxSum);
    printf("%d %d\n", start + 1, end+1);

    return 0;
}
7-8最长子序列
# include <stdio.h>

int max(int a, int b) {
    return (a>b) ? a : b;
}

int main() {
    int n;
    scanf("%d", &n);

    int arr[n];
    for (int i=0; i<n; i++){
        scanf ("%d", &arr[i]);
    }

    int lis[n];
    for (int i=0; i<n; i++){
        lis[i] = 1;
    }

    for (int i=1; i<n; i++){
        for(int j=0; j<i; j++){
            if (arr[i] > arr[j] && lis[i] < lis[j] + 1){
                lis[i] = lis[j] + 1;
            }
        }
    }
    int maxLength = 0;
    for (int i=0; i<n; i++){
        maxLength = max(maxLength, lis[i]);
    }
    printf("%d\n", maxLength);

    return 0;
}
7-9 子集和问题
#include <stdio.h>
#include <stdbool.h>

int n,W;
int w[1000];
bool chosen[1000];

void search(int pos, int currentSum){
    if(currentSum == W){
        //找到一个和为W的子集,打印它
        for (int i=0;i<pos;i++){
            if(chosen[i]){
                printf("%d ",w[i]);
            }
        }
        printf("\n");
    } else if(pos < n && currentSum < W){
        //考虑当前位置的元素
        //选择当前元素 
        chosen[pos] = true;
        search(pos+1,currentSum+w[pos]);
        //不选择当前元素 
        chosen[pos] = false;
        search(pos+1, currentSum);
    }
}
int main(){
    scanf("%d %d",&n,&W);
    for(int i=0;i<n;i++){
        scanf("%d",&w[i]);
    }
    search(0,0);
    return 0;
}
7-10 N皇后问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int count = 0;
int n;
int pos[20];  //记录每一行皇后列位置
//检查在第row行第col列放置皇后是否合法
int isSafe(int row, int col){
    for (int i=0; i<row; i++){
        //检查列和对角线上是否已有皇后
        if (pos[i] == col || pos[i]-i == col-row || pos[i]+i == col + row){
            return 0;
        }
    }
    return 1;
}
//放置皇后 
void placeQueens(int row){
    if(row == n){
        count++; //找到一个解
    } else {
        for (int col = 0; col < n; col++){
            if (isSafe(row,col)){
                pos[row] = col;
                placeQueens(row+1);
            }
        }
    }
}

int main(){
    scanf("%d",&n);
    memset(pos, -1, sizeof(pos));
    placeQueens(0);
    printf("%d\n",count);
    return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值