惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Engineering at Meta
Engineering at Meta
腾讯CDC
宝玉的分享
宝玉的分享
H
Help Net Security
I
InfoQ
博客园 - Franky
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
博客园_首页
美团技术团队
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
The Cloudflare Blog
博客园 - 司徒正美
Vercel News
Vercel News
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
月光博客
月光博客

博客园 - 天生舞男

多个平均数列的子查询 迁移sharepoint Sharepoint迁移数据库之绝版(只要照着做,一定能够迁移成功) Sharepoint的webpart 虚函数和非虚函数的调用基本原则 The visio to drawing project chart. - 天生舞男 Using MSChart from a C# WinForm About the deeply copy 学习程序的自我建议---从实际软件的源代码开始学习 MS bug "The connection pool" in Oracle 10g and the data sort according to specified filed on DataGrid control. How to get response content with specified post data by post method Post method tip automation服务器不能创建对象 rose破解 ArrayList按Index删除的问题 Recently expection RadioButtonList中选择框和文字对不齐的解决办法 resolve problem best method is find document from official document 看了宝玉的作品,心理甚是感叹
Get the biggest common divisor
天生舞男 · 2006-02-21 · via 博客园 - 天生舞男

// 我们可以从两个数的比较小的那个开始,看看时候可以同时整除这两个数,如果可以,那么就是结果。
  // 如果不行,我们就把它减一,再试。一直到减到1,这时一定是可以整除的。
  /*
  public static int GetBigestCommonDivisor(int x, int y)
  {
   int nresult;
   if (x > y)
    nresult = y;
   else
    nresult = x;
   for (;nresult > 1;nresult --)
   {
    if ((x % nresult) == 0 && (y % nresult) == 0)
     break;
   }
   return nresult;
  }
  */

  // 首先我们假设结果是两个数中小的那一个是结果,用它去除比较大的数,如果余数是0,
  // 那么它就是结果。如果不是0,那么我们就把这个余数假设为结果,把原来小的那个数作为验证的被除数,再进行验证。直到找到结果。
  public static int GetBigestCommonDivisor(int x, int y)
  {
   int ntemp;
   // Ensure x > y
   if (x < y)
   {
    ntemp = x;
    x = y;
    y = temp;
   } 

   while (y > 1)
   {
    temp = x % y;
    if (ntemp == 0)
     break;
    x = y;
    y = ntemp;
   }
   return y;
  }