鱼C论坛

 找回密码
 立即注册
查看: 1293|回复: 3

[已解决]10000以内的素数

[复制链接]
发表于 2021-7-14 12:43:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
#include <stdio.h>
#include <math.h>
int main ()
{
        long long int  num=0 ,i=2,j=2,ch;
        for (i=2;i++;i<10000)
        {
                ch=(int)sqrt(i);
                for (j=2;j++;j<ch)
                {
                        if(i%j==0)
                        {
                                num++;
                        }
                }
        }
        printf("10000以内有%d个合数",num);
}
编译没问题,但是代码哪里错了,为什么运行不出来
最佳答案
2021-7-14 12:57:19
for 循环写错了
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main ()
  4. {
  5.         long long int num=0 ,i=2,j=2,ch;
  6.         for (i=2;i<10000;i++)
  7.         {
  8.                 ch=(int)sqrt(i);
  9.                 for (j=2;j<ch;j++)
  10.                 {
  11.                         if(i%j==0)
  12.                         {
  13.                                 num++;
  14.                         }
  15.                 }
  16.         }
  17.         printf("10000以内有%d个合数",num);
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-14 12:57:19 | 显示全部楼层    本楼为最佳答案   
for 循环写错了
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main ()
  4. {
  5.         long long int num=0 ,i=2,j=2,ch;
  6.         for (i=2;i<10000;i++)
  7.         {
  8.                 ch=(int)sqrt(i);
  9.                 for (j=2;j<ch;j++)
  10.                 {
  11.                         if(i%j==0)
  12.                         {
  13.                                 num++;
  14.                         }
  15.                 }
  16.         }
  17.         printf("10000以内有%d个合数",num);
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-14 13:00:43 | 显示全部楼层

for 循环错了,而且第二个 for 循环应该 <= ch ,if 下面要加个 break

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main ()
  4. {
  5.     long long int  num=0 ,i=2,j=2,ch;
  6.     for (i=2;i<10000;i++)
  7.     {
  8.         ch=(int)sqrt(i);
  9.         for (j=2;j<=ch;j++)
  10.         {
  11.             if(i%j==0)
  12.             {
  13.                 num++;
  14.                 break;
  15.             }
  16.         }
  17.     }
  18.     printf("10000以内有%d个合数",num);
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-14 18:08:26 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 05:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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