首页 技术 正文
技术 2022年11月14日
0 收藏 358 点赞 3,714 浏览 2054 个字

选课时间(题目已修改,注意读题)

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5115    Accepted Submission(s): 3944

Problem Description又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合。你来帮帮他吧。(xhd认为一样学分的课没区别) Input输入数据的第一行是一个数据T,表示有T组数据。
每组数据的第一行是两个整数n(1 <= n <= 40),k(1 <= k <= 8)。
接着有k行,每行有两个整数a(1 <= a <= 8),b(1 <= b <= 10),表示学分为a的课有b门。 Output对于每组输入数据,输出一个整数,表示学n个学分的组合数。 Sample Input22 21 22 140 81 12 23 24 25 86 97 68 8Sample Output2445 题意  中文题,不说了题解   这题我们设一个dp[ i ]数组( i 表示学分数,dp[i]就是组成i学分的组合数),枚举每个课程,求出包括前面课程所能组成的学分,对数组进行更新0.0……说的自己都不明白—_—!!!。AC代码

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 100005
#define maxm 50000
using namespace std;
typedef long long ll;
int dp[];
struct course
{
int s;
int n;
}f[];
int main(int argc, char const *argv[])
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
memset(dp,,sizeof(dp));
for(int i=;i<=m;i++)
{
cin>>f[i].s>>f[i].n;
}
dp[]=;
for(int i=;i<=m;i++) //枚举课程
{
for(int j=n;j>=f[i].s;j--) //枚举价值,要从大到小推!
{
for(int k=;k<=f[i].n;k++) ////枚举这个物品的个数,如果当前枚举到的价值能放入此物品的话,就加上之前的价值
{
if(j>=k*f[i].s)
dp[j]+=dp[j-k*f[i].s]; //更新数组
else
break;
}
}
}
cout<<dp[n]<<endl;
}
return ;
}

另一种 从小到大推,别人写的,摘自http://www.lai18.com/content/2463208.html

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#define maxn 100005
#define maxm 50000
using namespace std;
typedef long long ll;
int main()
{
int T, n, num, a[], b[], c1[], c2[];
//由于1<=num<=8,所以a,b开到10;由于1<=n<=40,所以c1,c2开到41
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &num);
for (int i = ;i <= num;i++)
scanf("%d%d", &a[i], &b[i]);
memset(c1, , sizeof(c1));
memset(c2, , sizeof(c2));
c1[] = ;//由于本题对每个a[i]有个数限制,所以不能使数组c1初始化1【那样相当于默认学分为1的课程无限个】
//但是可以在相乘多项式前多乘一个1,所以赋值c1[0] = 1,同时下面是从i=1开始的
for (int i = ;i <= num;i++)
{//i表示乘到了第几项
for (int j = ;j <= n;j++)
{
for (int k = ;k + j <= n&&k <= b[i]*a[i];k += a[i])//a[i]是第i个多项式的单个课程学分
c2[k + j] += c1[j];
}
memcpy(c1, c2, sizeof(c1));//c2赋给c1
memset(c2, , sizeof(c2));//c2清零
}
printf("%d\n", c1[n]);
}
return ;
}

还可以用母函数写 可是我不会母函数 有时间去研究研究~

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,739
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,130
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293