AcWing语法基础 第三讲 循环结构习题

第三讲 循环结构

708. 偶数

//
// Created by lan on 2024/3/25.
// 708 偶数
#include <iostream>

using namespace std;

int main() {

    int i = 1;
    while (i <= 100) {
        if (i % 2 == 0) cout << i << endl;
        i++;
    }
    
    return 0;
}

709. 奇数

//
// Created by lanys on 2024/3/25.
// 奇数
#include <iostream>

using namespace std;

int main() {

    int n;
    cin >> n;
    for (int i = 0; i <= n; i++) {
        if (i % 2 != 0) {
            cout << i << endl;
        }
    }

    return 0;
}

710. 六个奇数

//
// Created by Administrator on 2013/1/1.
// 六个奇数
#include <iostream>

using namespace std;

int main() {

    int x;
    cin >> x;
    if (x % 2 == 0) x++;
    for (int i = 0; i < 6; i++) cout << x + i * 2 << endl;

    return 0;
}

711. 乘法表

//
// Created by Administrator on 2013/1/1.
// 乘法表
#include <cstdio>

using namespace std;

int main() {

    int n;
    scanf("%d", &n);
    for (int i = 1; i <= 10; i++) {
        // 注意这个是字母x,而不是×号
        printf("%d x %d = %d\n", i, n, i * n);
    }

    return 0;
}

712. 正数

//
// Created by lanys on 2024/3/25.
// 正数
#include <iostream>

using namespace std;

int main() {

    int count = 0;
    double a;
    for (int i = 0; i < 6; i++) {
        cin >> a;
        if (a > 0) count += 1;
    }
    cout << count <<" positive numbers" << endl;

    return 0;
}

713. 区间2

//
// Created by Administrator on 2013/1/1.
// 区间2
#include <iostream>

using namespace std;

int main() {

    int n, x;
    int in = 0, out = 0;
    cin >> n;
    while (n--) {
        cin >> x;
        if (x >= 10 && x <= 20) in++;
        else out++;
    }

    cout << in << " in" << endl;
    cout << out << " out" << endl;

    return 0;
}

714. 连续奇数的和1

//
// Created by lanys on 2024/3/25.
// 连续奇数的和1
#include <iostream>
#include <algorithm>

using namespace std;

int main() {

    int a, b;
    int sum = 0;
    cin >> a >> b;
    if (a > b) swap(a, b);
    for (int i = a + 1; i < b; i++) {
        if (i % 2 != 0) {
            sum += i;
        }
    }
    cout << sum << endl;


    return 0;
}

715. 余数

//
// Created by Administrator on 2013/1/1.
// 余数
#include <iostream>

using namespace std;

int main() {

    int n;
    cin >> n;
    for (int i = 2; i < 10000; i++) {
        if (i % n == 2) {
            cout << i << endl;
        }
    }

    return 0;
}

716. 最大数和它的位置

//
// Created by lan on 2024/3/25.
// 最大数和他的位置
#include <iostream>

using namespace std;

int main(){

    int index, n;
    int max = 0;
    for (int i = 0; i < 100; i++) {
        cin >> n;
        if (n > max) {
            max = n; // 将最大数赋值给max
            index = i + 1; // 记录index是第几个数
        }
    }
    cout << max << endl << index << endl;

    return 0;
}

717. 简单斐波那契

//
// Created by Administrator on 2013/1/1.
// 简单斐波那契
#include <iostream>

using namespace std;

int main() {

    int a = 0, b = 1;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cout << a << " ";
        int c = a + b;
        a = b;
        b = c;
    }


    return 0;
}

718. 实验

//
// Created by Administrator on 2013/1/1.
// 实验
#include <iostream>
#include <cstdio>

using namespace std;

int main() {

    int c = 0, r = 0, f = 0;
    int n, x;
    char y;
    // 输入整数n
    cin >> n;
    for (int i = 0; i < n; i++) {
        // 循环输入x y
        cin >> x >> y;
        // 根据所输入的y来判断要存的是哪个变量
        if (y == 'C' || y == 'c') c += x;
        else if (y == 'R' || y == 'r') r += x;
        else if (y == 'F' || y == 'f') f += x;
    }
    // 计算所占百分比
    double o = (c * 100.0) / (c + r + f);
    double p = (r * 100.0) / (c + r + f);
    double q = (f * 100.0) / (c + r + f);
    // 打印输出
    printf("Total: %d animals\n", c + r + f);
    printf("Total coneys: %d\n", c);
    printf("Total rats: %d\n", r);
    printf("Total frogs: %d\n", f);
    printf("Percentage of coneys: %.2lf %%\n", o);
    printf("Percentage of rats: %.2lf %%\n", p);
    printf("Percentage of frogs: %.2lf %%\n", q);

    return 0;
}

719. 连续奇数的和2

//
// Created by Administrator on 2013/1/1.
// 连续奇数的和2
#include <iostream>
#include <algorithm>

using namespace std;

int main() {

    int x, y, n;
    cin >> n;
    while (n--) {
        cin >> x >> y;
        if (x > y) swap(x, y);
        int sum = 0;
        for (int i = x + 1; i < y; i++) {
            if (i % 2 != 0) sum += i;
        }
        cout << sum << endl;
    }

    return 0;
}

720. 连续整数相加

//
// Created by lanys on 2024/3/25.
// 720   连续整数相加
#include <iostream>

using namespace std;

int main() {

    int a, b;
    cin >> a;
    while (cin >> b, b <= 0);
    int sum = 0;
    for (int i = 0; i < b; i++) {
        sum = sum + a;
        a++;
    }
    cout << sum << endl;

    return 0;
}

721. 递增序列

//
// Created by lan on 2024/3/25.
// 递增序列
#include <iostream>
#include <cstdio>

using namespace std;

int main() {

//    int a;
//    while (cin >> a, a) {
//        for (int j = 1; j <= a; j++)cout << j << " ";
//        cout << endl;
//    }

    int x;
    int cnt = 0;
    while (scanf("%d", &x) != -1) cnt++;
    cout << cnt << endl;

    return 0;
}

722. 数字序列和它的和

//
// Created by Administrator on 2013/1/1.
// 数字序列和它的和
#include <iostream>
#include <algorithm>

using namespace std;

int main() {

    int m, n;
    while (cin >> m >> n, n > 0 && m > 0) {
        if (m > n) swap(m, n);
        int sum = 0;
        for (int i = m; i <= n; i++) {
            cout << i << " ";
            sum += i;
        }
        cout << "Sum=" << sum << endl;

    }


    return 0;
}

723. PUM

//
// Created by lan on 2024/3/25.
// PUM
#include <iostream>

using namespace std;

int main() {

    int a, b;
    cin >> a >> b;
    for (int i = 0, x = 1; i < a; i++) {
        for (int j = 0; j < b - 1; j++) {
            cout << x << " ";
            x++;
        }
        cout << "PUM" << endl;
        x++;
    }

    return 0;
}

724. 约数

//
// Created by lanys on 2024/3/25.
// 约数
#include <iostream>

using namespace std;

int main() {

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) {
            cout << i << endl;
        }
    }


    return 0;
}

725. 完全数

//
// Created by lanys on 2024/3/26.
// 完全数
#include <iostream>

using namespace std;

int main() {

    int n, x;
    cin >> n;
    while (n--) {
        cin >> x;
        int sum = 0;
        /**
         * 2是12的约数,那6也是12的约数
         * 3是12的约数,那4也是12的约数
         * d是x的约数,那x/d也是12的约数
         * d <= x/d
         * d*d <= x
         * d <= 根号x 那么循环根号x次就可以了
         */
        for (int i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                // 加上一个约数d
                if (i < x) sum += i;
                // 和另一个约数x/d,排除 i != x / i是因为平方数只能算一次,例如三十六开平方6和6只能算一次
                if (i != x / i && x / i < x) sum += x / i;
            }
        }
        if (sum == x) cout << x << " is perfect" << endl;
        else cout << x << " is not perfect" << endl;
    }

    return 0;
}

726. 质数

//
// Created by lanys on 2024/3/26.
// 质数
#include <iostream>

using namespace std;

int main() {

    int n, x;
    cin >> n;
    while (n--) {
        cin >> x;
        bool is_prime = true;
        for (int i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                is_prime = false;
                break;
            }
        }
        if (is_prime) cout << x << " is prime" << endl;
        else cout << x << " is not prime" << endl;
    }

    return 0;
}

727. 菱形

//
// Created by lanys on 2024/3/26.
// 菱形
#include <iostream>
#include <cmath>

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;
}
  • 23
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值