C++语言:1.判断1个数能否同时被3和5整除。2.求m与n的最大公约数。用传统流程图,谢谢 急求
2个回答
展开全部
数不大的话直接枚举不就可以了
1.
#include<iostream>
using namespace std;
int main()
{
int i;
cout<<"请输入数字"<<endl;
cin>>i;
if(i%3==0&&i%5==0)
cout<<"可以被整除"<<endl;
else
cout<<"不能整除"<<endl;
return 0;
}
2.
#include<iostream>
using namespace std;
int main()
{
int m,n,i,t;
cout<<"请输入m"<<endl;
cin>>m;
cout<<"请输入n"<<endl;
cin>>n;
t=m>n?n:m; //两数中最小值
for(i=t;i>0;i--)
{
if(m%i==0&&n%i==0)
{
cout<<"最大公约数为"<<i<<endl;
break;
}
}
return 0;
}
//自己写的小程序,那个公约数没想出其他的办法,只有迭代
1.
#include<iostream>
using namespace std;
int main()
{
int i;
cout<<"请输入数字"<<endl;
cin>>i;
if(i%3==0&&i%5==0)
cout<<"可以被整除"<<endl;
else
cout<<"不能整除"<<endl;
return 0;
}
2.
#include<iostream>
using namespace std;
int main()
{
int m,n,i,t;
cout<<"请输入m"<<endl;
cin>>m;
cout<<"请输入n"<<endl;
cin>>n;
t=m>n?n:m; //两数中最小值
for(i=t;i>0;i--)
{
if(m%i==0&&n%i==0)
{
cout<<"最大公约数为"<<i<<endl;
break;
}
}
return 0;
}
//自己写的小程序,那个公约数没想出其他的办法,只有迭代
展开全部
帮你把所有实现放在一个代码里面,因为GCD 和 LCM其实可以一起求的,所以就顺带写了LCM,有一个建议的菜单操作,显得人性化一点:
#include <iostream>
using namespace std;
void GCD_LCM();
void BOTH();
int menu();
int main()
{
menu();
return 0;
}
void GCD_LCM() {
int integer1, integer2, remainder, product;
cout << "------------------------GCD & LCM------------------------" << endl;
cout << "Please enter two integers: ";
cin >> integer1 >> integer2;
int temp;
if (integer1 < integer2)
{
temp = integer1;
integer1 = integer2;
integer2 = temp;
}
product = integer1 * integer2;
while (integer2 != 0)
{
remainder = integer1%integer2;
integer1 = integer2;
integer2 = remainder;
}
cout << "The greatest common divisor is: " << integer1 << endl;
cout << "The lowest common multiple is: " << product/integer1 << endl;
cout << "---------------------------------------------------------\n" << endl;
}
void BOTH() {
cout << "--------------------------3 & 5--------------------------\n" << endl;
int integer;
cout << "Please enter a integer: ";
cin >> integer;
if (integer%3 == 0 && integer%5 == 0)
{
cout << "The integer can be divided with no remainder by 3 & 5" << endl;
}
else
cout << "The integer can;t be divided with no remainder by 3 & 5" << endl;
cout << "---------------------------------------------------------\n" << endl;
}
int menu() {
cout << "------------------------Welcome------------------------" << endl;
cout << "1: GCD & LCM 2: 3 & 5 3: quit" << endl;
cout << "-------------------------------------------------------" << endl;
cout << "Please enter your choice: ";
int command;
while (cin >> command && command != 3)
{
switch (command)
{
case 1:
GCD_LCM();
break;
case 2:
BOTH();
break;
default:
break;
}
cout << "Please enter your choice: ";
}
return 0;
}
#include <iostream>
using namespace std;
void GCD_LCM();
void BOTH();
int menu();
int main()
{
menu();
return 0;
}
void GCD_LCM() {
int integer1, integer2, remainder, product;
cout << "------------------------GCD & LCM------------------------" << endl;
cout << "Please enter two integers: ";
cin >> integer1 >> integer2;
int temp;
if (integer1 < integer2)
{
temp = integer1;
integer1 = integer2;
integer2 = temp;
}
product = integer1 * integer2;
while (integer2 != 0)
{
remainder = integer1%integer2;
integer1 = integer2;
integer2 = remainder;
}
cout << "The greatest common divisor is: " << integer1 << endl;
cout << "The lowest common multiple is: " << product/integer1 << endl;
cout << "---------------------------------------------------------\n" << endl;
}
void BOTH() {
cout << "--------------------------3 & 5--------------------------\n" << endl;
int integer;
cout << "Please enter a integer: ";
cin >> integer;
if (integer%3 == 0 && integer%5 == 0)
{
cout << "The integer can be divided with no remainder by 3 & 5" << endl;
}
else
cout << "The integer can;t be divided with no remainder by 3 & 5" << endl;
cout << "---------------------------------------------------------\n" << endl;
}
int menu() {
cout << "------------------------Welcome------------------------" << endl;
cout << "1: GCD & LCM 2: 3 & 5 3: quit" << endl;
cout << "-------------------------------------------------------" << endl;
cout << "Please enter your choice: ";
int command;
while (cin >> command && command != 3)
{
switch (command)
{
case 1:
GCD_LCM();
break;
case 2:
BOTH();
break;
default:
break;
}
cout << "Please enter your choice: ";
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询