008day(for循环及while语句的学习)

来源:互联网 发布:php 存储过程 编辑:程序博客网 时间:2024/05/29 18:43

172210704111-陈国佳总结《2017年10月18日》【连续008天总结】

标题:for循环及while语句的学习

内容:A.观看MOOC3.3,3.4,3.5,3.6

           B.(a).通过观看3.3学习了for循环,举例:

int i=5;
for(int i=0;i<26;++i){
cout<<char('a'+i);

}运行即为26个小写英文字母;(当语句组只有一条语句时,{ }可不加,表达式1,3可以是通过“,”连接的多条表达式)

当上例程序后加入cout<<i;输出的i与for语句无关(i=5)

                (b).通过观看3.4学习了多重for循环,例题:给定正整数n和m,在1至n这n个数中,取出两个不同的数,使得其和是m的因子,问有多少种不同的取法。

例:int n,m;
int total=0;
cin>>n>>m; 
for (int i=1;i<n;++i ){
   for(int j=i+1;j<=n;++j)
          if(m%(i+j)==0)
             ++total;
}
    cout<<total; 

运行后,输入55,66;输出值为56;

表达式中可不写,但必须有“;”

             (c).通过观看3.5,3.6,学习了while语句,并用while语句表达了迭代公式:

例:

          using namespace std;
double EPS= 0.001;
int main()
{
double a;
cin >>a;
if (a>=0){
double x=a/2,lastX=x+1+EPS;
while(x -lastX>EPS||lastX - x>EPS){

lastX=x;
x= (x + a/x)/2;
}
cout << x;
}
else 
   cout <<"It can't be nagitive";
return 0;

运行后,输入9000,输出为94.8683;

明日计划:学习break语句和continue语句;