1 //O(n)时间复杂度的找零钱问题 2 #include <iostream> 3 #include <bits/stdc++.h> 4 using namespace std; 5 int a[6]; 6 int main() 7 { 8 9 int n; 10 while(~scanf("%d",&n)) 11 { 12 memset(a,0,sizeof(a)); 13 for(int i=1;i<=n;i++) 14 { 15 if(i>=1) 16 { 17 a[i%6]=a[((i-1)%6+6)%6]+1; 18 } 19 if(i>=3) 20 { 21 a[i%6]=min(a[i%6],a[((i-3)%6+6)%6]+1); 22 } 23 if(i>=5) 24 { 25 a[i%6]=min(a[i%6],a[((i-5)%6+6)%6]+1); 26 } 27 } 28 printf("%d ",a[n%6]); 29 } 30 return 0; 31 }