gql241201 发表于 2008-12-26 15:54:28

写了个小程序,我不知错在那儿?无法运行

刚学在Visual C++ 6.0下写程序
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
voidmain()
{
longa,b,l=0;
cout<<"please input two numbers:\n";
cin>>a>>b;
cout<<"primes from"<<a<<"to"<<b<<"is:\n";
if(a%2==0)
a++;
    for(long m=a;m<=b;m+=2)
{
    int sqrtm=sqrt(m);
    int i;
    for(i=2;i<=sqrtm;i++)
      if(m%i==0)
      break;
          if(i>sqrtm)
    {
      if(l++%10==0)
      cout<<endl;
      cout<<setw(5)<<m;
    }
}
}
编译后是这样,
prime1.obj - 1 error(s), 0 warning(s)
我不知错在那儿?
请各位能指出来。

g99 发表于 2008-12-26 16:45:20

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

voidmain()
{
longa,b,l=0;
cout<<"please input two numbers:\n";
cin>>a>>b;
cout<<"primes from"<<a<<"to"<<b<<"is:\n";
if(a%2==0)
a++;
    for(long m=a;m<=b;m+=2)
{
    int sqrtm=(long)sqrt((double)m);
    int i;
    for(i=2;i<=sqrtm;i++)
      if(m%i==0)
      break;
          if(i>sqrtm)
    {
      if(l++%10==0)
      cout<<endl;
      cout<<setw(5)<<m;
    }
}
}

g99 发表于 2008-12-26 16:45:45

please input two numbers:
1
10000
primes from1to10000is:

    1    3    5    7   11   13   17   19   23   29
   31   37   41   43   47   53   59   61   67   71
   73   79   83   89   97101103107109113
...
9901 9907 9923 9929 9931 9941 9949 9967 9973

liangbch 发表于 2008-12-26 16:51:01

有几处错误,改了下,请仔细看看#include<iostream.h>
#include<iomanip.h>
#include<math.h>
voidmain()
{
        longa,b,l=0;
        cout<<"please input two numbers:\n";
        cin>>a>>b;
        cout<<"primes from"<<a<<"to"<<b<<"is:\n";
        if(a%2==0)
                a++;
          for(long m=a;m<=b;m+=2)
        {
                int sqrtm=(int)sqrt(m);
                int i;
                for(i=2;i<=sqrtm;i++)
                {
                        if(m%i==0)
                                break;
                }
               
                if(i>sqrtm)
                {
                        if(l++%10==0)
                                cout<<endl;
                        cout<<setw(5)<<m;
                }
        }
}改完以后,基本正确,但是如果输入的a从2开始的话,会漏掉素数2。另外效率依然有点提高。

liangbch 发表于 2008-12-26 16:52:56

求某一范围内的素数用筛法,而不用试除法。试除法太慢了。

gql241201 发表于 2008-12-27 14:59:12

我把二位的代码写上运行仍错,我怀疑步骤有错
1. 打开Visual C++ 6.0
2. 点击文件下拉菜单“新建”,弹出“新建”活动窗口
3. 点击“文件”标签
4. 双击C++ 源文件
5. 在客户区写入源程序代码
6. 单击编译按钮
这时输出窗口出现“prime1.obj - 1 error(s), 0 warning(s)”
否则可以执行步骤7.按运行键
出运行结果了。
不知错在何处?

gql241201 发表于 2008-12-27 21:01:55

下面是我的错误信息:
--------------------Configuration: prime0001 - Win32 Debug--------------------
Compiling...
Error spawning cl.exe

prime0001.obj - 1 error(s), 0 warning(s)
请指出改正的地方,谢了

medie2005 发表于 2008-12-27 21:19:07

你VC装得有问题.

gql241201 发表于 2008-12-28 22:30:16

多谢各位的帮忙,总算可以运算了。
但最多可以写300行,超过了怎么办呢?
下面是实际情况:
please input two numbers:
2 26900
primes from 2 to 26900 is:

   3   5   7    11    13    17    19    23    29    31
...................................................................................
26821 26833 26839 26849 26861 26863 26879 26881 26891 26893
Press any key to continue
这里少一行《please input two numbers:》
2 27000
primes from 2 to 27000 is:

   3   5   7    11    13    17    19    23    29    31
...................................................................................
26903 26921 26927 26947 26951 26953 26959 26981 26987 26993
Press any key to continue

gql241201 发表于 2008-12-28 22:35:02

请问筛法,与试除法有何不同?
筛法的求某一范围内的素数的代码如何写?
页: [1] 2 3
查看完整版本: 写了个小程序,我不知错在那儿?无法运行