《C和指针》——6.6

题目:

  在指定的下限、上限之间使用数组方法查找质数,并将质数提取出来。

要求:

  略

解答代码:

#include <stdio.h>

#define UPLIMIT        11000
#define DOWNLIMIT 10000
#define NUM UPLIMIT-DOWNLIMIT void show_array(int *p, int n) //显示数组p[]中的n个元素
{
int i;
for(i=; i<n; i++)
{
if ((i% == ) && (i!=))
printf("\n");
printf("%6d", *(p+i));
}
printf("\n");
} void def_array(int *p) //初始化数组元素值为序号+1
{
int i;
for(i=; (i+DOWNLIMIT)<(UPLIMIT); i++)
{
*(p+i) = i++DOWNLIMIT;
}
} void DataProcess(int *p) //将数组中的非质数替换为0
{
int i, j;
for(i=; i<=(UPLIMIT/); i++)
{
for(j=; j<NUM; j++)
{
if ((*(p+j) >= ) && (*(p+j) != i) && (*(p+j) % i == ))
{
*(p+j) = ;
}
}
}
} int DataSelect(int *p) //将数组中的所有0去掉
{
int i, j;
for(i=, j=; i<NUM; i++)
{
if(*(p+i) > )
{
*(p+j) = *(p+i);
j++;
}
}
return j;
} int main(void)
{
int arrayt[NUM];
int *p = arrayt;
int n = ; def_array(p); //初始化数组元素值为索引号+1
show_array(p, NUM); //显示数组p[]中的n个元素
DataProcess(p); //将数组中的非质数替换为0
n = DataSelect(p); //将数组中的所有0去掉,n为质数的个数
printf("There are %d numbers:\n", n);
show_array(p, n); getchar();
return ;
}

注:

  下限和上限可以设置

《C和指针》章节后编程练习解答参考——6.6的更多相关文章

  1. 《C和指针》章节后编程练习解答参考——6.2

    <C和指针>——6.2 题目: 编写一个函数,删除源字符串中含有的子字符串部分. 函数原型: int del_substr(char *str, char const *substr); ...

  2. 《C和指针》章节后编程练习解答参考——6.3

    <C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...

  3. 《C和指针》章节后编程练习解答参考——第5章

    5.1 题目: 略 解答代码: #include <stdio.h> int main(void) { char ch; while (((ch = getchar()) != EOF) ...

  4. 《C和指针》章节后编程练习解答参考——6.4

    <C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...

  5. 《C和指针》章节后编程练习解答参考——6.1

    <C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...

  6. 《C和指针》章节后编程练习解答参考——第10章

    10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...

  7. 《C和指针》章节后编程练习解答参考——第9章

    9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...

  8. 《C和指针》章节后编程练习解答参考——第8章

    8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...

  9. DSAPI多功能组件编程应用-参考-Win32API常数

    DSAPI多功能组件编程应用-参考-Win32API常数 在编程过程中,常常需要使用Win32API来实现一些特定功能,而Win32API又往往需要使用一些API常数,百度搜索常数值,查手册,也就成了 ...

随机推荐

  1. 【JAVA - SSM】之MyBatis与原生JDBC、Hibernate访问数据库的比较

    首先来看一下原生JDBC访问数据库的代码: public static void main(String[] args) { // 数据库连接 Connection connection = null ...

  2. SQL SERVER 查询死锁

    USE mastergo CREATE PROCEDURE [dbo].[sp_who_lock]AS     BEGIN        DECLARE @spid INT ,             ...

  3. 用C#实现通用守护进程

    1. 下载 源码下载:http://pan.baidu.com/s/1vqDA2 安装包下载:http://pan.baidu.com/s/1sjmEB0p 2. 安装注意事项 在配置档中配置你要守护 ...

  4. Web缩放

    可以这么写,这个网页缩放了 同时滚动条也被绽放了,明显结果不正确 webBrowser1.Document.Body.Style = "zoom:200%"; 正解 http:// ...

  5. linux kernel (proc文件系统)参数

    http://blog.csdn.net/guowake/article/details/3279796 Linux Proc文件系统,通过对Proc文件系统进行调整,达到性能优化的目的. 二./pr ...

  6. 3 Ways of JDK Source Code Attachment in Eclipse---reference

    You wanna look at a JVM class while you are coding and you cannot. Here is the solution. First of al ...

  7. .net+easyui系列--验证框

    1.允许从 0 到 10个字符 <input id="vv" class="easyui-validatebox" data-options=" ...

  8. .NET生成PDF文件

    C#未借助第三方组件,自己封装通用类,生成PDF文件. 调用方式: //路径 string path = @"C:\yuannwu22.pdf"; //内容 string strC ...

  9. SqlSugar-执行Sql语句查询实例

    使用SqlSugar执行sql语句 1.简单查询 SqlSugarClient db = SugarContext.GetInstance(); //执行sql语句,处理 //1.执行sql,转成li ...

  10. [转]深入理解JavaScript系列

    文章转自:汤姆大叔-深入理解JavaScript系列文章 深入理解JavaScript系列文章,包括了原创,翻译,转载,整理等各类型文章,如果对你有用,请推荐支持一把,给大叔写作的动力. 深入理解Ja ...