一、400以内的素数
#include<iostream>
using namespace std;
#include<math.h>
bool judge(
int n)
{
for(
int i=
2;i<=
sqrt(
float(n));i++)
if(n%i==
0)
return false;
return true;
}
void main()
{
int j=
0;
for(
int i=
2;i<
400;i++)
if(judge(i))
{
j++;
cout<<i<<(j%
5==
0?
'\n':
'\t');
}
cout<<endl;
}
二、Goldbech猜想
#include<iostream>
using namespace std;
#include<math.h>
bool j_prime(
int n)
{
for(
int i=
2;i<
sqrt(
float(n));i++)
if(n%i==
0)
return false;
return true;
}
void main()
{
int i,n;
cout<<
"请输入一个和数:\n";
cin>>n;
for(i=
2;i<n;i++)
if(j_prime(i)&&j_prime(n-i))
break;
cout<<n<<
"="<<i<<
"+"<<n-i<<endl;
}
三、质因子之和(20=2×2×5)
#include<iostream>
using namespace std;
#include<math.h>
bool j_prime(
int n)
{
for(
int i=
2;i<
sqrt(
float(n));i++)
if(n%i==
0)
return false;
return true;
}
void main()
{
int i,n;
cout<<
"请输入一个和数:\n";
cin>>n;
for(i=
2;i<n;i++)
if(j_prime(i)&&j_prime(n-i))
break;
cout<<n<<
"="<<i<<
"+"<<n-i<<endl;
}
四、变量排序
#include<iostream>
using namespace std;
void v_sort(
int &a,
int &b,
int &c)
{
int temp;
if(a>b)
if(b<c)
{temp=b,b=c,c=temp;}
else
return;
else
{
temp=a,a=b,b=temp;
if(b<c)
{temp=b,b=c,c=temp;}
else
return;
}
}
void main()
{
int a,b,c;
cout<<
"请输入3个整数\n";
cin>>a>>b>>c;
cout<<a<<
'\t'<<b<<
'\t'<<c<<
'\n';
v_sort(a,b,c);
cout<<
"排序后:\n"<<a<<
'\t'<<b<<
'\t'<<c<<endl;
}
转载请注明原文地址: https://ju.6miu.com/read-7561.html