zoukankan      html  css  js  c++  java
  • C语言:找出一个大于给定整数m且紧随m的素数,-求出能整除x且不是偶数的数的个数,

    //函数fun功能:找出一个大于给定整数m且紧随m的素数,并作为函数值返回。

     1 #include  <stdlib.h>
     2 #include  <conio.h>
     3 #include  <stdio.h>
     4 int fun( int m)
     5 { int i,k;
     6   for (i=m+1; ;i++)//取大于m的逐个数
     7      { for (k=2;k<i;k++)//判断是否为素数质数
     8 /*************found**************/
     9           if (i%k==0)
    10           break;
    11 /*************found**************/
    12         if (k==i)//判断ki是不是相等。
    13         return(i);
    14      }
    15 }
    16 void main()
    17 {  int n;
    18    system("CLS");
    19    printf("
    Please enter n: ");
    20    scanf("%d",&n);
    21    printf ("%d
    ",fun(n));
    22 }

    //函数fun功能:求出能整除x且不是偶数的数的个数,并按从小到大的顺序放在pp所指的数组中,个数通过形参n返回。

     1 #include <conio.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 
     5 void fun (int x, int pp[], int *n)
     6 {
     7     int j = 0;
     8     for (int i = 1; i <= x; i++)//切记这里i不能等于0
     9     {
    10         if ((x%i == 0) && (i % 2 != 0))
    11         {
    12             pp[j++] = i;
    13         }
    14     }
    15     //n=&j;//这种方式不可以
    16     *n = j;
    17 }
    18 
    19 void main ()
    20 { 
    21   FILE *wf;
    22   int  x,aa[1000], n, i ;
    23   system("CLS");
    24   printf("
    Please enter an integer number : 
     ") ;
    25   scanf ("%d", &x) ;
    26   fun (x, aa, &n) ;
    27   for (i=0 ; i<n ; i++)
    28       printf ("%d ", aa [i]);
    29   printf ("
     ") ;
    30 /******************************/
    31   wf=fopen("out.dat","w");
    32   fun (30, aa, &n) ;
    33   for (i=0 ; i<n ; i++)
    34       fprintf (wf,"%d ", aa [i]);
    35   fclose(wf);
    36 /*****************************/
    37 }
  • 相关阅读:
    linux环境变量(一)
    linux常用命令-ps
    linux实用小命令--查看文本内容
    postman tests常用方法
    Vue 中嵌套 Iframe,用postMessage通信 踩坑记
    [Vue warn]: Error in nextTick: "RangeError: Maximum call stack size exceeded"
    对STM32所用位带操作宏的详细剖析
    移植Modbus TCP二
    移植Modbus TCP一
    STM32位带操作
  • 原文地址:https://www.cnblogs.com/ming-4/p/10491254.html
Copyright © 2011-2022 走看看