首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求解,为何求1到100的所有质数为什么会打印出9

2013-10-10 
求解,为什么求1到100的所有质数为什么会打印出9RT,这是小弟写的代码#include mystd.h#define SIZE 49int

求解,为什么求1到100的所有质数为什么会打印出9
RT,这是小弟写的代码


#include "mystd.h"
#define SIZE 49

int main()
{
    int sieve[SIZE]; /* the sieve */
    int *sp; /* point to access sieve */
    int number;/* number we are comupting */
    int total = 1;/* count the total number of primes */
    /*
    ** Set the entire sieve to true
    */
    for(sp = sieve; sp < &sieve[SIZE];)
       *sp++ = TRUE;
   /*
   ** Process each number from 3 to as many
   ** as the sieve holds.
    */
    for(number = 3; number += 2;)/*the loop is terminated from inside*/
    {
       /*
       ** Set the point to the proper element in the sieve
       ** and stop the loop if we've gone too far
       */
       sp = &sieve[0] + (number - 3) / 2;
       if(sp > &sieve[SIZE])
             break;
       /*
       ** Now adwance the point by multiple of the number
       ** and set each subsequent entry false
       */
       while( sp += number,sp < &sieve[SIZE])
           *sp = FALSE;
    }
   /*
   ** Now we go through the entire sieve and print the numbers
   ** corresponding to the location that remain TRUE
   */
   printf("2\n");
   for(number = 3,sp = &sieve[0]; sp < &sieve[SIZE] ; number += 2,sp++)
   {
          if(*sp)
          {
          printf("%d\n",number);
          total++;
          }
   }
   printf("the total number of primes is %d\n",total);
   system("pause");
   return 0;
}
而且为什么把筛选数组改为char之后反倒不能找出更多的质数了呢?
[解决办法]
大师经常灌水不用理会,我在4楼帮你找到问题点了。

引用:
Quote: 引用:

char的取值范围-128~127

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试是程序员必须掌握的技能之一。

我调试了好久一直没找到原因,所以才论坛找一下解答的,至于CHAR的取值范围我知道比INT小,但是我还是不明白为什么它不能在给定的数组范围内找到和INT一样的质数,而且我觉得这个代码里面用的数组在一个比较小的范围内CHAR和INT应该关系不大,毕竟每一个数组元素只是一个标志位啊,至于这些注释我只是在模仿英文的写法,上面的代码我也不是照着抄写的,而是理解思想之后自己写出来再调试的,只是到最后没弄明白为什么最后还能打印出数字9来,而且我把数组类型换成CHAR之后找到的质数更少了。

热点排行