返回   cpper编程论坛 > Zion/测试
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

Zion/测试 惹人烦的东西这边来

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2005-06-27
up2009 的头像
普通会员
 
注册日期: 2005-05-11
帖子: 37
up2009 正向着好的方向发展
默认 初学者问题之七:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
/[highlight=red:eb294d9f30]*3.45整数x和y的最大公约数是x和y能够整除的最大整数。编写一个递归函数gcd,
返回整数x和y的最大公约数。整数x和y的最大公约数的递归定义如下:如果y等于0,
则gcd(x,y)为x,否则gcd(x,y)为gcd(y,x%y),其中%是求模运算符。[/highlight:eb294d9f30]

*/
int gcd(int,int);
int main()
{
int x,y;
cout<<"x=";
cin>>x;
cout<<endl<<"y=";
cin>>y;
cout<<endl<<"最大公约数为:"<<gcd(x,y)<<endl;
char quit;
cout<<"quit?yes or no";
cin>>quit;
if(quit='q')
return 0;
}
int gcd(int q,int p)
{
for(int i=q;i>=1;i--)
if(q%i==0&&p%i==0)
return i;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2005-06-27
up2009 的头像
普通会员
 
注册日期: 2005-05-11
帖子: 37
up2009 正向着好的方向发展
默认

最大公约数是求出来了,我总觉得他题目的意思不是这么做的;
请教一下该如何做?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2005-06-27
yumagi 的头像
高级会员
 
注册日期: 2003-06-09
住址: 上海
帖子: 314
yumagi 正向着好的方向发展
发送 MSN 消息给 yumagi
默认

递归的意思是如果满足某种条件,就再次调用函数本身;否则就返回结果。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2005-06-27
up2009 的头像
普通会员
 
注册日期: 2005-05-11
帖子: 37
up2009 正向着好的方向发展
默认

那就是切题了,是吧!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2005-06-27
wqqafnd 的头像
高级会员
 
注册日期: 2004-10-08
帖子: 196
文章: 1
wqqafnd 正向着好的方向发展
发送 MSN 消息给 wqqafnd
默认

引用:
整数x和y的最大公约数是x和y能够整除的最大整数。编写一个递归函数gcd, 返回整数x和y的最大公约数。整数x和y的最大公约数的递归定义如下:如果y等于0, 则gcd(x,y)为x,否则gcd(x,y)为gcd(y,x%y),其中%是求模运算符。
其实这里已经把算法说出来了。照书的方法,我想应该是这样:
代码:
int gcd(int x,int y){ if(!y) return x; return gcd(y,x%y); }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #6 (permalink)  
旧 2005-06-27
cat cat 当前离线
高级会员
 
注册日期: 2003-11-06
帖子: 1,563
文章: 6
cat 正向着好的方向发展
默认

引用:
欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。其计算原理依赖于下面的定理:

定理:gcd(a,b) = gcd(b,a mod b)
gcd(a, b) = gcd(b, a mod b) if a mod b != 0
= b if a mod b == 0
根据这个写吧
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #7 (permalink)  
旧 2005-06-27
up2009 的头像
普通会员
 
注册日期: 2005-05-11
帖子: 37
up2009 正向着好的方向发展
默认

谢了;
确实是说出来了,仔细体会一下,挺巧妙的;
不过,我的方法也能解决问题,为什么要弄得那么复杂呢。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #8 (permalink)  
旧 2005-06-27
Elminster 的头像
超级版主
 
注册日期: 2002-09-09
帖子: 1,764
Elminster 正向着好的方向发展
默认

引用:
作者: up2009
谢了;
确实是说出来了,仔细体会一下,挺巧妙的;
不过,我的方法也能解决问题,为什么要弄得那么复杂呢。
因为要减小运行时间。同样大的两个数字求公约数,你的方法比这个更巧妙的方法需要更多的运行时间,而且是多得多。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #9 (permalink)  
旧 2005-06-28
yumagi 的头像
高级会员
 
注册日期: 2003-06-09
住址: 上海
帖子: 314
yumagi 正向着好的方向发展
发送 MSN 消息给 yumagi
默认

要不要再来一次求GCD的时间复杂度
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #10 (permalink)  
旧 2005-06-28
up2009 的头像
普通会员
 
注册日期: 2005-05-11
帖子: 37
up2009 正向着好的方向发展
默认

呵呵,明白了;
谢谢大家!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签
主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 02:30 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0