• C++ 语法基础课 习题3 —— 循环结构


    课堂

    1. 判断素数

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        
        bool is_prime = true;
        for(int i = 2;i<n;i++)//i从2开始
        {
            if(n%i==0)
            {
                cout<< i <<endl;
                is_prime = false;
                break;
            }
        }
        if(is_prime)  cout<<"yes"<<endl;
        else cout<<"not"<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2. 矩阵(难题)

    在这里插入图片描述

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        5
        int k = 1;
        for(int i = 1; i <= n;i++)
        {
            for(int j = 1; j <= n; j++,k++)
            {
                //cout<
                printf("%-5d",k);
            }
            cout<< endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3. 菱形(难题)

    在这里插入图片描述

    #include
    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        int cx = n/2,cy = n/2;
        for(int i=0;i<n;i++)
        {
            for(int j = 0;j<n;j++)
            {
                if(abs(i-cx)+abs(j-cy)<=n/2)//只适用于奇数
                    cout<<"*";
                else 
                    cout<<' ';
            }
            cout<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    例题

    1. 偶数

    Acwing 708.偶数

    #include
    #include
    using namespace std;
    
    int main()
    {
        for(int i=2;i<=100;i+=2)
        cout << i << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2. 奇数

    Acwing 709.奇数

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin>>n;
        for(int i = 1;i<=n;i+=2)
            cout<<i<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3. 正数

    Acwing 712.正数

    #include
    using namespace std;
    
    int main()
    {
        int cnt = 0;// 定义读入正数数据的个数
        for(int i =0;i<6;i++)
        {
            double x;
            cin >> x;
            
            if(x > 0)
            cnt ++;
        }
        cout << cnt << " positive numbers" << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4. 连续奇数的和1

    Acwing 714.连续奇数的和1

    #include
    #include
    
    using namespace std;
    
    int main()
    {
        int x,y;
        cin >> x >> y;
        
        if(x > y) swap(x,y);// 将小的放在前面,大的放在后面
        
        int sum = 0;
        int i = x+1;
        while(i < y)
        {
            if(i % 2)// 非0即为真,就是表示奇数
            sum += i;
            i++;
        }
        cout << sum << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    5. 最大数和它的位置

    Acwing 716.最大数和它的位置

    #include
    using namespace std;
    
    int main()
    {
        int max = 0;
        int position ;
        
        for(int i = 1;i <= 100;i ++)//位置从1开始算起
        {
            int x;
            cin >> x;
            if(x > max)
            {
                max = x;
                position = i;
            }
        }
        cout << max << endl;
        cout << position << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    6. 递增排列

    Acwing 721.递增排列

    #include
    using namespace std;
    
    int main()
    {
        int x;
        while(true)
        {
            cin >> x;
            if(!x) break;//x为零则退出
            
            for(int i = 1;i <= x;i++)
            cout << i << ' ';
            cout << endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    7. 连续整数相加(难点一直读数据)

    Acwing 720.连续整数相加

    • while(cin>>n,n<=0);// 如果为小等于0的数,则一直读入数据
    #include
    using namespace std;
    
    int main()
    {
        int a,n;
        cin >> a;
        while(cin>>n,n<=0);// 如果为小等于0的数,则一直读入数据
        
        int s = 0;
        for(int i = 0;i<n;i++)
        {
            s+=a+i;
        }
        cout<<s<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    8. 约数

    Acwing 724.约数

    #include
    using namespace std;
    
    int main()
    {
        int a;
        cin>>a;
        for(int i = 1;i<=a;i++)
        {
            if(a%i==0)
            {
                cout<<i<<endl;
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    9. PUM

    Acwing 723.PUM

    #include
    using namespace std;
    
    int main()
    {
        int n,m;
        int k = 1;
        cin >> n >> m;
        for(int i = 0;i < n;i++ )
        {
            for(int i = 1;i <= m;i++,k++)
            {
                if(i!=m)
                {
                    cout << k << ' ';
                }
                else 
                {
                    cout << "PUM";
                    cout << endl;
                }
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    习题

    1. 余数

    Acwing 715.余数

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        for(int i = 1;i < 10000;i++)
        {
            if( i%n==2)
            cout << i <<endl; 
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2. 六个奇数

    Acwing 710.六个余数

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        if(n%2==0)n++;
        
        for(int i=0;i<6;i++) cout<<n+i*2<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3. 乘法表

    Acwing 711.乘法表

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        for(int i = 1; i <= 10; i++)
        {
            cout << i << " x "<< n << " = "<< i*n << endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4. 实验

    Acwing 718.实验

    #include
    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        
        int c = 0,f = 0, r = 0;
        for(int i=0;i<n;i++)
        {
            int k;
            char t;
            cin >> k >> t;
            //scanf("%d %f",&k,&t);不可以使用,scanf读入数据时,不会自动过滤空格,回车,制表符,需要手动过滤
            if(t=='C') c+=k;
            else if(t=='F') f+=k;
            else if(t=='R') r+=k;
        }
        int s = c + r + f;
        printf("Total: %d animals\n", s);
        printf("Total coneys: %d\n", c);
        printf("Total rats: %d\n", r);
        printf("Total frogs: %d\n", f);
        printf("Percentage of coneys: %.2lf %%\n", (double)c / s * 100);
        printf("Percentage of rats: %.2lf %%\n", (double)r / s * 100);
        printf("Percentage of frogs: %.2lf %%\n", (double)f / s * 100);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    5. 区间2

    Acwing 713.区间2

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        
        int x = 0,y = 0;
        while(n--)//循环n次,读取n个数据
        {
            int t;
            cin >> t;
            
            if(t>=10&&t<=20)x ++;
            else y++;
        }
        printf("%d in\n%d out",x,y);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6. 连续奇数的和2

    Acwing 连续奇数的和2

    #include
    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;// 读取n对测试数据
        
        while(n--)// 进行 N 次测试
        {
            int a,b;
            cin >> a >> b;
            if(a > b) swap(a,b);
            
            int s = 0;
            for (int i = a + 1; i < b; i ++ )
                if (i % 2)
                    s += i;
            cout << s << endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    7. 简单斐波那契

    Acwing 717.简单斐波那契

    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        
        int a = 0,b = 1;
        for(int i = 0;i < n;i ++)
        {
            cout << a << ' ';
            int c = a + b;
            a = b;
            b = c;
        }
        cout << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    8. 数字序列和它的和

    • while(cin >> a >> b,a > 0 && b > 0)

    Acwing 722.数字序列和它的和

    #include
    using namespace std;
    
    int main()
    {
        int a,b;
        
        while(cin >> a >> b,a > 0 && b > 0)// 输入时就进行判断
        {
            if(a > b) swap(a,b);
            int sum = 0;
            for(int i = a;i<= b;i++)
            {
                sum += i;
                cout << i << ' ';
            }
            cout<<"Sum="<<sum<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    8. 完全数

    Acwing 725. 完全数

    #include
    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
    
        while (n -- )// 对 n 个数进行判断
        {
            int x;
            cin >> x;
    
            int s = 0;
            for (int i = 1; i * i <= x; i ++ )
                if (x % i == 0)// 说明i是约数
                {
                    if (i < x) s += i;
                    if (i != x / i && x / i < x) s += x / i;
                }
            if (s == x) printf("%d is perfect\n", x);
            else printf("%d is not perfect\n", x);
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    9. 质数

    Acwing 726.质数

    #include
    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        
        while(n--)
        {
            int a;
            cin >> a;
            
            bool is_prime = true;
            for(int i = 2;i*i <= a;i++)
            {
                if(a % i == 0)
                {
                    is_prime = false;
                    break;
                }
            }
            if(is_prime)
                cout<< a <<" is prime"<<endl;
            else
                cout<< a <<" is not prime"<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    10. 菱形(难点)

    Acwing 727.菱形

    • 可以使用曼哈顿距离
      在这里插入图片描述
    • n = 5时,图形如下

    在这里插入图片描述

    #include
    #include
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        
        int a = n/2,b = n/2;
        for(int i = 0; i < n;i ++)
        {
            for(int j = 0;j < n;j ++)
            {
                if(abs(i-a) + abs(j-b) <= n/2)
                cout << "*";
                else 
                cout << ' ';
            }
            cout<<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    [附源码]java毕业设计餐厅卫生安全系统
    2023年【电工(中级)】考试资料及电工(中级)找解析
    【目标检测】YOLOv5遇上知识蒸馏
    python经典百题之打印楼梯
    Chrony让内网设备时间同步
    华为机试真题 C++ 实现【最大括号深度】
    安装yolov3(Anaconda)
    升级Flutter 3.13.x 之后出现watcher-1.0.2报错
    计算机网络 | I/O模型、网络模型(OSI七层及TCP/IP四层)
    【Java】NIO概述
  • 原文地址:https://blog.csdn.net/qq_42731062/article/details/127848105