题目,如题,最大时间复杂度O(n)
可求解出最少的张数的组合,还有动态规划方法,后续补充
#include <iostream>
using namespace std;
int main() {
// your code goes here
int input,t,sum=0;
cin>>input;
t=input;
int a[7]={100,50,20,10,5,2,1};
int b[7]={0};
for (int i=0;i<7;i++)
{
b[i]=t/a[i];
t=t-b[i]*a[i];
sum+=b[i];
}
cout<<"least pieces: "<<sum<<endl<<"need : ";
for (int i=0;i<7;i++)
cout<<a[i]<<" yuan "<<b[i]<<" piece "<<endl;
return 0;
}