鱼C论坛

 找回密码
 立即注册
查看: 4869|回复: 8

一些简单的C语言练习给鱼油们锻炼下,有奖励的哦。

[复制链接]
发表于 2012-12-2 00:55:30 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 lqyha 于 2012-12-2 23:52 编辑

题目:判断101-200之间有多少个素数,并输出所有素数。(C语言代码)
要求自己写,不能抄袭。。。如果代码写得好有奖励的哦限时一天,明天换新的题目!!
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-12-2 00:55:31 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <assert.h>

  4. void outputPrimes( int begin, int end );
  5. int isPrime( int n );
  6. int main(void) {
  7.         outputPrimes( 101, 200 );
  8.         return 0;
  9. }

  10. void outputPrimes( int begin, int end ) {
  11.         int i;
  12.         for( i = begin; i <= end; ++i )
  13.                 if( isPrime(i) ) printf("%d\n",i);
  14. }


  15. int isPrime( int n ) {
  16.         int i;
  17.         assert(n>=2);
  18.         for( i = 2; i<=sqrt(n); ++i )
  19.                 if( n%i==0 ) return 0;
  20.         return 1;
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-12-2 09:50:58 | 显示全部楼层
本帖最后由 zhaozhenwei 于 2012-12-2 10:03 编辑

程序一
#include<stdio.h>
void main()
{
int n,i,j;
for(i=101;i<=200;i++)
{
n=i;
for(j=2;j<=i;j++)
{
while(1)
{
if(i%j==0)
{
i=i/j;
}
else
break;
}

}
if(i==n)
printf("%5d",i);
i==n;
}
}

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-12-2 10:04:00 | 显示全部楼层
#include<stdio.h>
void main()
{
int n,i,j;
for(i=101;i<=200;i++)
{
n=i;
for(j=2;j<=i;j++)
{
while(1)
{
if(i%j==0)
{
i=i/j;
}
else
break;
}

}
if(i==n)
printf("%5d",i);
i==n;
}
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-12-2 10:42:26 | 显示全部楼层
#include <stdio.h>
main()
{
        int a,b,num=100;
        for (a=101;a<=200;a++)
        {
                for (b=2;b<a;b++)
                {
                        if (a%b==0)
                        {
                                num--;
                                break;
                        }

                }
        }
        printf("一共有素数:%d\n",num);
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-12-2 13:43:03 | 显示全部楼层
#include<stdio.h>
#include<math.h>
int main()
{
        int n,k,i,m=0;
        for(n=101;n<=200;n=n+2)
        {
                k=sqrt(n);
                for(i=2;i<=k;i++)
                        if(n%i==0)break;
                        if(i>=k+1)
                        {
                                printf("%d ",n);
                                m=m+1;
                        }
                        if(m%10==0)
                                printf("\n");
        }
   printf("\n");
   return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-12-2 23:54:36 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-12-2 23:56:13 | 显示全部楼层
殘影 发表于 2012-12-2 13:43
#include
#include
int main()

你这程序可以实现了题目的要求,但是格式不够规范化,还有希望下次写程序养成习惯,在一些重要步骤上写上注释。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-12-2 23:58:17 | 显示全部楼层

该程序实现了题目的要求,而且格式也可以。 使用函数调用来实现。就是没有在重要的步骤上标明注释。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-16 15:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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