#12505: 請問最後兩個測姿勢甚麼?


0822_137934 (14306)


#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
int c,n,sum;
while(cin>>c>>n)
{int a[n];sum=0;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
n--;
while(c>0)
{sum+=c/a[n];
c%=a[n];
n--;}
cout<<sum<<endl;}
return 0;
}

 

 

 

請問哪裡錯?

#13204: Re:請問最後兩個測姿勢甚麼?


mmi366127 (unknown)


#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
int c,n,sum;
while(cin>>c>>n)
{int a[n];sum=0;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
n--;
while(c>0)
{sum+=c/a[n];
c%=a[n];
n--;}
cout<<sum<<endl;}
return 0;
}

 

 

 

請問哪裡錯?

這題不能用greedy

for example :

47 4

25

18

5

1

最小解應該是 5 -> 18+18+5+5+1

你的程式會得到 25 + 18 + 1 + 1 + 1 + 1 -> 6