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

课堂

1. 判断素数

#include<iostream>
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;
}

2. 矩阵(难题)

在这里插入图片描述

#include<iostream>
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<<k<<' ';
            printf("%-5d",k);
        }
        cout<< endl;
    }
    return 0;
}

3. 菱形(难题)

在这里插入图片描述

#include<iostream>
#include<cstdio>
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. 偶数

Acwing 708.偶数

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    for(int i=2;i<=100;i+=2)
    cout << i << endl;
    return 0;
}

2. 奇数

Acwing 709.奇数

#include<iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int i = 1;i<=n;i+=2)
        cout<<i<<endl;
    return 0;
}

3. 正数

Acwing 712.正数

#include<iostream>
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;
}

4. 连续奇数的和1

Acwing 714.连续奇数的和1

#include<iostream>
#include<algorithm>

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;
}

5. 最大数和它的位置

Acwing 716.最大数和它的位置

#include<iostream>
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;
}

6. 递增排列

Acwing 721.递增排列

#include<iostream>
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;
}

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

Acwing 720.连续整数相加

  • while(cin>>n,n<=0);// 如果为小等于0的数,则一直读入数据
#include<iostream>
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;
}

8. 约数

Acwing 724.约数

#include<iostream>
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;
}

9. PUM

Acwing 723.PUM

#include<iostream>
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. 余数

Acwing 715.余数

#include<iostream>
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;
}

2. 六个奇数

Acwing 710.六个余数

#include<iostream>
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;
}

3. 乘法表

Acwing 711.乘法表

#include<iostream>
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;
}

4. 实验

Acwing 718.实验

#include<iostream>
#include<cstdio>
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;
}

5. 区间2

Acwing 713.区间2

#include<iostream>
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;
}

6. 连续奇数的和2

Acwing 连续奇数的和2

#include<iostream>
#include<algorithm>
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;
}

7. 简单斐波那契

Acwing 717.简单斐波那契

#include<iostream>
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;
}

8. 数字序列和它的和

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

Acwing 722.数字序列和它的和

#include<iostream>
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;
}

8. 完全数

Acwing 725. 完全数

#include<iostream>
#include<cstdio>
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;
}

9. 质数

Acwing 726.质数

#include<iostream>
#include<cstdio>
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;
}

10. 菱形(难点)

Acwing 727.菱形

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

在这里插入图片描述

#include<iostream>
#include<cmath>
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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2021 Nqq

你的鼓励是我学习的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值