⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex0419.cpp

📁 practice c++, it is from the book http://www.amazon.com/Schaums-Outline-Programming-John-Hubbard
💻 CPP
字号:
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Example 4.19 on page 70
//  Testing a loop invariant

#include <cmath>     // defines pow() and log()
#include <iostream>  // defines cin and cout
#include <iomanip>   // defines setw()
using namespace std;

int main()
{ // computes the discrete binary logarithm of an input number:
  long n;
  cout << "Enter a positive integer: ";
  cin >> n;
  int d=0;  // the discrete binary logarithm of n
  double p2d=1;  // = 2^d
  for (int i=n; i > 1; i /= 2, d++)
  { // INVARIANT: 2^d <= n/i < 2*2^d
    p2d=pow(2,d);  // = 2^d
    cout << setw(2) << p2d << " <= " << setw(2) << n/i
         << " < " << setw(2) << 2*p2d << endl;
  }
  p2d=pow(2,d);  // = 2^d
  cout << setw(2) << p2d << " <= " << setw(2) << n
       << " < " << setw(2) << 2*p2d << endl;
  cout << "  The discrete binary logarithm of " << n
       << " is " << d << endl;
  double lgn = log(n)/log(2);  // base 2 logarithm of n
  cout << "The continuous binary logarithm of " << n
       << " is " << lgn << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -