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

📄 2635368_ce.cpp

📁 北大大牛代码 1240道题的原代码 超级权威
💻 CPP
字号:
#include <iostream.h> 
#include <math.h> 
using namespace std;
int main() 
{ 
  int firsttime = 1; 

  int G, L; 
  while (cin >> G >> L) 
    { 
      if (!firsttime) cout << "\n"; 
      firsttime = 0; 
      double exact = (double)G/L; 
      double bestapprox = floor(exact + 0.5); 
      cout << bestapprox << "/1\n"; 

      for (unsigned i = 2; i <= L; ++i) 
        { 
          int estG = exact * i + 0.5; 
          double approx = (double)estG / i; 
          if (fabs(approx-exact) < fabs(bestapprox-exact)) 
            { 
              bestapprox = approx; 
              cout << estG << "/" <<  i << "\n"; 
              if (bestapprox == exact) 
                break; 
            } 
        } 
    } 
  return 0; 
} 

⌨️ 快捷键说明

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