#include
//检查一个数是否是是质数
//质数返回1
//非质数返回0
//从2开始计算质数,假设质数prim1 + prim2 = num, 即 prim2-prim1 = num - 2*prim1
//欲使prim2-prim1最大,即 num - 2*prim1最大,即prim1最小
//因此可从最小值2开始计算prim1,求得prim1之后,prim2=num - prim1。
//对于从最小素数2开始的一个素数prim1,如果满足:
//num - prim1,记作prim2,也为素数,即可得prim2与prim1就是满足条件(差最大)的一对素数
int check_prime(unsigned int n)
{
unsigned int c;
for (c = 2; c <= n - 1; ++c)
{
if (n % c == 0)
return 0;
}
if (c == n)
return 1;
return 0;
}
//对于大于2的偶数num,计算其差值最大的两个素数,prim1和prim2,求得之后打印输出然后退出函数
void calc(unsigned int num)
{
unsigned int prim1, max;
prim1 = 2;
max = num / 2;
for (; prim1 < max; ++prim1)
{
if (check_prime(prim1) && check_prime(num - prim1))
{
printf("FOUND! %u + %u = %u\n", prim1, num - prim1, num);
return;
}
}
printf("NOT FOUND!\n");
}
//金字塔函数
//输入:n,金字塔的行数
//函数没有进行最大行数判断,如输入行数太大,可能会遇到不可打印字符
//并导致运行结果混乱
void pyramid(unsigned int n)
{
const int max_char_per_line = 80;
int middle = (max_char_per_line - 1) / 2;
unsigned int i = 0, start_pos, j;
#define GO_TO(pos)\
int _lvar = (pos);\
while (--_lvar >= 0) printf(" ")
for (; i < n; ++i)
{
start_pos = middle - i;
//go to start_pos
GO_TO(start_pos);
for (j = 0; j < i + 1; ++j)
{
printf("%c", 'A' + i);
if (j != i)
{
printf(" ");
}
}
printf("\n");
}
printf("\n");
}
//测试函数
int main (int argc, char *argv[])
{
unsigned int n;
printf("input a number: ");
scanf("%u", &n);
calc(n);
printf("input pyramid height:");
scanf("%u", &n);
pyramid(n);
return 0;
}
楼主你好
代码如下:
(1)
/*验证哥德巴赫猜想*/
#include
#include
#include
int f(int n){
int i;
for(i=2;i<=sqrt((double) n);i++)
if(n%i==0)
return 0;
return 1;
}
int main()
{
int i,k,n,count=0;
printf("Input n(大于2的偶数):");
scanf("%d",&n);
for(i=2;i<=n/2;i++){
for(k=2;k<=i;k++){
if(f(k)&&f(2*i-k)){
printf("%d=%d+%d ",2*i,k,2*i-k);
count++;
if(count%5==0)
printf("\n");
break;
}
}
}
printf("\n");
system("pause");
return 0;
}
(2)
#include
#include
void pyramid(int n){
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++){
if(i>=10)
printf("%c",'A'+i-1);
else
printf("%c ",'A'+i-1);
}
printf("\n");
}
}
int main()
{
int n;
printf("Input n:");
scanf("%d",&n);
printf("\n");
pyramid(n);
printf("\n");
return 0;
}
希望能帮助你哈^_^
#include
#include
bool isPrime(int num){
float a=sqrt(num);
for(int i=2;i<=a;i++){
if (num%i==0)
return false;
}
return true;
}
void main(){
int num;
cin>>num;
for (int i=2;i
if (isPrime(i)&&isPrime(num-i))
{
cout<
}
}
}
#include
void main(){
int n;
char x='A';
cin>>n;
for (int i=0;i
for (int j=0;j
if (j
cout<<" ";
}
else
{
cout<
}
cout<
}
}
555