项目1:素数和回文
#include<iostream.h>
inline int isPrimer(int x)
{
int k;
for(k=2;k<x;k++)
{
if(x%k==0)
break;
}
if(k==x)
return 1;
else
return 0;
}
inline int isPalindrome(int x)
{
int a,b,c,d;
a=x/10%10;
b=x%10;
c=x/100;
if(x<10) d=x;
else if(x<100) d=b*10+a;
else d=b*100+a*10+c;
if(d==x)
return 1;
else
return 0;
}
inline int fan(int x)
{
int a,b,c,d;
a=x/10%10;
b=x%10;
c=x/100;
if(x<10) d=x;
else if(x<100) d=b*10+a;
else d=b*100+a*10+c;
return d;
}
int main()
{
int x,y,i=0,j=0,p=0,r=0;
int a[300]={0},b[300]={0},c[300]={0},d[300]={0};
for(x=2;x<=300;x++)
{
if(isPrimer(x)==1)
a[i++]=x;
if(isPalindrome(x)==1)
b[j++]=x;
if((isPalindrome(x)==1)&&(isPrimer(x)==1))
c[p++]=x;
if((isPrimer(fan(x))==1)&&(isPrimer(x)==1))
d[r++]=x;
}
cout<<"300以内的所有素数:"<<endl;
for(y=0;y<i;y++)
cout<<a[y]<<" "<<"\t";
cout<<endl;
cout<<"300以内的所有回文数:"<<endl;
for(y=0;y<j;y++)
cout<<b[y]<<" "<<"\t";
cout<<endl;
cout<<"300以内的所有回文素数:"<<endl;
for(y=0;y<p;y++)
cout<<c[y]<<" "<<"\t";
cout<<endl;
cout<<"300以内的所有可逆素数:"<<endl;
for(y=0;y<r;y++)
cout<<d[y]<<" "<<"\t";
cout<<endl;
}
项目2:最大公约和和最小公倍数
#include<iostream.h>
int w,b;
void f1(int m,int n)
{
int t;
if(m<n)
{
f1(n,m);
}
else
{
while(n!=0)
{
t=m%n;
m=n;
n=t;
}
w=m;
}
}
void f2(int m,int n)
{
b=(m*n)/w;
}
void main()
{
int m,n;
cout<<"请输入两个数: ";
cin>>m>>n;
f1(m,n);
f2(m,n);
cout<<"最大公约数是:"<<w<<endl;
cout<<"最大公倍数是:"<<b<<endl;
}