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;
}

1878

被折叠的 条评论
为什么被折叠?



