求几道C++编程题答案,谢谢了!
1.一个素数,当它的数字位置对换以后仍为素数,这样的数称为绝对素数。编写一个程序,求出所有的两位绝对素数。(要求:判断素数、数字对换位置分别自定义函数实现)2.写两个函数...
1. 一个素数,当它的数字位置对换以后仍为素数,这样的数称为绝对素数。编写一个程序,求出所有的两位绝对素数。(要求:判断素数、数字对换位置分别自定义函数实现)
2. 写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入。(提示:最小公倍数=两整数的乘积/最大公约数)
3. 求方程ax2+bx+c=0的根,用三个函数分别求当b2-4ac大于0、等于0和小于0时的根,并输出结果。( a、b、c的值从主函数中由键盘输入获得。)
4. 已知计算组合的公式如下:
Cm = Cm n>m-n
1 n=0
Cm = m n=1
Cm-1 + Cm-1 n>1
用递归算法计算组合的函数计算Cm的值。 展开
2. 写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,两个整数由键盘输入。(提示:最小公倍数=两整数的乘积/最大公约数)
3. 求方程ax2+bx+c=0的根,用三个函数分别求当b2-4ac大于0、等于0和小于0时的根,并输出结果。( a、b、c的值从主函数中由键盘输入获得。)
4. 已知计算组合的公式如下:
Cm = Cm n>m-n
1 n=0
Cm = m n=1
Cm-1 + Cm-1 n>1
用递归算法计算组合的函数计算Cm的值。 展开
3个回答
展开全部
//第一题
#include <iostream>
#include <cmath>
using namespace std;
bool prime (int m);
int changP(int m);
int main(int argc, char* argv[])
{
for (int i=10; i<100; ++i)
{
if (prime(i))
{
changP(i);
if (prime(i))
{
cout<<i<<' ';
}
}
}
cout<<endl;
system("pause");
return EXIT_SUCCESS;
}
bool prime (int m)
{
int r = sqrt (m);
for (int i = 2; i <= r; i++)
if (m % i == 0)
return false;
return true;
}
int changP(int m)
{
m = (m%10)*10 + m/10;
return m;
}
//第二题
#include <iostream>
#include <cmath>
using namespace std;
void LeastCommonMultiply(int m, int n, int b);
int GreatestCommonDivisor(int m, int n);
int main(int argc, char* argv[])
{
int m = 0, n = 0;
cout<<"请输入两个整数: \b";
cin >> m >> n;
int b = GreatestCommonDivisor(m,n);
LeastCommonMultiply(m, n, b);
system("pause");
return EXIT_SUCCESS;
}
int GreatestCommonDivisor(int m, int n)
{
int a = (m > n) ? m : n;
int b = (m > n) ? n : m;
int c = 0;
while ((a % b) != 0)
{
c = a % b;
a = b;
b = c;
}
return b;
}
//求最小公倍数
void LeastCommonMultiply(int m, int n, int b)
{
//两数相乘再除以最小公倍数
cout << m << "和" << n << "的最大公约数 = " << b << endl;
cout << m << "和" << n << "的最小公倍数 = " << m * n / b << endl;
}
//第三题
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,c,t;
double x1,x2;
cout<<"a,b,c的值:";
cin>>a>>b>>c;
t=b*b-4*a*c;
if(t>=0)
x1=(-b+sqrt(t))/(2*a);
x2=(-b-sqrt(t))/(2*a);
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
if(t<0)
cout<<"该方程无解!"<<endl;
return 0;
}
//第四题
麻烦你再清楚一点:Cm-1 + Cm-1 n>1 看不懂
#include <iostream>
#include <cmath>
using namespace std;
bool prime (int m);
int changP(int m);
int main(int argc, char* argv[])
{
for (int i=10; i<100; ++i)
{
if (prime(i))
{
changP(i);
if (prime(i))
{
cout<<i<<' ';
}
}
}
cout<<endl;
system("pause");
return EXIT_SUCCESS;
}
bool prime (int m)
{
int r = sqrt (m);
for (int i = 2; i <= r; i++)
if (m % i == 0)
return false;
return true;
}
int changP(int m)
{
m = (m%10)*10 + m/10;
return m;
}
//第二题
#include <iostream>
#include <cmath>
using namespace std;
void LeastCommonMultiply(int m, int n, int b);
int GreatestCommonDivisor(int m, int n);
int main(int argc, char* argv[])
{
int m = 0, n = 0;
cout<<"请输入两个整数: \b";
cin >> m >> n;
int b = GreatestCommonDivisor(m,n);
LeastCommonMultiply(m, n, b);
system("pause");
return EXIT_SUCCESS;
}
int GreatestCommonDivisor(int m, int n)
{
int a = (m > n) ? m : n;
int b = (m > n) ? n : m;
int c = 0;
while ((a % b) != 0)
{
c = a % b;
a = b;
b = c;
}
return b;
}
//求最小公倍数
void LeastCommonMultiply(int m, int n, int b)
{
//两数相乘再除以最小公倍数
cout << m << "和" << n << "的最大公约数 = " << b << endl;
cout << m << "和" << n << "的最小公倍数 = " << m * n / b << endl;
}
//第三题
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,c,t;
double x1,x2;
cout<<"a,b,c的值:";
cin>>a>>b>>c;
t=b*b-4*a*c;
if(t>=0)
x1=(-b+sqrt(t))/(2*a);
x2=(-b-sqrt(t))/(2*a);
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
if(t<0)
cout<<"该方程无解!"<<endl;
return 0;
}
//第四题
麻烦你再清楚一点:Cm-1 + Cm-1 n>1 看不懂
展开全部
//========第一题================
#include <iostream>
#include <cmath>
using namespace std;
bool prime (int m);
int changP(int m);
int main(int argc, char* argv[])
{
for (int i=10; i<100; ++i)
{
if (prime(i))
{
changP(i);
if (prime(i))
{
cout<<i<<' ';
}
}
}
cout<<endl;
system("pause");
return EXIT_SUCCESS;
}
bool prime (int m)
{
int r = sqrt (m);
for (int i = 2; i <= r; i++)
if (m % i == 0)
return false;
return true;
}
int changP(int m)
{
m = (m%10)*10 + m/10;
return m;
}
//第二题==========================
#include <iostream>
#include <cmath>
using namespace std;
void LeastCommonMultiply(int m, int n, int b);
int GreatestCommonDivisor(int m, int n);
int main(int argc, char* argv[])
{
int m = 0, n = 0;
cout<<"请输入两个整数: \b";
cin >> m >> n;
int b = GreatestCommonDivisor(m,n);
LeastCommonMultiply(m, n, b);
system("pause");
return EXIT_SUCCESS;
}
int GreatestCommonDivisor(int m, int n)
{
int a = (m > n) ? m : n;
int b = (m > n) ? n : m;
int c = 0;
while ((a % b) != 0)
{
c = a % b;
a = b;
b = c;
}
return b;
}
//求最小公倍数
void LeastCommonMultiply(int m, int n, int b)
{
//两数相乘再除以最小公倍数
cout << m << "和" << n << "的最大公约数 = " << b << endl;
cout << m << "和" << n << "的最小公倍数 = " << m * n / b << endl;
}
后面两题我不会做了.
#include <iostream>
#include <cmath>
using namespace std;
bool prime (int m);
int changP(int m);
int main(int argc, char* argv[])
{
for (int i=10; i<100; ++i)
{
if (prime(i))
{
changP(i);
if (prime(i))
{
cout<<i<<' ';
}
}
}
cout<<endl;
system("pause");
return EXIT_SUCCESS;
}
bool prime (int m)
{
int r = sqrt (m);
for (int i = 2; i <= r; i++)
if (m % i == 0)
return false;
return true;
}
int changP(int m)
{
m = (m%10)*10 + m/10;
return m;
}
//第二题==========================
#include <iostream>
#include <cmath>
using namespace std;
void LeastCommonMultiply(int m, int n, int b);
int GreatestCommonDivisor(int m, int n);
int main(int argc, char* argv[])
{
int m = 0, n = 0;
cout<<"请输入两个整数: \b";
cin >> m >> n;
int b = GreatestCommonDivisor(m,n);
LeastCommonMultiply(m, n, b);
system("pause");
return EXIT_SUCCESS;
}
int GreatestCommonDivisor(int m, int n)
{
int a = (m > n) ? m : n;
int b = (m > n) ? n : m;
int c = 0;
while ((a % b) != 0)
{
c = a % b;
a = b;
b = c;
}
return b;
}
//求最小公倍数
void LeastCommonMultiply(int m, int n, int b)
{
//两数相乘再除以最小公倍数
cout << m << "和" << n << "的最大公约数 = " << b << endl;
cout << m << "和" << n << "的最小公倍数 = " << m * n / b << endl;
}
后面两题我不会做了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
123题都不难,第4题看不懂
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询