qq_41829465 2018-11-03 06:15 采纳率: 57.1%
浏览 331
已采纳

c程序新手 想问问这个问题怎么写?

图片说明

 #include<stdio.h>
int main ()
{
    int i,j,s,b,c;
    printf("Input num:");
    scanf("%d",&s);
    if(s<2)
    {
        printf("Prime number is 2");
    }
    else if(s==2)
    {
        printf("Prime number is 3");
    }
    else
    {
        while(b==i)
        {
           i=s++;
           for(j=2;j<i;j++)
           if(i%j==0)break;
           if(j=i)
               b=i;
        }
        while(c==i)
        {
           i=s--;
           for(j=2;j<i;j++)
           if(i%j==0)break;
           if(j=i)
               c=i;
        } 
     if((b-s)>(s-c))
     {
         printf("Prime number is %d",b);
     }
     else if((b-s)==(s-c))
     { 
      printf("Prime number is %d %d",b,c);
     }
     else
     {
         printf("Prime number is %d",c);
     }
    }

     return 0;
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2018-11-03 06:51
    关注

    你的程序的循环错了,给你修改了下

    // Q707861.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include<stdio.h>
    int main ()
    {
        int i,j,s,b,c;
        printf("Input num:");
        scanf("%d",&s);
        if(s<2)
        {
            printf("Prime number is 2\n");
        }
        else if(s==2)
        {
            printf("Prime number is 3\n");
        }
        else
        {
            i = s+1;
            while (true)
            {
                for(j=2;j<i;j++)
                    if(i%j==0)break;
                if(j==i)
                {
                    b=i;
                    break;
                }
               i++;
            }
            i = s-1;
            while (true)
            {
                for(j=2;j<i;j++)
                    if(i%j==0)break;
                if(j==i)
                {
                    c=i;
                    break;
                }
               i--;
            }
         if((b-s)<(s-c))
         {
             printf("Prime number is %d\n",b);
         }
         else if((b-s)==(s-c))
         { 
          printf("Prime number is %d %d\n",c,b);
         }
         else
         {
             printf("Prime number is %d\n",c);
         }
        }
    
         return 0;
    }
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改