第三节循环语句

1.基础知识

:while do while for

if 是判断一次执行后面的语句,while是每次成立执行循环语句中的语句

if(a%2)与if(a%2==0)i区别

f(a%2)是对A%2的结果进行判断,如果是非零,则判断为真,否则为假;if(a%2==0)是判断a%2是否为零,是则为真,否则为假。

int i=0;
if(i<10)
{
 cout<<i<endl;
    i++;
}
while(i<10)
{
  cout<<i<endl;
    i++;
}
第二个输出
0
第二个输出
    0
    1
    2
    ……
    9

2.do while while

do while循环先执行再判断。while判断执行

while(cin>>n,n)意思是电脑让用户读入n,并且循环n次。如果将cin>>n放到外面,可能会做超时处理。

例如:从1加到10

int main()
{
    int s=0,r=0;
    while(j<=10)
    {
        r=r+j;j++;
    }
    cout << r << endl;
    int i=1;
    do{
        s=s+i;i++;
    }while(i<=10);
    cout <<s<<endl;
    return 0;
}

两种输出一样都是55

3.for

for(int 语句;条件语句;表达式)

例:

int main()
{
    for (int i=1;i<=10;i++)
    {
        cout << i <<endl;
    }
    return 0;
}输出1
    2
    ……
    10

多重循环

画一个菱形,用曼哈顿距离求解,走到中心点的距离小于等于2

image-20221111173003197

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    cin >> n;

    int sx = n / 2, sy = n / 2;

    for (int i = 0; i < n ; i ++ )
    {
        for (int j = 0; j < n; j ++ )
        {
            if ( abs(sx - i) + abs(sy - j) <= n / 2 ) cout << "*";
            else cout << " ";
        }
        cout << endl;    
    }

    return 0;
}


image-20221111174353767

1.输入三个数,a,b,c 并将 a,b,c 三个数降序排列

    if (A<B)
    {    double T;
        T=A;
        A=B;
        B=T;
    }//使A>b
        if(A<C)
        {
            double t;
            t=A;
            A=C;
            C=t;
        }//使A>c
        if(C>B)
        {
            double t;
            t=B;
            B=C;
            C=t;
        }//使b>c

4.例题

image-20221111092214622

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    int n=0;
    for (int i=0;i<6;i++)//定义一个循环,使其输入6次
    {
        double x;
        cin >> x;
        if(x>0)
        n++;
    }
    cout << n << " positive numbers" << endl;
    return 0;
}

2.1加到n的立方

image-20221111094933982

3.斐波那契数列

image-20221111153453225

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int a,b;
    a=0;
    b=1;
    int n;
    cin>>n;
    int i=0;
    printf("%d ",a);
    while(i<n-1)
    {
        int c=a+b;
        a=b;
        b=c;
        i++;
        printf("%d ",a);
    }

    return 0;
 } 

4.输出六个奇数

image-20221112121423563

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for(int i=0;i<12;i++)
    {

            if(n%2==1)
            cout << n << endl;
            n++;
        }

    return 0;
}
yxc
using namespace std;

int main()
{
    int x;
    cin >> x;

    if (x % 2 == 0) x ++ ;//如果是偶数,一直加,如输入3

    for (int i = 0; i < 6; i ++ ) cout << x + i * 2 << endl;//3+2.3+2*2.3+3*2

    return 0;
}

5.完全数(不大理解)

image-20221113133602272
image-20221113133625645

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int n;cin >> n;
    while(n--)
    {
        int x;cin>>x;
        int m=0;
        for(int i=1;i*i<=x;i++)
        if(x%i==0)
        {
            if(i<x) m+=i;
            if(i!=x/i&&x/i<x) m+=x/i;
        }
        if(m==x)printf("%d is perfect\n",x);
        else printf("%d is not prefect\n",x);
    }
    return 0;//约数都是成对出现的

原文地址:http://www.cnblogs.com/Cathy-cat/p/16885880.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性