首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

计算素数的程序,能否不使用goto语句而得到更简练的程序?该如何处理

2012-02-09 
计算素数的程序,能否不使用goto语句而得到更简练的程序?C/C++ code#includemath.h#includestdio.hmain

计算素数的程序,能否不使用goto语句而得到更简练的程序?

C/C++ code
#include<math.h>#include<stdio.h>main(){    int i,j;    for (i=2;i<100;i++)    {        for (j=2;j<=sqrt(i);j++)        {            if (i%j==0) goto loop;        }        printf("%d ",i);        loop:;    }}


[解决办法]
C/C++ code
#include<math.h>#include<stdio.h>int main(){    int i,j;    for (i=2;i<100;i++)    {        for (j=2;j<=sqrt(i);j++)        {            if (i%j==0)             {                break;            }        }        if(j>sqrt(i))printf("%d ",i);    }    return 0;} 

热点排行