4.1正数的n的平方根可以通过:

ai+1= (a+ n / a) / 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和指针 第四章 习题的更多相关文章

  1. 统计学习导论:基于R应用——第四章习题

    第四章习题,部分题目未给出答案 1. 这个题比较简单,有高中生推导水平的应该不难. 2~3证明题,略 4. (a) 这个问题问我略困惑,答案怎么直接写出来了,难道不是10%么 (b) 这个答案是(0. ...

  2. PythonCrashCourse 第四章习题

    Python 从入门到实践第四章习题 4.1想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅 ...

  3. C和指针 第十四章 习题

    14.1 打印函数 #include <stdio.h> void print_ledger_long(){ printf("function print_ledger_long ...

  4. C和指针 第六章 习题

    6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...

  5. C和指针 第十七章 习题

    17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...

  6. C和指针 第十三章 习题

    1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...

  7. C和指针 第十一章 习题

    1编写calloc,内部使用malloc函数获取内存 #include <stdio.h> #include <stdlib.h> void *myAlloc(unsigned ...

  8. C和指针 第七章 习题

    7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...

  9. 《学习OpenCV》 第四章 习题六

    实现的是一个图像标签编辑器,其间遇到了些问题还未解决或者可能解决方法上不是最优,若你有更好的思路可以提供给我,大恩不言谢啦!!☆⌒(*^-゜)v. #include "stdafx.h&qu ...

随机推荐

  1. [转载]彻底弄清struct和typedef struct

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  2. 初识Quartz(入门案例)+常用的Cron表达式

    1.Quartz架构图 1.实体层 package cn.happy.entity; //1. public class Plan { //时间 private String date; //任务 p ...

  3. python高级之多线程

    python高级之多线程 本节内容 线程与进程定义及区别 python全局解释器锁 线程的定义及使用 互斥锁 线程死锁和递归锁 条件变量同步(Condition) 同步条件(Event) 信号量 队列 ...

  4. PAT 1032. 挖掘机技术哪家强(20)

    为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位 ...

  5. ajax实现下拉菜单无刷新加载更多

    $(function() { var page = 1; var discount = $('#discount'); var innerHeight = window.innerHeight; va ...

  6. 关于javascript中apply()和call()方法的区别

    如果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的事偏偏就发生了,甚至觉得不可理喻.如果在学JavaScript这自由而变幻无穷的语 ...

  7. 在Windows Server 2012 R2上安装SharePoint 2013 with SP1失败,提示没有.net4.5的解决办法

    现在的Server用Windows Server 2012 R2的越来越多了,在部署带Sp1的SharePoint2013的时候,走完预安装工具后,点击setup提示缺少.net4.5. 其实Wind ...

  8. MyBatis的Mapper文件的foreach标签详解

    MyBatis的Mapper文件的foreach标签用来迭代用户传递过来的Lise或者Array,让后根据迭代来拼凑或者批量处理数据.如:使用foreach来拼接in子语句. 在学习MyBatis M ...

  9. Leetcode 230. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  10. MapReduce实现手机上网日志分析(排序)

    一.背景 1.1 流程 实现排序,分组拍上一篇通过Partitioner实现了. 实现接口,自动产生接口方法,写属性,产生getter和setter,序列化和反序列化属性,写比较方法,重写toStri ...