首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

一段求最大公约数和最小公倍数的代码,求改错解决方法

2012-04-17 
一段求最大公约数和最小公倍数的代码,求改错C/C++ code#include stdafx.hint _tmain(int argc, _TCHAR*

一段求最大公约数和最小公倍数的代码,求改错

C/C++ code
#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){    int m,n,temp,p,r;    printf("输入两个数字m,n:\n");    scanf("%d,%d",&m,&n);    if(m>n)    {        temp=m;        m=n;        n=temp;    }    p=n*m;    while(m!=0)    {        r=n%m;    }    printf("m和n的最大公约数为: %d\n",r);    printf("m和n的最小公倍数为: %d\n",p/r);    return 0;}


[解决办法]
while(m!=0)
{
r=n%m;
}

若m不等于0,则持续计算r=n%m
死循环

建议楼主学会单步调试——说是这么说,我真不能理解为什么有人不会
[解决办法]
C/C++ code
// 1016.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){    int m, n;     int m_cup, n_cup, res; /*被除数, 除数, 余数*/     printf("Enter two integer:\n");     scanf("%d %d", &m, &n);     if (m > 0 && n >0)     {         m_cup = m;         n_cup = n;         res = m_cup % n_cup;         while (res != 0)         {              m_cup = n_cup;             n_cup = res;             res = m_cup % n_cup;         }         printf("Greatest common divisor: %d\n", n_cup);         printf("Lease common multiple : %d\n", m * n / n_cup);     }     else printf("Error!\n");     return 0; } 

热点排行