鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖

(已经解决)感谢版主

[复制链接]
发表于 2014-1-27 12:38:04 | 显示全部楼层

最后判断这个范围内的数字是否符合猜想的时候写错了
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>

  4. int isPrime(long int i);
  5. int isGold(long int i);

  6. int main()
  7. {
  8.                 long int low,hig,temp;
  9.                 int k;
  10.                 clock_t start, finish;
  11.                 double  duration;
  12. star:        setbuf(stdin,NULL);
  13.                 k = 1;
  14.                 printf("please putinto low:");
  15.                 scanf("%ld",&low);
  16.                 printf("please putinto hig:");
  17.                 scanf("%ld",&hig);
  18.                
  19.                 if(low % 2 == 0)
  20.                 {
  21.                         if(low>=hig)
  22.                         {
  23.                                 printf("low can not bigger than hig!\n");
  24.                                 goto star;
  25.                         }
  26.                         else
  27.                         {
  28.                                 if(low <= 2)
  29.                                 {
  30.                                         printf("low can not smaller than 2!\n");
  31.                                         goto star;
  32.                                 }
  33.                                 else
  34.                                 {
  35.                                         if(hig %2 ==0)
  36.                                         {
  37.                                         }
  38.                                         else
  39.                                         {
  40.                                                 printf("hig必须是偶数!\n");
  41.                                                 goto star;
  42.                                         }
  43.                                 }
  44.                         }
  45.                 }
  46.                 else
  47.                 {
  48.                         printf("low必须是偶数!\n");
  49.                         goto star;
  50.                 }
  51.                 start = clock();
  52.                 for(temp = low;temp <= hig;temp=temp+2)
  53.                 {
  54.                         if(isGold(temp) == 0)
  55.                         {
  56.                                 k = 0;
  57.                                 break;
  58.                         }
  59.                 }
  60.                 finish = clock();
  61.                 duration = (double)(finish - start) / CLOCKS_PER_SEC;
  62.                 if(k == 1)
  63.                 {
  64.                         printf("在%ld--%ld符合哥德巴赫猜想\n",low,hig);
  65.                         printf("验证过程所用时间为%lf seconds\n", duration);
  66.                         goto star;
  67.                 }
  68.                 else
  69.                 {
  70.                         printf("在%ld--%ld不符合哥德巴赫猜想\n",low,hig);
  71.                         printf("验证过程所用时间为%lf seconds\n", duration);
  72.                         goto star;
  73.                 }
  74.                
  75.                 return 0;
  76. }

  77. int isPrime(long int i)
  78. {
  79.         long int j;
  80.         if(i == 2)
  81.         {
  82.                 return 1;
  83.         }
  84.         if(i%2==0)
  85.         {
  86.                 return 0;
  87.         }
  88.         for(j = 3;j<= sqrt(i);j = j + 2)
  89.         {
  90.                 if(i%j == 0)
  91.                 {
  92.                         return 0;
  93.                 }
  94.         }
  95.         return 1;
  96. }

  97. int isGold(long int i)
  98. {
  99.         int j;
  100.         j = i - 2;
  101.         if(isPrime(j)==1)
  102.         {
  103.                 return 1;
  104.         }
  105.         for(j = 3;j <= i/2+1;j = j + 2)
  106.         {
  107.                 if(isPrime(i-j)==1 && isPrime(j)==1)
  108.                 {
  109.                         return 1;
  110.                 }
  111.         }
  112.        
  113.         return 0;
  114. }
复制代码
OK了
至于卡住了是因为要循环的次数太多 我估计你开着去吃顿饭回来应该有结果了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-1-27 12:57:04 | 显示全部楼层
牡丹花下死做鬼 发表于 2014-1-27 12:38
最后判断这个范围内的数字是否符合猜想的时候写错了OK了
至于卡住了是因为要循环的次数太多 我估计你开着 ...

恩恩 真心感谢,,来回看版主你的代码学习到很多,,:victory:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-1-29 17:26:30 | 显示全部楼层

路过打个酱油,学习一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-17 14:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表