《C和指针》章节后编程练习解答参考——6.6
《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的更多相关文章
- 《C和指针》章节后编程练习解答参考——6.2
<C和指针>——6.2 题目: 编写一个函数,删除源字符串中含有的子字符串部分. 函数原型: int del_substr(char *str, char const *substr); ...
- 《C和指针》章节后编程练习解答参考——6.3
<C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...
- 《C和指针》章节后编程练习解答参考——第5章
5.1 题目: 略 解答代码: #include <stdio.h> int main(void) { char ch; while (((ch = getchar()) != EOF) ...
- 《C和指针》章节后编程练习解答参考——6.4
<C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...
- 《C和指针》章节后编程练习解答参考——6.1
<C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...
- 《C和指针》章节后编程练习解答参考——第10章
10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...
- 《C和指针》章节后编程练习解答参考——第9章
9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...
- 《C和指针》章节后编程练习解答参考——第8章
8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...
- DSAPI多功能组件编程应用-参考-Win32API常数
DSAPI多功能组件编程应用-参考-Win32API常数 在编程过程中,常常需要使用Win32API来实现一些特定功能,而Win32API又往往需要使用一些API常数,百度搜索常数值,查手册,也就成了 ...
随机推荐
- ios xcode如何在控制台打印frame
进入正题 打开终端输入三条命令: 1. touch ~/.lldbinit 2. echo display @import UIKit >> ~/.lldbinit 3. echo tar ...
- 【转】Eclipse上安装GIT插件EGit及使用
http://yufenfei.iteye.com/blog/1750124 一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32 ...
- adt-bundle-linux-x86_64-20131030下新建project提示找不到adb和R.java问题的解决
adt-bundle-linux-x86_64-20131030下新建project提示找不到adb和R.java问题的解决 在ubuntu14.04下,搭建Android开发环境,下载官方的adt- ...
- PrintWriter的print和write方法(转)
public void print(String s) {if (s == null) {s = "null";}write(s); } print只是先对s==null转换为 ...
- 用C语言扩展Python的功能
https://www.ibm.com/developerworks/cn/linux/l-pythc/
- one problem about Apple Keychain in use
解决方案 Add Security.framework, then rebuild. Sometimes I find I have to build clean and then rebuild. ...
- IntelliJ插件安装
1. 插件管理器在线安装 在IntelliJ插件管理页面([FileàSettingsàIDE SettingsàPlugins]),点击[Browse repositories-]按钮,在搜索框内输 ...
- 超过2T,磁盘分区
MBR:MBR分区表(即主引导记录)大家都很熟悉.所支持的最大卷:2T,而且对分区有限制:最多4个主分区或3个主分区加一个扩展分区 GPT: GPT(即GUID分区表).是源自EFI标准的一种较新的磁 ...
- nginx之服务器
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- C# Java DES加密解密
转自http://www.cnblogs.com/zhuiyi/archive/2013/04/01/2993201.html 最近被DES加解密弄得超级郁闷,我用C#的方法加密得到的密文老是跟客户给 ...