首页 > 其他 > 详细

1012 数字分类

时间:2020-03-09 18:53:22      阅读:60      评论:0      收藏:0      [点我收藏+]

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:

  • A?1?? = 能被 5 整除的数字中所有偶数的和;
  • A?2?? = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n?1??n?2??+n?3??n?4???;
  • A?3?? = 被 5 除后余 2 的数字的个数;
  • A?4?? = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;
  • A?5?? = 被 5 除后余 4 的数字中最大数字。

输入格式:

每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。

输出格式:

对给定的 N 个正整数,按题目要求计算 A?1??~A?5?? 并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出 N

输入样例 1:

13 1 2 3 4 5 6 7 8 9 10 20 16 18
 

输出样例 1:

30 11 2 9.7 9
 

输入样例 2:

8 1 2 4 5 6 7 9 16
 

输出样例 2:

N 11 2 N 9



这次试着用递归去写,虽然比较繁琐,但自己对递归了解更加深入了。
#include <iostream>

#include<algorithm>


using namespace std;

int a = 0,g=0;

int suma1(int* p,int n) {
    
    if (n ==0)
        return 0;
    else if ((*p) % 10 == 0) 
       
        return *p + suma1(p+1,n-1);

    else 
        return suma1(p+1,n-1);
      
   
}
int suma2(int* p,int n) {
    if (n == 0)
        return 0;
   
    
    else if ((*p) % 5 == 1) {
        a = 1;
        return *p - suma2(p+1,n-1);
    }
    else
        return suma2(p +1,n-1);
}
int suma3(int* p ,int n) {
    if (n==0)
        return 0;
    else if ((*p) % 5 == 3) {
        g++;
        return *p + suma3(p +1,n-1);
    }
    else
        return suma3(p+1,n-1);
}



int main() {
    int all[1010];
    int n, i = 0, m;
    cin >> n;
    for (; i < n; i++)
        cin >> all[i];
    int sumfive = suma2(all,n);
    sort(all, all + n);



    int sumten = suma1(all,n);
    
    int two = 0, four = 0;
    int sumthree = suma3(all,n);
    double three;
    if (g != 0)
        three = ((double)sumthree) / ((double)g);
    for (int i = 0; i < n; i++) 
        if (all[i] % 5 == 2)
            two++;
    for (int i = n - 1; i >= 0; i--)
            if (all[i] % 5 == 4) {
                four = all[i];
                break;
            }
    if (sumten != 0)
        cout << sumten << " ";
    else
        cout << "N" << " ";
    if (a == 1)
        cout << sumfive << " ";
    else
        cout << "N" << " ";
    if (two != 0)
        cout << two << " ";
    else
        cout << "N" << " ";
    if (g != 0)
        printf("%.1f ", three);
    else
        cout << "N" << " ";
    if (four != 0)
        cout << four;
    else
        cout << "N";



}

 

附柳姐姐代码,一如既往的帅气,学不来,学不来。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n, num, A1 = 0, A2 = 0, A5 = 0;
    double A4 = 0.0;
    cin >> n;
    vector<int> v[5];
    for (int i = 0; i < n; i++) {
        cin >> num;
        v[num % 5].push_back(num);
    }
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < v[i].size(); j++) {
            if (i == 0 && v[i][j] % 2 == 0) A1 += v[i][j];
            if (i == 1 && j % 2 == 0) A2 += v[i][j];
            if (i == 1 && j % 2 == 1) A2 -= v[i][j];
            if (i == 3) A4 += v[i][j];
            if (i == 4 && v[i][j] > A5) A5 = v[i][j];
        }
    }
    for (int i = 0; i < 5; i++) {
        if (i != 0) printf(" ");
        if (i == 0 && A1 == 0 || i != 0 && v[i].size() == 0) {
            printf("N");
            continue;
        }
        if (i == 0) printf("%d", A1);
        if (i == 1) printf("%d", A2);
        if (i == 2) printf("%d", v[2].size());
        if (i == 3) printf("%.1f", A4 / v[3].size());
        if (i == 4) printf("%d", A5);
    }
    return 0;
}

 



1012 数字分类

原文:https://www.cnblogs.com/kalicener/p/12450038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!