文章目录
1、最大公约数和最小公倍数
#include<iostream>
using namespace std;
int gcd(int a,int b);
int lcm(int a,int b);
int main(){
int x,y,max,min;
cin>>x>>y;
max=gcd(x,y);
min=lcm(x,y);
cout<<"最大公倍数:"<<max<<endl;
cout<<"最小公倍数:"<<min<<endl;
return 0;
}
int gcd(int a,int b){
int tmp = a%b;
if(tmp == 0){
return b;
}
else{
return gcd(b,tmp);
}
}
int lcm(int a,int b){
int c;
c=a*b/gcd(a,b);
return c;
}
2、求一元二次方程的根
#include<iostream>
#include<cmath>
using namespace std;
double result1(double a,double b,double c,double t);
double result2(double a,double b,double c,double t);
double result3(double a,double b,double c,double t);
double x1,x2;
int main(){
double a,b,c,t;
cin>>a>>b>>c;
t=b*b-4*a*c;
if(t>0){
result1(a,b,c,t);
}
else if(t==0){
result2(a,b,c,t);
}
else{
result3(a,b,c,t);
}
return 0;
}
double result1(double a,double b,double c,double t){
x1 = (-b + sqrt(t)) / (2*a);
x2 = (-b - sqrt(t)) / (2*a);
cout << "两个不同的实根." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
double result2(double a,double b,double c,double t){
x1 = (-b + sqrt(t)) / (2*a);
cout << "两个相同的实根." << endl;
cout << "x1 = x2 = " << x1 << endl;
}
double result3(double a,double b,double c,double t){
double real=-b/(2*a);
cout << "两个不同的虚根." << endl;
cout << "x1 = " << real<<"+" <<(sqrt(t)/(2*a))<<"i"<< endl;
cout << "x1 = " << real<<"-" <<(sqrt(t)/(2*a))<<"i"<< endl;
}
3、判别素数
#include<iostream>
using namespace std;
bool is_sushu(int a);
int main(){
int n;
cin>>n;
if(is_sushu(n)){
cout<<n<<"是素数"<<endl;
}
else{
cout<<n<<"不是素数"<<endl;
}
return 0;
}
bool is_sushu(int a){
for(int i=2;i<=a/2;i++){
if(a%i==0){
return false;
}
}
return true;
}
4、求a!+b!+c!
#include<iostream>
using namespace std;
int fac(int);
int main(){
int a,b,c,sum=0;
cin>>a>>b>>c;
sum=fac(a)+fac(b)+fac(c);
cout<<"a!+b!+c!="<<sum<<endl;
return 0;
}
int fac(int n){//递归函数
if(n==1)
return 1;
else{
return n*fac(n-1);
}
}
6、用牛顿迭代法求方程的根
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double solut(double,double,double,double);
double a,b,c,d;
cin>>a>>b>>c>>d;
cout<<solut(a,b,c,d)<<endl;
return 0;
}
double solut(double a,double b,double c,double d){
double x=1,x0,f,f1;
do{
x0=x;
f=((a*x0+b)*x0+c)*x0+d;
f1=(3*a*x0+2*b)*x0+c;
x=x0-f/f1;
}
while(fabs(x-x0)>=1e-5);
return x;
}
7、验证哥德巴赫猜想
哥德巴赫猜想:
1)任一不小于6的偶数,都可以表示成两个奇质数之和
2)任一不小于9的奇数,都可以表示成三个奇质数之和
#include<iostream>
using namespace std;
int main(){
void godbaha(int);
int n;
cin>>n;
godbaha(n);
return 0;
}
void godbaha(int n){
bool prime(int);//声明
int a,b;
for(a=3;a<=n/2;a+=2){
if(prime(a)){
b=n-a;
if(prime(b)){
cout<<n<<"="<<a<<"+"<<b<<endl;
}
}
}
}
//判别是否是素数
bool prime(int a){
for(int i=2;i<=a/2;i++){
if(a%i==0){
return false;
}
}
return true;
}
8、用递归方法求n阶勒让德多项式的值
#include<iostream>
using namespace std;
int main(){
float p(int,int);
int x,n;
cin>>n>>x;
cout<<"P"<<n<<"(x)="<<p(n,x)<<endl;
return 0;
}
float p(int a,int b){
if(a==0)return 1;
else if(a==1)return b;
else{
return (((2*a-1)*b*p((a-1),b)-(a-1)*p((a-2),b))/a);
}
}
9、汉诺塔问题
只有3个盘时,分三步:A–>B(上面的),A–>C(最下面的),B–>C(剩下的)。上面的有两个,第一步又可分为:A–>C,A–>B,C–>B;第三步可分为:B–>A,B–>C,A–>C。所以一共有7步:A–>C,A–>B,C–>B,A–>C,B–>A,B–>C,A–>C。
当数量多于3时,如果有n个,可以先完成n-1个盘的移动,把n-1个盘都放到B后,A剩下的一个就可以直接移动到C了(这也是最容易实现的一步),然后再把B上的n-1个借助A移动到C。同样的,对于n-1个,可以先完成n-2个的移动。直到最后剩下一个了。
#include<iostream>
using namespace std;
int main(){
void hanoi(int n,char one,char two,char three);
int m;
cin>>m;
cout<<"The steps of moving "<< m<<" disks:"<<endl;
hanoi(m,'A','B','C');
return 0;
}
void hanoi(int n,char one,char two,char three){
void move(char x,char y);
if(n==1)move(one,three);
else{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y){
cout<<x<<"-->"<<y<<endl;
}
10、递归法将一个整数n转换成字符串。
#include<iostream>
using namespace std;
int main(){
void convert(int n);
int m;
cin>>m;
if(m<0){
cout<<"-";
m=-m;
}
convert(m);
cout<<endl;
return 0;
}
void convert(int n){
int i;
char c;
if((i=n/10)!=0)convert(i);
c=n%10+'0';
}