鱼C论坛

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

求第1000个合数

[复制链接]
发表于 2012-4-27 20:29:12 | 显示全部楼层 |阅读模式
1鱼币
求第1000个合数C语言或VBS脚本代码的解法,最好的事VBS

最佳答案

查看完整内容

//C代码 #include "stdio.h" int main() { int i=3,j,num=0;//num合数的个数 while(1) { i++; for(j=2;j
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-4-27 20:29:13 | 显示全部楼层
本帖最后由 NetBoy 于 2012-4-28 23:19 编辑

//C代码
#include "stdio.h"
int main()
{
        int i=3,j,num=0;//num合数的个数
        while(1)
        {
                i++;
                for(j=2;j<i;j++)
                  if(i%j==0){   //如果是合数 num加1后跳出for循环
                          num++;
                          break;
                  }
                  if(num==1000){//当数目为1000时,输出
                          printf("Num=%d\n",i);
                          break;
                  }         
        }
        return 0;
}

//VBS代码
dim i,j,num
i=3:num=0
do
  i=i+1
  for j = 2 to i-1
    if i mod j=0 then
      num = num+1
      exit for
    end if
  next
  if(num=1000) then
     msgbox "第1000个合数为:"&i&"."
     exit do
  end if
loop

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

使用道具 举报

发表于 2012-4-27 23:53:02 | 显示全部楼层
/*
目的:求第一千个合数
*/
# include <stdio.h>
m=0;
int he(int n)
{
    int i;   
    for(i=2;i<n;i++)
    {
        if(0==n%i)
        {
            m++;
            break;
        }
    }
    return m ;
}
int main()
{
    int j=3, s;
    while(1)
    {
        
        s = he(j);
        if(s==1000) /*1不是素数也不是合数*/
        {
            break;
        }
        j++;
        
    }
   
    printf("第一千个合数是%d\n" ,j);
   
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-4-30 20:22:46 | 显示全部楼层
路过,看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-13 02:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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