找回密码
 欢迎注册
查看: 19623|回复: 20

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

[复制链接]
发表于 2008-12-26 15:54:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
刚学在Visual C++ 6.0下写程序
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
void  main()
{
  long  a,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)
我不知错在那儿?
请各位能指出来。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-12-26 16:45:20 | 显示全部楼层
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

void  main()
{
  long  a,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;
    }
  }
}
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 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   97  101  103  107  109  113
...
9901 9907 9923 9929 9931 9941 9949 9967 9973
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-12-26 16:51:01 | 显示全部楼层
有几处错误,改了下,请仔细看看
  1. #include<iostream.h>
  2. #include<iomanip.h>
  3. #include<math.h>
  4. void  main()
  5. {
  6.         long  a,b,l=0;
  7.         cout<<"please input two numbers:\n";
  8.         cin>>a>>b;
  9.         cout<<"primes from"<<a<<"to"<<b<<"is:\n";
  10.         if(a%2==0)
  11.                 a++;
  12.             for(long m=a;m<=b;m+=2)
  13.         {
  14.                 int sqrtm=(int)sqrt(m);
  15.                 int i;
  16.                 for(i=2;i<=sqrtm;i++)
  17.                 {
  18.                         if(m%i==0)
  19.                                 break;
  20.                 }
  21.                
  22.                 if(i>sqrtm)
  23.                 {
  24.                         if(l++%10==0)
  25.                                 cout<<endl;
  26.                         cout<<setw(5)<<m;
  27.                 }
  28.         }
  29. }
复制代码
改完以后,基本正确,但是如果输入的a从2开始的话,会漏掉素数2。另外效率依然有点提高。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-12-26 16:52:56 | 显示全部楼层
求某一范围内的素数用筛法,而不用试除法。试除法太慢了。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 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.按运行键
出运行结果了。
不知错在何处?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-12-27 21:01:55 | 显示全部楼层
下面是我的错误信息:
--------------------Configuration: prime0001 - Win32 Debug--------------------
Compiling...
Error spawning cl.exe

prime0001.obj - 1 error(s), 0 warning(s)
请指出改正的地方,谢了
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2008-12-27 21:19:07 | 显示全部楼层
你VC装得有问题.
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 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
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2008-12-28 22:35:02 | 显示全部楼层
请问筛法,与试除法有何不同?
筛法的求某一范围内的素数的代码如何写?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-4-20 21:57 , Processed in 0.047739 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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