acm 寻找素数对

来源:互联网 发布:怎么关淘宝店铺 编辑:程序博客网 时间:2024/06/04 00:50

这里写图片描述

这里写图片描述

#include <stdio.h>#include <windows.h>#define MAX 100bool 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
原创粉丝点击