acm 寻找素数对

这里写图片描述

这里写图片描述

#include <stdio.h>
#include <windows.h>
#define MAX 100
bool function(int n);
int main()
{   
    int n,i = 0;
    char ch;
    int arr1[MAX],arr2[2*MAX];  /* 第一个数组保存输入,第二个数组保存输出*/
    int low,high;
    int count1,count2 = 0;

    do { 
        scanf("%d",&arr1[i]);   
        ch=getchar(); 
        i++;
    }while(ch!='\n');


    for(count1=0; count1<i; count1++)
    {   /* low high初始化时先-1成为奇数,之后循环-2 提高效率*/
        int flag =1;
        for(low = arr1[count1]-1; low>0 && flag==1;low-=2 ) 
            for(high = arr1[count1]-1; high>low && flag==1; high -= 2) {
                if(function(low) && function(high) &&low + high == arr1[count1]) {
                    arr2[count2] = low;
                    count2++; 
                    arr2[count2] = high;
                    count2++;
                    flag = 0;
                }
            } 
    }

    for(count1=0,count2=0; count1<i; count1++) {
        printf("%d",arr2[count2]);
        count2++;
        printf("  %d\n",arr2[count2]);
        count2++;
    }
    return 0;   
} 
bool function(int n)
{
    for(int i = n-1; i > 1 ; i--)
    {
        if(n%i ==0) return false; 
    }
    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值