zoukankan      html  css  js  c++  java
  • 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 }



  • 相关阅读:
    数据库的安装
    数据库大整合
    数据库的设置及其初始密码
    HTML标签
    增加删除的js
    增删改查js
    表单验证码限制条件
    倒序输出插入的数组
    插入数组排序法1
    求下标长度
  • 原文地址:https://www.cnblogs.com/yayagamer/p/2305866.html
Copyright © 2011-2022 走看看