C和指针 第四章 习题
4.1正数的n的平方根可以通过:
ai+1= (ai + n / ai ) / 2
得到,第一个a1是1,结果会越来越精确。
#include <stdio.h> int main()
{
double input;
double exp;
scanf_s("%lf", &input); double aBefore = 1;
double aNow = (aBefore + input / aBefore) / 2; exp = aBefore - aNow;
exp = exp < 0 ? -exp : exp;
printf("aBefore: %lf, aNow: %lf, exp: %f\n\n", aBefore, aNow, exp);
while (exp > 0.000001) {
aBefore = aNow;
aNow = (aBefore + input / aBefore) / 2;
exp = aBefore - aNow;
exp = exp < 0 ? -exp : exp;
printf("aBefore: %lf, aNow: %lf, exp: %lf\n", aBefore, aNow, exp);
} return 0;
}

4.2 打印100以内的质数
因为2* 50 和 50 *2一样,如果按照1 2 3 4 一直遍历到目标的数其实有很多重复,事实上只需要计算到这个数的平方根即可停止。
#include <stdio.h>
#include <math.h> #define TRUE 1
#define FALSE 0 int isPrimer(int num)
{
int idx;
int end = floor(sqrt(num)) + 1;
for (idx = 2; idx <= end ; idx++)
{
if (num % idx == 0) {
return FALSE;
}
}
return TRUE;
}
int main()
{
int num;
for (num = 1; num <= 100; num++)
{
if (isPrimer(num)) {
printf("%d ", num);
}
} return 0;
}

4.7去除字符串中多余的空格
#include <stdio.h> void trim(char str[])
{
//判断之前是否在空格中
int inEmpty = 0;
//字符串下标
int idx = 0; //循环字符串
while (str[idx] != '\0') {
//遇到空格
if (str[idx] == ' ' || str[idx] == '\t' || str[idx] == '\n') {
//如果之前不是空格,设置空格状态为1
if (!inEmpty) {
inEmpty = 1;
idx++;
}else{
//如果之前是空格将之后的字符全部前移一位
int len = strlen(str);
for (int movStart = idx; movStart <= len; movStart++) {
str[movStart] = str[movStart + 1];
}
}
}else {
//没遇到空格需要恢复非空格状态
inEmpty = 0;
idx++;
}
}
} int main()
{
char name[] = " this is my name";
printf("%s\n", name);
trim(name);
printf("%s\n", name); return 0;
}

C和指针 第四章 习题的更多相关文章
- 统计学习导论:基于R应用——第四章习题
第四章习题,部分题目未给出答案 1. 这个题比较简单,有高中生推导水平的应该不难. 2~3证明题,略 4. (a) 这个问题问我略困惑,答案怎么直接写出来了,难道不是10%么 (b) 这个答案是(0. ...
- PythonCrashCourse 第四章习题
Python 从入门到实践第四章习题 4.1想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅 ...
- C和指针 第十四章 习题
14.1 打印函数 #include <stdio.h> void print_ledger_long(){ printf("function print_ledger_long ...
- C和指针 第六章 习题
6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...
- C和指针 第十七章 习题
17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...
- C和指针 第十三章 习题
1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...
- C和指针 第十一章 习题
1编写calloc,内部使用malloc函数获取内存 #include <stdio.h> #include <stdlib.h> void *myAlloc(unsigned ...
- C和指针 第七章 习题
7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...
- 《学习OpenCV》 第四章 习题六
实现的是一个图像标签编辑器,其间遇到了些问题还未解决或者可能解决方法上不是最优,若你有更好的思路可以提供给我,大恩不言谢啦!!☆⌒(*^-゜)v. #include "stdafx.h&qu ...
随机推荐
- HAOI2015 泛做
T1 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两之间的 ...
- Warning: file_put_contents(data.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/test.php on line 22
最近在学习PHP 在保存文件的时候报Warning: file_put_contents(data.txt): failed to open stream: Permission denied in ...
- fMRI: spatial smoothing
Source: Brain voyager support Theoretical Background Spatial smoothing means that data points are av ...
- SeaJS入门篇一 怎么使用query
研究了2个多小时,终于搞定引入jq 1.写个html页面 <h1>ddddddddd</h1> <script src='/Scripts/dist/sea.js'> ...
- AppBox升级进行时 - 扁平化的权限设计
AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. AppBox v2.0中的权限实现 AppBox v2.0中权限管理中涉及三个 ...
- 【knockout】ko绑定click事件传多个参数,
源:http://knockoutjs.com/documentation/event-binding.html <a href="javascript:;" class=& ...
- 在c#中使用指针
如果想在c#中使用指针,首先对项目进行配置:在解决方案资源管理器中右击项目名选择属性(或在项目菜单中选择consoleApplication属性(consoleApplication为项名)),在生成 ...
- 链队列java实现
public class LinkHeap<T> { class Node<T> { T data; Node<T> next; Node(T data) { th ...
- TortoiseGit的使用教程(转)
1.windows安装TortoiseGit详细使用教程[基础篇] http://www.mamicode.com/info-detail-423481.html 2.Windows下Git与Tort ...
- javaweb局部刷新-ajax异步请求springMVC显示返回的jsp内容,代替iframe
在jsp上要引入jquery <script src="<%=request.getContextPath()%>/js/jquery_ui/jquery.js" ...