第四章例题、心得及问题。

例题4-1:

#include<stdio.h>
#include<math.h>
int main(void)
{int denominator,flag;double item,pi;flag=1;denominator=1;item=1.0;pi=0;while(fabs(item)>=0.0001){item=flag*1.0/denominator;pi=pi+item;flag=-flag;denominator=denominator+2;}pi=pi*4;printf("pi=%.4f\n",pi);return 0;
}

 

例题4-2:

#include<stdio.h>
int main(void)
{int count,num;double grade,total;num=0;total=0;count=0;printf("Enter grades:");scanf("%lf",&grade);while(grade>=0){total=total+grade;num++;if(grade<60)count++;scanf("%lf",&grade);}if(num!=0){printf("Grade average is %.2f\n",total/num);printf("Number of failures is %d\n",count);}elseprintf("Grade average is 0\n");return 0;
}

 

例题4-3:

#include<stdio.h>
int main(void)
{int count,number;count=0;printf("Enter a number:");scanf("%d",&number);if(number<0)number=-number;do{number=number/10;count++;}while(number!=0);printf("It contains %d digits.\n",count);return 0;
}

运行结果1:

运行结果2:

 

例题4-4:

#include<stdio.h>
int main(void)
{int i,m;printf("Enter a number:");scanf("%d",&m);for(i=2;i<=m/2;i++)if(m%i == 0)break;if(i>m/2 && m!=1)printf("%d is a prime number!\n",m);elseprintf("No!\n");return 0;
}

运行结果1:

运行结果2:

 

例题4-5:

#include<stdio.h>
int main(void)
{int mynumber=44;int count=0,yournumber;for(count=1;count<=5;count++){printf("Input your number:");scanf("%d",&yournumber);if(yournumber==mynumber){printf("OK!you are right!\n");break;}elseif(yournumber>mynumber)printf("Sorry!your number is bigger than my number!\n");elseprintf("Sorry!your number is smaller than my number!\n");}printf("Game is over!\n");return 0;
}

 

例题4-6:

#include<stdio.h>
double fact (int n);
int main(void)
{int i;double sum;sum=0;for(i=1;i<=100;i++)sum=sum+fact(i);printf("1!+2!+...+100!=%e\n",sum);return 0;
}
double fact(int n)
{int i;double result;result=1;for(i=1;i<=n;i++)result=result*i;return result;
}

 

例题4-7:

#include<stdio.h>
int main(void)
{int i,j;double item,sum;sum=0;for(i=1;i<=100;i++){item=1;for(j=1;j<=i;j++)item=item*j;sum=sum+item;}printf("1!+2!+...+100!=%e\n",sum);return 0;
}

 

例题4-8-1:

#include<stdio.h>
int main(void)
{int i,mark,max,n;printf("Enter n:");scanf("%d",&n);printf("Enter %d marks:",n);scanf("%d",&mark);max=mark;for(i=1;i<n;i++){scanf("%d",&mark);if(max<mark)max=mark;}printf("Max=%d\n",max);return 0;
}

 

例题4-8-2:

#include<stdio.h>
int main(void)
{int mark,max;printf("Enter marks:");scanf("%d",&mark);max=mark;while(mark>=0){if(max<mark)max=mark;scanf("%d",&mark);};printf("Max = %d\n",max);return 0;
}

 

例题4-9:

#include<stdio.h>
int main(void)
{int x;printf("Enter x:");scanf("%d",&x);while(x!=0){printf("%d",x%10);x=x/10;}return 0;
}

 

例题4-10:

#include<stdio.h>
#include<math.h>
int main(void)
{int count,i,m,n;count=0;for(m=2;m<=100;m++){n=sqrt(m);for(i=2;i<=n;i++)if(m%i==0)break;if(i>n){printf("%6d",m);count++;if(count%10==0)printf("\n");}}printf("\n");return 0;
}

 

例题4-11:

#include<stdio.h>
int main(void)
{int i,x1,x2,x;x1=1;x2=1;printf("%6d%6d",x1,x2);for(i=1;i<=8;i++){x=x1+x2;printf("%6d",x);x1=x2;x2=x;}printf("\n");return 0;
}

 

例题4-12-1:

#include<stdio.h>
int main(void)
{int child,men,women;for(men=0;men<=45;men++)for(women=0;women<=45;women++)for(child=0;child<=45;child++)if(men+women+child==45&&men*3+women*2+child*0.5==45)printf("men=%d,women=%d,child=%d\n",men,women,child);return 0;
}

 

例题4-12-2:

#include<stdio.h>
int main(void)
{int child,men,women;for(men=0;men<=15;men++)for(women=0;women<=22;women++){child=45-women-men;if(men*3+women*2+child*0.5==45)printf("men=%d,women=%d,child=%d\n",men,women,child);}return 0;
}

 

 

 

第四章也完工了。

这一章主要是循环结构,可用for、while和do-while这三种循环语句来实现循环。

差别在于,若事先给定了循环次数,首选for语句,它最清晰。

如果循环次数不明确,通常选用while和do-while语句。

 

以上。

转载于:https://www.cnblogs.com/danson-daisy/p/3354369.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/292737.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springboot springcloud区别_SpringCloud微服务全家桶-第一篇!为什么要用微服务

从今天开始&#xff0c;学习SpringCloud微服务全家桶。一、引导1、什么是微服务&#xff1f;2、微服务之间是如何独立通讯的3、springCloud和Dubbo有哪些区别&#xff1f;4、什么是服务熔断&#xff1f;什么是服务降级5、微服务的优缺点分别是什么&#xff1f;6、微服务技术栈有…

[Todo] 乐观悲观锁,自旋互斥锁等等

乐观锁、悲观锁、要实践 http://chenzhou123520.iteye.com/blog/1860954 《mysql悲观锁总结和实践》 http://chenzhou123520.iteye.com/blog/1863407 《mysql乐观锁总结和实践》 http://outofmemory.cn/sql/optimistic-lock-and-pessimistic-lock 注意&#xff0c;以下的表里面…

Android之 ListView滑动时不加载图片

listview加载图片优化的功能&#xff0c; 在我们使用新浪微博的时候&#xff0c;细心的同学一定发现了&#xff0c;在滑动的过程中&#xff0c;图片是没有被加载的&#xff0c; 而是在滑动停止时&#xff0c;才加载图片了。 我们今天就做一个这样的效果吧。 我们先考虑两个问题…

C#之Lock

lock 关键字将语句块标记为临界区&#xff0c;方法是获取给定对象的互斥锁&#xff0c;执行语句&#xff0c;然后释放该锁。 class Program{static void Main(string[] args){Thread t new Thread(LockObject.MonitorIncrement);Thread t1new Thread(new ThreadStart(LockObje…

记一次 .NET 某消防物联网 后台服务 内存泄漏分析

一&#xff1a;背景 1. 讲故事去年十月份有位朋友从微信找到我&#xff0c;说他的程序内存要炸掉了。。。截图如下&#xff1a;时间有点久&#xff0c;图片都被清理了&#xff0c;不过有点讽刺的是&#xff0c;自己的程序本身就是做监控的&#xff0c;结果自己出了问题&#xf…

高性能网站建设的最佳实践(二)

原文译自雅虎开发者社区&#xff0c;转载译文请标明出处。关注我的sina微博&#xff0c;共同进步&#xff01;为了让网页响应速度更快Exceptional Performance团队列出了一系列的最佳实践&#xff0c;包括35个最佳实践条目&#xff0c;分成7种类型类。避免重定向标签&#xff1…

python带通配符的字符串匹配_Bash技巧:实例介绍数个参数扩展表达式以处理字符串变量...

Linux 的 bash shell 提供了多种形式的参数扩展表达式&#xff0c;可以获取变量自身的值&#xff0c;或者对变量值进行特定处理得到一个新的值&#xff0c;等等。本篇文章对字符串变量值相关的参数扩展表达式进行汇总说明。假设在 bash 中定义了 filepathexample/subdir/testfi…

幸福手机,给爸妈的高端大气上档次的手机

打造高端老人手机——幸福手机 江苏智联天地科技有限公司历经2年&#xff0c;手机研发投入超过4000万&#xff0c;打造中国第一品牌的高端老人手机&#xff0c;手机将于2014年12月正式对外发布&#xff0c;是国内第一款高端老人手机——幸福手机&#xff08;ThimFone&#xff0…

Fisher–Yates shuffle 算法

简单来说 Fisher–Yates shuffle 算法是一个用来将一个有限集合生成一个随机排列的算法(数组随机排序)。这个算法生成的随机排列是等概率的。同时这个算法非常高效。 Fisher–Yates shuffle 的原始版本,最初描述在 1938 年的 Ronald Fisher(上图) 和 Frank Yates 写的书中…

MASA Framework - EventBus设计

概述利用发布订阅模式来解耦不同架构层级&#xff0c;亦可用于解决隔离业务之间的交互优点&#xff1a;松耦合横切关注点可测试性事件驱动发布订阅模式发布者通过调度中心将消息发送给订阅者。调度中心解决发布与订阅者之间的关系&#xff0c;保证消息可以送达订阅者手中。发布…

Qt经验积累:常见的驱动打包处理方法

Qt对于常见的驱动打包,如数据库,图片等常用的插件驱动.如果处理不好,程序运行不起来,不能加载驱动,处理方法如下:在主程序中加入以下两句话:QString strLibPath(QDir::toNativeSeparators(QApplication::applicationDirPath())QDir::separator()"plugins"); a.addLib…

wireshark-win64-3.4.0安装_这9类轴承的安装方法,你可都知道?有哪些需要注意的呢?...

轴承是当代机械设备中一种重要零部件。随着时间的推移&#xff0c;轴承会发生磨损&#xff0c;合理的安装和使用可以让机械设备减少不必要的安全隐患。前面文章讲了如何拆卸轴承&#xff0c;今天就给大家讲讲各类轴承应该如何安装&#xff01;一、轴承安装前的准备工作轴承的安…

linux之which命令

我们经常在linux要查找某个文件&#xff0c;但不知道放在哪里了&#xff0c;可以使用下面的一些命令来搜索&#xff1a; which 查看可执行文件的位置。 whereis 查看文件的位置。 locate 配合数据库查看文件位置。 find 实际搜寻硬盘查询文…

slidingmenu能否实现菜单页在内容页上方,而不是把内容页挤到一边去????...

问题描述。。。。。。。。。。。。。。。。。。。。。。。。。。。。 解决方案1这样的话 你自己写不就好了 左边菜单view显示出来加一个动画 设置view的透明度 还用什么slidingmenu啊 解决方案2这个效果不是用的slidingmenu吧&#xff0c;自己实现一个 解决方案3对啊&#xff0…

史上最牛数学简史

全世界只有3.14 % 的人关注了爆炸吧知识“中国现代数学之父”华罗庚曾说过宇宙之大&#xff0c;粒子之微火箭之速&#xff0c;化工之巧地球之变&#xff0c;生物之谜日用之繁&#xff0c;无处不用数学回首往昔数学始终伴随我们左右纵横交错的几何、繁琐复杂的运算难以求解的方程…

log4net日志插件的使用

1、安装log4net2、引用3、配置&#xff08;web.config文件&#xff09;<configSections><section name"log4net" type"log4net.Config.Log4NetConfigurationSectionHandler, log4net"/></configSections><log4net><!--信息日志…

源代码提交SOP(Git版)

一、原则1、在维护公共基类、工具类和二方库等可能影响到其他团队成员的代码之前&#xff0c;必须同其他团队成员讨论&#xff0c;达成共识后方可进行维护。2、严格遵守源码签入规范&#xff0c;有助于发现代码漏洞&#xff0c;降低代码合并风险&#xff0c;降低远程仓库代码的…

linux下使用pidcat找bug

第一步&#xff1a; 安装pidcat 第二步&#xff1a; 找到APP的包名比如adb shell ps | grep sangforadb shell pm list package第三步&#xff1a; 在ubuntu终端输入pidcat.py 包名结果&#xff1a;