| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 551 人关注过本帖
标题:用函数写100—200之间的素数,哪里错了~望高手指正
只看楼主 加入收藏
kidundead
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2009-8-21
结帖率:100%
收藏
 问题点数:0 回复次数:5 
用函数写100—200之间的素数,哪里错了~望高手指正
程序代码:
#include "stdio.h"
#include "math.h"
void main()
{
    int k,i;
    k=1;
    int sushu(int x);
    for(i=100;i<=200;i++,k++)
       {
            if(k%10==0)
            printf("\n");
         sushu(i);
       }
}
  void sushu(int x)
  {
      int n,j;
      n=sqrt(x);
      for(j=2;j<=n;j++)
         {
               if(x%j==0)
                 break;
         }
       if(j>n)
         printf("a prime number");
       else
         printf("not a prime number");
  }
  改了,lx的~也运行不了,不知道怎么搞~

[ 本帖最后由 kidundead 于 2009-10-17 17:13 编辑 ]
搜索更多相关主题的帖子: 函数 素数 
2009-10-17 16:06
zodiac207
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2009-10-14
收藏
得分:0 
你想说的是哪错了?
2009-10-17 16:49
zodiac207
Rank: 2
等 级:论坛游民
帖 子:16
专家分:38
注 册:2009-10-14
收藏
得分:0 
#include "stdio.h"
#include "math.h"
void main()
{
    int k,i;
    k=1;
    void sushu(int x); //你定义与下面的函数的类型不一样,可改成VOID
    for(i=100;i<=200;i++,k++)
       {
            if(k%10==0)
            printf("\n");
         sushu(i);
       }
}
  void sushu(int x)
  {
      int n,j;
      n=(int)sqrt(x); //将这里强制转换成整型
      for(j=2;j<=n;j++)
         {
               if(x%j==0)
                 break;
         }
       if(j>n)
         printf("a prime number");
       else
         printf("not a prime number");
  }
2009-10-17 16:54
飞天的猪
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:141
注 册:2009-9-19
收藏
得分:0 
楼上程序运行结果
not a prime numbera prime numbernot a prime numbera prime numbernot a prime numb
ernot a prime numbernot a prime numbera prime numbernot a prime number
a prime numbernot a prime numbernot a prime numbernot a prime numbera prime numb
ernot a prime numbernot a prime numbernot a prime numbernot a prime numbernot a
prime number
not a prime numbernot a prime numbernot a prime numbernot a prime numbernot a pr
ime numbernot a prime numbernot a prime numbernot a prime numbera prime numberno
t a prime number
not a prime numbernot a prime numbera prime numbernot a prime numbernot a prime
numbernot a prime numbernot a prime numbernot a prime numbera prime numbernot a
prime number
a prime numbernot a prime numbernot a prime numbernot a prime numbernot a prime
numbernot a prime numbernot a prime numbernot a prime numbernot a prime numberno
t a prime number
a prime numbernot a prime numbera prime numbernot a prime numbernot a prime numb
ernot a prime numbernot a prime numbernot a prime numbera prime numbernot a prim
e number
not a prime numbernot a prime numbernot a prime numbernot a prime numbera prime
numbernot a prime numbernot a prime numbernot a prime numbera prime numbernot a
prime number
not a prime numbernot a prime numbernot a prime numbernot a prime numbera prime
numbernot a prime numbernot a prime numbernot a prime numbernot a prime numberno
t a prime number
a prime numbernot a prime numbera prime numbernot a prime numbernot a prime numb
ernot a prime numbernot a prime numbernot a prime numbernot a prime numbernot a
prime number
not a prime numbernot a prime numbera prime numbernot a prime numbera prime numb
ernot a prime numbernot a prime numbernot a prime numbera prime numbernot a prim
e number
a prime numbernot a prime numberPress any key to continue
出现错误!
2009-10-17 23:50
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:0 
#include "stdio.h"  
#include "math.h"  
#include<conio.h>
void main()  
{     void sushu(int x); //声明应该该写在这里
    int k,i;  
    k=1;  
 
    for(i=100;i<=200;i++,k++)  
       {  
            if(k%10==0)  
            printf("\n");  
         sushu(i);  
       }  
       getch();
   }
  void sushu(int x)  
  {  
      int n,j;  
      n=(int)sqrt(x);
      for(j=2;j<=n;j++)  
         {  
               if(x%j==0)  
                 printf("%d ",x);
         }  
    /*   if(j>n)
         printf("a prime number");  
       else  
         printf("not a prime number");  */
 
  } 这样还行。

离恨恰如春草,更行更远还生。
2009-10-18 02:13
hzyzxj
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:168
注 册:2009-6-14
收藏
得分:0 
试试这个。
#include "stdio.h"
#include "math.h"
void main()
{
    int k,i;
    k=1;
    void sushu(int x);
    for(i=100;i<=200;i++,k++)
       {
            if(k%10==0)
            printf("\n");
         sushu(i);
       }
}
  void sushu(int x)
  {
      int n,j;
      n=sqrt(x);
      for(j=2;j<=n;j++)
         {
               if(x%j==0)
                 break;
         }
       if(j>n)
         printf("%d is a prime number     ",x);
       else
         printf("%d is not a prime number     ",x);
  }
2009-10-18 09:03
快速回复:用函数写100—200之间的素数,哪里错了~望高手指正
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016718 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved