鱼C论坛

 找回密码
 立即注册
查看: 3357|回复: 10

求公约数的题 你能看出错在哪里吗???

[复制链接]
发表于 2012-2-7 11:51:59 | 显示全部楼层 |阅读模式
1鱼币
include<stdio.h>
int CommonFactors(int a,int b)
{
int r;
if(a<0||b<0)
return -1;
for(r=a;r>0;r--)
{
if(a%r==0 && b%r==0)
return r;
}
}
main()
{
int sub;
while((sub=CommonFactors(100,50)>0))
{
static int counter=1;
printf("common factor %d is %d\n",counter++,sub);
}
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-7 11:52:00 | 显示全部楼层
  1. #include<stdio.h>
  2. int CommonFactors(int a,int b)
  3. {
  4.         static int r = 0;
  5.         if(a<0||b<0) return -1;
  6.         do{
  7.                 ++r;
  8.                 if(a%r==0 && b%r==0) return r;
  9.         }while( r<=a );
  10. }
  11. main()
  12. {
  13.         int sub;
  14.         while((sub=CommonFactors(100,50))>0)
  15.         {
  16.                 static int counter=1;
  17.                 printf("common factor %d is %d\n",counter++,sub);
  18.         }
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-2-7 14:27:20 | 显示全部楼层
逻辑太混乱了,首先你子函数里的if语句后面需要一个else,不然都输出后面for语句无论如何都会执行的。

看看我根据你的算法改写的:
#include <stdio.h>

void commonFactors(int bigNum,int smallNum) //不设置返回值,直接在子函数里打印
{
        int k,i=0;   //变量k用来置换两个参数和控制循环,i作为计数,统计公约数的个数
        if (bigNum < 0 || smallNum < 0)
                printf(" 参数错误!");  //打印错误信息
        else
         if (bigNum < smallNum)  //如果前面的数小,则置换下
         {       
                k = smallNum;
                smallNum = bigNum;
                bigNum = k;
         }       
        for (k = smallNum;k>0;k--)
        {
                if (smallNum % k == 0 && bigNum % k ==0)
                {       
                        i++;
                        printf("Common factor %d is %d. \n", i, k);
                }
        }

         

}

int main()
{
        commonFactors(100,50);       

        return 0;
}

该程序在ubuntu下gcc编译通过。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-2-7 16:18:51 | 显示全部楼层
你的函数只能返回最大公约数
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-2-7 20:02:25 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-2-7 20:09:35 | 显示全部楼层

可是如果是要第一次调用 返回最大公约数 以后只要再使用相同参数调用 每次返回下一个小一些的公约数 那怎么改呢{:1_1:}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-2-7 20:18:38 | 显示全部楼层
Cabernet 发表于 2012-2-7 14:27
逻辑太混乱了,首先你子函数里的if语句后面需要一个else,不然都输出后面for语句无论如何都会执行的。

看 ...

亲 不能改动主函数哦 再试试{:1_1:}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-2-7 21:32:00 | 显示全部楼层
  1. #include<stdio.h>
  2. int CommonFactors(int a,int b)
  3. {
  4.         static int r = 0;
  5.         if(a<0||b<0||(a-r==1)) return -1;
  6.         do{
  7.                 ++r;
  8.                 if(a%(a-r)==0 && b%(a-r)==0) return a-r;
  9.         }while( r<a-1 );
  10. }
  11. main()
  12. {
  13.         int sub;
  14.         while((sub=CommonFactors(100,50))>0)
  15.         {
  16.                 static int counter=1;
  17.                 printf("common factor %d is %d\n",counter++,sub);
  18.         }
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-2-13 21:28:34 | 显示全部楼层

:hug:   谢谢……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-2-27 02:06:45 | 显示全部楼层
lz要注意编程的书写规范喔
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-11 17:07:37 | 显示全部楼层
来站坐
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-13 08:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表