


求大神看一下哪里有問題呢?謝謝
uj5u.com熱心網友回復:
建議樓主別貼圖片,貼代碼更方便。供參考:#include<iostream.h>
#include<algorithm.h>
using namespace std;
typedef struct{
int T; //面值
int C; //硬幣個數
}Node;
bool compare(Node &a,Node &b)
{
return a.T < b.T;
}
int seek(Node *p,int n,int m)
{
int q=m;
int t;
int sum=0;
int i;
for(i=n-1;i>=0;i--) //for(i=n;i>=0;i--)陣列下標越界
{
t=(q/p[i].T);
if(t>p[i].C) t=p[i].C;//不能超過所持有的當前面值的硬幣個數
q-=t*p[i].T;
sum += t;
if(q==0) break;
}
if(q==0)
{
return sum;
}else{
return -1;
}
}
int main()
{
int n;
cin>>n;
//Node a[n];
Node *a =new Node[n];
int i;
for(i=0;i<n;i++)
{
cin>>a[i].T>>a[i].C;
}
int m;
cin>>m;
sort(a,a+n,compare);
int p=seek(a,n,m);
if(p>0)
{
cout<<p;
}else{
cout<<"-1";
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/275580.html
標籤:其它技術問題
