【問題】C++的問題


Recommended Posts

/*計算兩數的最大公因數及最小公倍數*/

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

int a,b,i,M=0,m=0;

printf("請輸入兩個正整數:");

scanf("%d %d",&a,&b);

for(i=1;i if(!(a%i) && !(b%i))

M = i;

}

printf("%d 和 %d 之最大公因數 %d\n",a,b,M);

i = a while(1) {

if(!(i%a) && !(i%b)) {

m = i;

break;

}

i++;

}

printf("%d 和 %d 之最小公倍數 %d\n",a,b,m);

system("pause");

}

鏈接文章
分享到其他網站

/*計算兩數的最大公因數及最小公倍數*/

#include <stdio.h>

#include <stdlib.h>

int main(void){

int a,b,i,M=0,m=0;

printf("請輸入兩個正整數:");

scanf("%d %d",&a,&b);

for(i=1;(i<=a)&&(i<=b);i++){

if(a%i==0 && b%i==0){

M = i;

}

}

printf("%d 和 %d 之最大公因數 %d\n",a,b,M);

m=(a*b)/M;

printf("%d 和 %d 之最小公倍數 %d\n",a,b,m);

system("pause");

}

記得按三餐膜拜賴董

還有自己判斷負數和小數....等錯誤輸入

鏈接文章
分享到其他網站
  • 2 weeks later...

你的問題是『計算兩數的最大公因數及最小公倍數』嗎?

首先已知,兩數之最小公倍數等於兩數相乘除以最大公因數。

解一:(以C++方式撰寫)

#include <iostream>

#include <cstdlib>

using namespace std;

int gcd(int,int); //函數原型的宣告

int main(void)

{

int a,b;

cout << "a = ?";

cin >> a;

cout << "b = ?";

cin >> b;

cout << "The GCD of " << a << " and " << b;

cout << " is " << gcd(a,b) << endl; //印出gcd值

cout << "The LCM of " << a << " and " << b;

cout << " is " << (a*b)/gcd(a,b) << endl; //印出lcm值

system("pause");

return 0; //傳回整數0,此數由系統接收,表此程式可順利完成且無誤

}

int gcd(int i,int j) //自訂的函數gcd(),傳回最大公因數

{

int g;

while(j!=0)

{

g=i%j;

i=j;

j=g;

}

return i;

}

以上為使用c++的方式寫的,我將你們所#include 的 stdio.h 及 stdlib.h 更換成iostream(input/output stream 輸入/輸出串流) 及 cstdlib(standard library 標準函數庫),cout、cin、endl 是c++的寫法。

若改成以C寫的話為如下:

#include <stdio.h>

#include <stdlib.h>

int gcd(int,int); //函數原型的宣告

int main(void)

{

int a,b,lcm;

printf("請輸入二數:");

scanf("%d %d",&a,&b);

lcm = (a*b)/gcd(a,b);

printf("The GCD of %d and %d is %d \n",a,b,gcd(a,b));

printf("The LCM of %d and %d is %d \n",a,b,lcm);

system("pause");

return 0; //傳回整數0,此數由系統接收,表此程式可順利完成且無誤

}

int gcd(int i,int j) //自訂的函數gcd(),傳回最大公因數

{

int g;

while(j!=0)

{

g=i%j;

i=j;

j=g;

}

return i;

}

※若內容有誤、有須改善之處,歡迎各位踴躍提出,或能寄信、即時通留言給我,謝謝大家!

鏈接文章
分享到其他網站

解二:使用歐幾里德轉輾相除法:設有兩個整數m、n(m>n),m和n的最大公因數等於m-n和n的公因數。

(以C++方式撰寫)

#include <iostream>

#include <cstdlib>

using namespace std;

int gcd(int,int); //函數原型的宣告

int main(void)

{

int a,b,m,n;

cout << "a = ?";

cin >> a;

cout << "b = ?";

cin >> b;

cout << "The GCD of " << a << " and " << b;

cout << " is " << gcd(a,b) << endl; //印出gcd值

cout << "The LCM of " << a << " and " << b;

cout << " is " << (a*b)/gcd(a,b) << endl; //印出lcm值

system("pause");

return 0; //傳回整數0,此數由系統接收,表此程式可順利完成且無誤

}

int gcd(int i,int j) //自訂的函數gcd(),傳回最大公因數

{

int m,n;

m = i;

n = j;

while(m!=n)

{

if(m>n)

m=m-n;

else

n=n-m;

}

return m;

}

那如果要改成C的寫法的話,你可以參考上篇的寫法,自己改看看吧!^^

※若內容有誤、有須改善之處,歡迎各位踴躍提出,或能寄信、即時通留言給我,謝謝大家!

鏈接文章
分享到其他網站
  • 1 month later...

只要利用輾轉相除法的原理就好了=w=~

目前想到最簡便的:(我用C++的寫法)

#include<iostream>

#include<algorithm>

using namespace std;

int main( int argc, char * argv[] )

{

int a,b;

cin>>a>>b;

if(b>a)

swap(a,b);

while(b>0)

{

a=a%b;

swap(a,b);

}

cout<<a;

return 0;

}

這裡面就是說一開始輸入a,b兩數

如果b>a的話就把這兩個數字互換

例如b=12,a=8 → b=8,a=12

之後做一個迴圈,把a和b相除的餘數丟進a裡

再做一次互換的動作

(這是為了讓a都維持>b的情況)

迴圈的持續條件設成b>0,也就是一直做到不能作輾轉相除為止

之後將a印出來就是答案了

好奇怪在編輯列裡有把程式碼排版都弄好,有縮排

結果傳出來就都靠左了= =""

鏈接文章
分享到其他網站
  • 10 months later...

樓主大括號少一個..我只知道這樣

以下是我的寫法

#include<stdio.h>

#include<stdlib.h>

int main(void)

{

int a,b,c,d,e;

printf("輸入第一個整數:");

scanf("%d",&a);

printf("輸入第二個整數:");

scanf("%d",&b);

d=a%b;

e=a*b;

while(d=a%b)

{

a=b;

b=d;

}

printf("最大公因數為:%d\n",b);

printf("最小公倍數為:%d\n",e/b);

system("pause");

}

兩數之最小公倍數為兩數相乘再除以最大公因數....

當然 這只限於兩數....

我們老師出3數的題目.... 所以我寫不出來說 有人能題供一下3數之最小公倍數的寫法嗎

感謝!!!

鏈接文章
分享到其他網站

樓上的...

N個就做N-1次啊.....

我的程式:


#include <stdio.h>

int main(void)
{
unsigned long int a,b,r;

scanf("%ld %ld",&a,&b);
while((r=a%b)!=0){
a=b;
b=r;
}
printf("The GCD is %ld\n",b);

return 0;
}

基本上...這算基本題吧...

PS:判斷大小是不必要的....因為算完餘數後一樣會交換

鏈接文章
分享到其他網站

的確 3數的最大公因數只要再用一次while就可以 所以是寫的出來...

可是3數的最小公倍數就沒辦法了阿 總不能拿3數相乘再除以最大公因數吧 我之前還

傻傻的試過了 結果花了我1小時才發現這錯誤

聽說是用短除法算 可是我不會><

鏈接文章
分享到其他網站
總不能拿3數相乘再除以最大公因數吧

N個要除以GCD的N-1次方才對

公式啊..不,應該說證明...

三數a,b,c

a=gm

b=gn

c=gl

且m,n,l,互質,所以g為a,b,c之公因數

三數之最小公倍數為:g*m*n*l

也可以寫成:(a*b*c)/(g^2)

N個的就自己推啦,很簡單的!!!

另外最小公倍數也可以用標準分解式吧..

至少那種方法一定解的開,可是會比較慢

不過找質數的地方要記得用sqrt()才不會跑N久...哈哈

加油啦~

鏈接文章
分享到其他網站

順便補給你程式碼:


#include <stdio.h>
#include <stdlib.h>

long gcd(long a,long b)
{
long r;
while((r=a%b)!=0){
a=b;
b=r;
}
return b;
}

long lcm(long g,long *a,long n)
{
long i,l=1;
for(i=0;i<n-1;i++)
l*=a[i]/g;
l*=a[n-1];
return l;
}

int main(void)
{
long num,*a,i,l,g=0;

printf("How many numbers do you want to enter:");
scanf("%ld",&num);
if(num<=1){
printf("Error!\n");
return -1;
}
a=(long *)malloc(num*sizeof(long));
for(i=0;i<num;i++){
scanf("%ld",&a[i]);
if(g==1)
continue;
if(i==1)
g=gcd(a[0],a[1]);
else if(i>1)
g=gcd(g,a[i]);
}
l=lcm(g,a,num);
free(a);
printf("The GCD is %ld\nThe LCM is %ld\n",g,l);
return 0;
}

還沒除錯....因為人在外面...

不懂我在加註解...

鏈接文章
分享到其他網站

請登入後來留意見

在登入之後,您才能留意見



立即登入