| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 497 人关注过本帖
标题:大家帮我看一下这个m定义的问题。。。
只看楼主 加入收藏
Areik
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2009-9-28
结帖率:87.5%
收藏
 问题点数:0 回复次数:3 
大家帮我看一下这个m定义的问题。。。
要求写一个求m,n最小公倍数的程序,m,m的最小公倍数为m*n除以他们的最大公约数。我写的程序如下,但在用visual2005执行时总会在启用时提示我没有定义m。。。我改如何改正?



#include <stdio.h>

int main()
{
   int m,n,a=1,r;
   
   
   printf("Enter a number:");
   scanf("%d",m);
   printf("Enter a number:");
   scanf("%d",n);
   for(m%a==0&&n%a==0;a<=m&&a<=n;a++)
   {
       if(m<=0||n<=0)
        {
          printf("The value are wrong!");
        }
   
       else r=(m*n/a);

   }
   printf("%d",r);
}
搜索更多相关主题的帖子: 定义 
2009-11-12 02:37
zcwind
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-10-16
收藏
得分:0 
your code has one syntax error in the scanf function. you forget '&', before m and n.
in addition, you have to initialize m,n. this means you have to give a value to m, n when you created these two variables.

#include <stdio.h>

int main()
{
   int m = 0,n = 0,b=0;
      
   printf("Enter a number:");
   scanf(" %d",&m);
   printf("Enter a number:");
   scanf(" %d",&n);

   if (m > n)
       b = m;
   else
       b = n;

   while( (m%b != 0 || n%b !=0) && b > 1)
       b--;

   printf("%d",m*n/b);
}
you can refere to my code inorder to find 最小公倍数
2009-11-12 08:20
yangxiaochao
Rank: 2
等 级:论坛游民
帖 子:34
专家分:34
注 册:2009-10-9
收藏
得分:0 
这是我帮你改后的
#include <stdio.h>

int main()
{
   int m,n,a=1,r,t;
   
   
   printf("Enter a number:");
   scanf("%d",&m);//这里,你没有加&
   printf("Enter a number:");
   scanf("%d",&n);
   for(;a<=m&&a<=n;a++)
   {
       if(m<=0||n<=0)
        {
          printf("The value are wrong!");
        }
       if(m%a==0&&n%a==0)//还有这里,你不能一直用a,你要将他保存到另一个数上,不然你到最后a肯定是等于m或n的其中一个数
           t=a;
   }
        r=(m*n/t);

   
   printf("%d",r);
}
2009-11-12 09:00
Areik
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2009-9-28
收藏
得分:0 
非常感谢~~~
2009-11-12 18:11
快速回复:大家帮我看一下这个m定义的问题。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015141 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved