码迷,mamicode.com
首页 > 编程语言 > 详细

1012 数字分类 (20 分(C和C++版本)

时间:2018-09-23 18:18:58      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:lag   注意   ase   ble   判断   code   amp   out   max   

这道题其实就是按照要求写程序,注意几个细节就没有问题:

1.数字不存在是指不满足条件的数字(例如:A1...)

2.A2中可能会使所求的变量值为0,因此在这里不能用所求变量的值为0判断。

 

C++版本:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int N, *ptr, sum = 0, num, sum1 = 0, flag = 0;
    int ncount = 0, m_ncount = 0, t_ncount = 0, Max = 0;
    double ave = 0.0;

    cin >> N;

    ptr = new int[N];

    for (int i = 0; i < N; ++i) {
        cin >> ptr[i];
    }

    for (int k = 0; k < N; k++) {
        num = ptr[k] % 5;
        switch (num) {
        case 0:
            if(0 == ptr[k] % 2)
                sum += ptr[k];
            break;
        case 1:
            if (1 == flag % 2) {
                sum1 -= ptr[k];
                ++flag;
            }
            else if (0 == flag % 2) {
                sum1 += ptr[k];
                ++flag;
            }
            break;
        case 2:
            if (2 == ptr[k] % 5) {
                ++ncount;
            }
            break;
        case 3:
            if (3 == ptr[k] % 5) {
                ++m_ncount;
                ave += ptr[k];
            }
            break;
        case 4:
            if (0 == t_ncount) {
                Max = ptr[k];
            }
            else {
                if (Max < ptr[k]) {
                    Max = ptr[k];
                }
            }
            ++t_ncount;
            break;
        }
    }

    if (0 == sum) {
        cout << "N ";
    }
    else {
        cout << sum << " ";
    }

    if (0 == flag) {
        cout << "N ";
    }
    else {
        cout << sum1 << " ";
    }

    if (0 == ncount) {
        cout << "N ";
    }
    else {
        cout << ncount << " ";
    }

    if (0 == m_ncount) {
        cout << "N ";
    }
    else {
        ave /= m_ncount;
        cout.setf(ios::fixed);
        cout << setprecision(1) << ave << " ";
    }

    if (0 == t_ncount) {
        cout << "N";
    }
    else {
        cout << Max;
    }

    delete []ptr;

    return 0;
}

 

C语言版本:

#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    int N, *ptr, sum = 0, num, sum1 = 0, flag = 0;
    int ncount = 0, m_ncount = 0, t_ncount = 0, Max = 0;
    double ave = 0.0;

    scanf("%d", &N);

    ptr = (int*)malloc(sizeof(int) * N);

    for (int i = 0; i < N; ++i) {
        scanf("%d", &ptr[i]);
    }

    for (int k = 0; k < N; k++) {
        num = ptr[k] % 5;
        switch (num) {
        case 0:
            if(0 == ptr[k] % 2)
                sum += ptr[k];
            break;
        case 1:
            if (1 == flag % 2) {
                sum1 -= ptr[k];
                ++flag;
            }
            else if (0 == flag % 2) {
                sum1 += ptr[k];
                ++flag;
            }
            break;
        case 2:
            if (2 == ptr[k] % 5) {
                ++ncount;
            }
            break;
        case 3:
            if (3 == ptr[k] % 5) {
                ++m_ncount;
                ave += ptr[k];
            }
            break;
        case 4:
            if (0 == t_ncount) {
                Max = ptr[k];
            }
            else {
                if (Max < ptr[k]) {
                    Max = ptr[k];
                }
            }
            ++t_ncount;
            break;
        }
    }

    if (0 == sum) {
        printf("N ");
    }
    else {
        printf("%d ", sum);
    }

    if (0 == flag) {
        printf("N ");
    }
    else {
        printf("%d ", sum1);
    }

    if (0 == ncount) {
        printf("N ");
    }
    else {
        printf("%d ", ncount);
    }

    if (0 == m_ncount) {
        printf("N ");
    }
    else {
        ave /= m_ncount;
        printf("%0.1lf ", ave);
    }

    if (0 == t_ncount) {
        printf("N");
    }
    else {
        printf("%d", Max);
    }

    delete []ptr;

    return 0;
}

 

1012 数字分类 (20 分(C和C++版本)

标签:lag   注意   ase   ble   判断   code   amp   out   max   

原文地址:https://www.cnblogs.com/Mayfly-nymph/p/9692992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!