《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. jetty-如何配置虚拟主机【转】

    jetty-如何配置虚拟主机[转]http://weifly.iteye.com/blog/1152688 官方配置:http://wiki.eclipse.org/Jetty/Howto/Confi ...

  2. 20169210《Linux内核原理与分析》第十周作业

    第一部分:实验 进程的调度时机与进程的切换 操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已. 对于理解操作系统的工作机制 ...

  3. hdu4488 Faulhaber’s Triangle(模拟题)

    Faulhaber’s Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. PowerMock注解PowerMockIgnore的使用方法

    故事要从一个异常开始,某天我在开发一个加密.解密特性,算法使用的是3DES,样例代码如下. package org.jackie.study.powermock; import java.io.Uns ...

  5. PAT---1050. String Subtraction (20)

    #include<iostream> #include<string.h> #include<stdio.h> using namespace std; #defi ...

  6. 二分PKU3273

    <span style="color:#3333ff;">/* F - 二分 Time Limit:2000MS Memory Limit:65536KB 64bit ...

  7. Openstack Ice-House 版本号说明--之中的一个 NOVA

    OpenStack Icehouse在4.17正式公布,看了下release note,发现改变不小,说明openstack还是在高速发展中,有不少新的特性增加,也有些小的剔除.以下就我所关注的项目做 ...

  8. Run-Time Check Failure #2 - Stack around the variable 'ucPriKey' was corrupt

    Run-Time    Check    Failure    #2        一般是栈被破坏,你的代码可能有缓冲区溢出一类的问题. Run-Time Check Failure #2 - Sta ...

  9. oracle9

    约束 维护数据的完整性 数据的完整性用于确保数据库数据遵从一定的商业和逻辑规则(比如年纪不能为-,性别不能为非男女),在oracle中,数据完整性可以使用约束.触发器.应用程序(过程.函数)三种方法来 ...

  10. myeclipse设置技巧

    如何设置jsp的默认打开为MyEclipse JSP Editor? windows -> General -> Editor - > File Associations 选择 *. ...