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 ...
随机推荐
- UVALive 4864 Bit Counting --记忆化搜索 / 数位DP?
题目链接: 题目链接 题意:如果一个数二进制n有k位1,那么f1[n] = k,如果k有s位二进制1,那么f2[n] = f1[k] = s. 如此往复,直到fx[n] = 1,此时的x就是n的”K ...
- 洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game
题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...
- CHM打不开的解决方法
CHM打不开的解决方法 听语音 | 浏览:62240 | 更新:2013-02-04 14:58 | 标签:软件 1 2 3 4 5 6 分步阅读 一键约师傅 百度师傅高质屏和好师傅,拯救你的碎屏机 ...
- Mac如何找到从AppStore下载的正版Xcode安装包
前言:本文介绍在Mac下如何找到AppStore下载的安装包路径,以及如何提取出来供以后使用,希望对大家有所帮助(前提:想要提取某个安装包,前提是你正在从AppStore安装这个程序.比如你想提取im ...
- mac os 下搭建android开发环境
mac os 下搭建android开发环境 周银辉 mac os 下搭建android环境比较方便, 如下几个步骤: 1,安装jdk 先搞清楚自己是否已经安装,在命令行下:java -version, ...
- scp 上传文件到多个服务器节点
参考:scp批量上传文件到多台机器上(升级版) 实测,代码可运行. 1.如果遇到syntax error near unexpected token问题,基本是由于windows环境下编写的shell ...
- 在WebAPI使用Session
最近在改写WebApp时要将以前用泛型处理例程写的Captcha 改成使用WebApi 来实作机制,在实作的过程中发现使用IRequiresSessionState session也无法使用(cont ...
- asp.net mvc ajax 异步刷新例子
这几天在asp.net中使用ajax来做异步刷新,这里整理一下 1.首先看前台页面点击的时候调用函数 function shuxin() { $.ajax( { url: "GetValue ...
- 数学的东西(BZOJ1951)
#include <cstdio> #define LL long long LL finmo=; LL fac[][],inv[][]; LL tmp[],rev[]; LL n,g,x ...
- 实现简单sed替换功能的python脚本
#可以用来修改配置文件参数 # -*- coding:utf-8 -*- import os,sys old = sys.argv[1] new = sys.argv[2] file = sys.ar ...