1012--乙级

该C++程序读取一串数字,统计其中能被5和2同时整除的数的总和及数量,正负交替的数的差值及出现次数,偶数位置的数的个数,平均值以及最大值。程序通过输入控制台进行数据交互,并以特定格式输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<string>
#include <iomanip>
#include<sstream>
using namespace std;

int main()
{
    int noUse = 0;
    cin >> noUse;
    string s;
    getline(cin, s);
    int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0;
    int t, num1 = 0, num2 = 0, tag2 = 0, num3 = 0, max5 = 0;
    double sum4 = 0.0, num4 = 0.0, aver4 = 0.0;
    for (istringstream sin(s); sin >> t;) {
        
        if (t % 5 == 0 && t % 2 == 0) {
            num1 += t;
            a1++;
        }
        if (t % 5 == 1) {

            if (tag2 == 0) {
                num2 += t;
                tag2 = 1;
                continue;  //跳过下面这个if
            }
            if (tag2 == 1) {
                num2 -= t;
                tag2 = 0;
            }
            a2++;
        }
        if (t % 5 == 2) {
            num3++;
            a3++;
        }
        if (t % 5 == 3) {
            num4++;
            sum4 += t;
            a4++;
        }
        if (t % 5 == 4) {
            if (t > max5) {
                max5 = t;
            }
            a5++;
        }


    } 
    aver4 = sum4 / num4;
    if (a1 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << num1 << " ";
    }
    if (a2 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << num2 << " ";
    }
    if (a3 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << num3 << " ";
    }
    if (a4 == 0) {
        cout << "N" << " ";
    }
    else {
        cout << fixed << setprecision(1) << aver4 << " ";
    }
    if (a5 == 0) {
        cout << "N";
    }
    else {
        cout << max5;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

二零二三.

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值