Sunday, April 5, 2020

POJ.1338 Ugly Numbers

1.Problem
http://poj.org/problem?id=1338

2.Idea
Using 3 variables

3.Source
 ll ans[1502];  
 int main()  
 {  
      ans[1] = 1;  
      int u2 = 1, u3 = 1, u5 = 1;  
      for (int i = 2; i <= 1500; i++) {  
           int value2 = ans[u2] * 2;  
           int value3 = ans[u3] * 3;  
           int value5 = ans[u5] * 5;  
           ans[i] = min(min(value2, value3), value5);  
           if (ans[i] == value2) u2++;  
           if (ans[i] == value3) u3++;  
           if (ans[i] == value5) u5++;  
      }  
      int n;  
      while ((cin >> n), n)  
      {  
           cout << ans[n] << endl;  
      }  
      return 0;  
 }  

No comments:

Post a Comment