首页 > 其他分享 >杭电1085

杭电1085

时间:2023-02-06 20:32:16浏览次数:56  
标签:1085 int money 杭电 num c2 c1 he


​这里写链接内容​

Holding Bin-Laden Captive!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13861 Accepted Submission(s): 6230

Problem Description
We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China!
“Oh, God! How terrible! ”

Don’t be so afraid, guys. Although he hides in a cave of Hang Zhou, he dares not to go out. Laden is so bored recent years that he fling himself into some math problems, and he said that if anyone can solve his problem, he will give himself up!
Ha-ha! Obviously, Laden is too proud of his intelligence! But, what is his problem?
“Given some Chinese Coins (硬币) (three kinds– 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins.”
You, super ACMer, should solve the problem easily, and don’t forget to take $25000000 from Bush!

Input

Output
Output the minimum positive value that one cannot pay with given coins, one line for one case.

Sample Input
1 1 3
0 0 0

Sample Output
4

题意:给定三中硬币,面值分别为1,2,5,并且给定三种硬币的数量分别为num_1,num_2,num_5,然后让求解这些硬币所不能组成的最小的面额是多少,例如给定的例子,面值为1的硬币为1枚,面值为2的硬币为1枚,面值为5的硬币为3枚,则可以组成的面值有1,2,3,5……所以,不能组成的最小面额为4。这就是一道母函数的问题,但是这里并不是求组合数,所以,不需要int行的数组来记录组合数,只需要true和false来标示是否能表示该面值就可以了。当然用int记录组合数也可以,但要注意,因为组合量非常大,要注意溢出的情况,我试了一下可以,判断大于0的时候置1就能过。

代码:

# include <iostream>
# include <cstring>
using namespace std;

int main(){

int n,i,j,k,m;
int money[9];//金钱的面值
int c1[10000],c2[10000];

while(cin>>money[1]>>money[2]>>money[5]&&money[1]||money[2]||money[5]){
//利用下标
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));

m = money[1]*1+money[2]*2+money[5]*5;//计算出面值的最大值
for(i=0;i<=money[1];i++){//初始化第一项
c1[i] = 1;
}

for(i=2;i<=5;i+=3){//代表第二项与第三项
for(j=0;j<=m;j++){// m代表最大的面值,
for(k=0;k+j<=m&&k/i<=money[i];k+=i){ //面值k=i*money[i]; k代表2或5面值的和
c2[k+j]+= c1[j];
}
}

for(k=0;k<=m;k++){
c1[k] = c2[k];
c2[k] = 0;
}
}

for(i=1;i<=m;i++){
if(!c1[i]){
break;
}
}

cout<<i<<endl;
}


}

方法2:

# include <iostream>
using namespace std;

int main()
{
int a,b,c;
while(cin>>a>>b>>c)
{
if(a==0&&b==0&c==0)
break;
if(a==0)
printf("1\n");
else if(a+2*b<4)//a>=1,b只能是0
printf("%d\n",a+2*b+1);
else//a!=0&&b1!=0 c不确定
printf("%d\n",a+2*b+5*c+1);
}
return 0;
}
代码3:
# include <iostream>
# include <cstring>
using namespace std;

# define MAX 10000
int c1[MAX],c2[MAX];
int num[4];
int main(){

while(cin>>num[1]>>num[2]>>num[3]&&num[1]||num[2]||num[3]){

int max_ = num[1]*1 + num[2]*2 + num[3]*5;
int i,j,k;
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));

for(i=0;i<=num[1];i++){//对第一项初始化
c1[i] = 1;
}

//这两层for的作用就是第一项的每一个数与第二项的每一数相乘
for(i=0;i<=num[1];i++){//代表第一项
for(j=0;j<=num[2]*2;j+=2){//代表第二项
c2[i+j] += c1[i];// c2[i+j]-->代表取系数 c1[i]-->代表取ax^n中的a,即系数
}
}

/*
for循环中间的条件为什么是 j<=num[1]*1+num[2]*2
上面是第一项与第二项的for循环是 x^a与x^b相加==x^(a+b) 最大值是num[1]*1+num[2]*2
*/
for(j=0;j<=num[1]*1+num[2]*2;j++){

c1[j] = c2[j];
c2[j] = 0;
}



for(i=0;i<=num[1]*1+num[2]*2;i++){
for(j=0;j<=num[3]*5;j+=5){
c2[j+i] += c1[i];
}
}
for(j=0;j<=num[1]*1+num[2]*2+num[3]*5;j++){

c1[j] = c2[j];
c2[j] = 0;
}

for(j=0;j<=max_;j++){
if(c1[j]==0){
break;
}
}

cout<<j<<endl;


}


return 0;
}
Runtime Error(ACCESS_VIOLATION)数组开的太小


标签:1085,int,money,杭电,num,c2,c1,he
From: https://blog.51cto.com/u_15955675/6040463

相关文章

  • 杭电1398
    SquareCoinsProblemDescriptionPeopleinSilverlandusesquarecoins.Notonlytheyhavesquareshapesbutalsotheirvaluesaresquarenumbers.Coinswithva......
  • 杭电2073
    ​​http://acm.hdu.edu.cn/showproblem.php?pid=2073​​无限的路ProblemDescription甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直......
  • 杭电2095(find your present (2))
    ​​这里写链接内容​​>引用块内容ProblemDescriptionInthenewyearparty,everybodywillgeta“specialpresent”.Nowit’syourturntogetyourspecialpre......
  • 整除的为数 杭电2099
    [1.(​​http://acm.hdu.edu.cn/showproblem.php?pid=2099%20%20%E9%97%AE%E9%A2%98%E9%93%BE%E6%8E%A5​​)]​​``这里写代码片​#include......
  • UVA 10859 Placing Lampposts--树形dp
    原题链接:​​http://vjudge.net/problem/UVA-10859​​题意:给一个N个点M条边的无向无环图,就是树的意思,每个节点都可以放灯。每盏灯将照亮以它为一个端点的所有边,在所有边都......
  • 洛谷P1085 [NOIP2004 普及组] 不高兴的津津
    [NOIP2004普及组]不高兴的津津题目描述津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班。另外每周妈妈还会送她去......
  • 洛谷题单【入门2】分支结构-P1085 [NOIP2004 普及组] 不高兴的津津
    题目描述津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班。另外每周妈妈还会送她去学习朗诵、舞蹈和钢琴。但是津津......
  • 2022 年杭电多校第六场 Multiply 2 Divide 2
    2022年杭电多校第六场Multiply2Divide2题意:BXY的序列\(a\)长度为\(n\)\((1\leqn\leq10^5,1\leqa_i\leq10^5)\)对于每个操作,他选择一个数字\(a_i(1\leqi\leqn......
  • 百度之星 初赛 A 杭电6376 度度熊剪纸条
    C度度熊剪纸条链接:​​​点我​​​ProblemDescription度度熊有一张纸条和一把剪刀。纸条上依次写着N个数字,数字只可能是0或者1。度度熊想在纸条上剪K刀(每一刀......
  • 2022 杭电杯(7) I Counting Good Arrays
    本题是2022CCPCF题的强化版.给出一个n,m求出所有长度小于等于n的数列\(a_k(k\len)\)且\(a_k\lem\)且\(a_i|a_{i+1}\)固定n显然可以发现对于m的标准分解\(m=p_1^{k......