Print the numbers of form (2^i)*(5^j) in increasing order

Print the numbers of form (2^i)*(5^j) in increasing order, e.g. 1, 2, 4, 5, 8, 10, 16, 20, …

Q:题目很简单了,就是打印整数序列,该序列的生成规则为(2^i)*(5^j)

 

View Code
 1 void Compute_Increasing_Order() 
2 {
3 int arr[20] = {0};
4 arr[0]= 1; printf("%d, ", arr[0]);
5 int count_2 = 0;
6 int count_5 = 0;
7 for(int i = 1; i <= 10 ; i++)
8 {
9 // 将(2^i)*(5^j)转化为2*arr[count_2] 或者 5*arr[count_5]
10 if(2 * arr[count_2] <= 5 * arr[count_5])
11 {
12 arr[i] = 2 * arr[count_2];
13 count_2 ++;
14
15 // 该句是保证2 * arr[count_2] == 5 * arr[count_5] 相等时,
16 // count_2 and count_5都必须+1,否则,会出现重复
17 if (2 * arr[count_2] == 5 * arr[count_5]) count_5++;
18 }
19 else
20 {
21 arr[i] = 5 * arr[count_5];
22 count_5 ++;
23 }
24
25 printf("%d, ", arr[i]);
26 }
27
28 cout << endl;
29 }



posted @ 2011-12-29 11:52  百分百好牛  阅读(217)  评论(0编辑  收藏  举报