洛谷c语言

P1044 [NOIP 2003 普及组] 栈

#include<stdio.h>
long long fac(int n){
    if(n == 1) return 1;
    else return fac(n-1) * (4*n-2) / (n+1);
}
int main(){
    int n = 0;
    scanf("%d",&n);
    long long result = fac(n);
    printf("%lld",result);
    return 0;
}

数据结构里面有个科特兰数,然后找到公式后,直接用阶乘去写,只能通过三,俩个溢出,然后找到递推公式

最后编写递归代码

P1208 [USACO1.3] 混合牛奶 Mixing Milk

#include<stdio.h>
#include<stdlib.h>
struct Node{
    int price;
    int amount;
};
int compare(const void *a,const void *b){
    return((struct Node*)a)->price -((struct Node*)b)->price;
}
int main(){
    int n = 0,m = 0;
    if(scanf("%d %d",&n,&m) != 2) return 0;
    if(n==0){
        printf("0\n");
        return 0;
    }
    struct Node farmers[5005];
    for(int i = 0; i < m; i++){
        scanf("%d %d",&farmers[i].price,&farmers[i].amount);
    }
    //贪心排序,单价排序
    qsort(farmers,m,sizeof(struct Node),compare);
    long long sum = 0;
    int currentNeed = n;
    for(int i = 0; i < m; i++){
        if(farmers[i].amount >= currentNeed){
            sum += (long long)currentNeed * farmers[i].price;
            currentNeed = 0;
            break;
        }else{
            sum += (long long)farmers[i].amount * farmers[i].price;
            currentNeed -= farmers[i].amount;
        }
    }
    printf("%lld\n",sum);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值