PAT 1012. 数字分类 (20)

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

  • A1 = 能被5整除的数字中所有偶数的和;
  • A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
  • A3 = 被5除后余2的数字的个数;
  • A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
  • A5 = 被5除后余4的数字中最大数字。

    输入格式:

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

    输出格式:

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

    若其中某一类数字不存在,则在相应位置输出“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
    题目分析:
    (1)安要求一类一类统计或处理即可
    (2)调节输出格式方法:
        #include<iomanip>
          {cout<<setiosflags(ios::fixed)<<setprecision(1);
    
    #include <iostream>
    #include<iomanip>//inportant
    #include <string>
    using namespace std;
    int main ()
    {
        int n;
    	cin>>n;
    	int income[1000];
    	for (int i=0;i<n;i++)
    	{
    		cin>>income[i];
    	}
    
    	float outcome[5]={0};
    
        
    	for (int a=0;a<n;a++)
    	{
    		if (income[a]%5==0&&income[a]%2==0)
    		{
    			outcome[0]=outcome[0]+income[a];
    		}
    	}
        int mark=0;
    	int mark1=-1;
    	for (int b=0;b<n;b++)
    	{
            if (income[b]%5==1)
    		{
    			mark++;
    			mark1=0-mark1;
    			outcome[1]=outcome[1]+income[b]*mark1;
    		}
    	}
    
    	for (int c=0;c<n;c++)
    	{
    		if (income[c]%5==2)
    		{
                outcome[2]++;
    		}
    	}
    
    	int mark2=0;
    	for (int d=0;d<n;d++)
    	{
    		if (income[d]%5==3)
    		{
    			mark2++;
    			outcome[3]=outcome[3]+income[d];
    		}
    	}
    	if (outcome[3]!=0)
    	{outcome[3]=outcome[3]/mark2;}
    
    	for (int e=0;e<n;e++)
    	{
    		if (income[e]%5==4)
    		{
    			if (income[e]>outcome[4])
    				outcome[4]=income[e];
    		}
    	}
    
    	if (outcome[0]==0)
    		cout<<'N';
    	else 
    		cout<<outcome[0];
    
    	if (mark==0)
    		cout<<" N";
    	else
    		cout<<' '<<outcome[1];
    	
    	if (outcome[2]==0)
    		cout<<" N";
    	else 
    		cout<<' '<<outcome[2];
    
    	if (outcome[3]==0)
    		cout<<" N";
    	else 
    	{cout<<setiosflags(ios::fixed)<<setprecision(1);//importent
    		cout<<' '<<outcome[3];}
    
    	if (outcome[4]==0)
    		cout<<" N";
        else
    		cout<<' '<<int(outcome[4]);
    
    	return 0;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值