当前位置:网站首页>PAT 乙等 1012 C语言
PAT 乙等 1012 C语言
2022-06-23 04:11:00 【章鱼bro】
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
代码:
//开始时间20:49
//结束时间21:40
#include "stdio.h"
#include "stdlib.h"
int main()
{
int N;
int A1,A2,A3,A5;
A1 = A2 = A3 = A5 = 0;
int flag_2 = -1;//2的符号位
double A4;//A4要保留小数点
A4 = 0.0;
int count_1,count_2,count_3,count_4,count_5;
count_1 = count_2 = count_3 = count_4 = count_5 = 0;
scanf("%d",&N);
int * array = (int *)malloc(N * sizeof(int));
int i;
for(i = 0; i < N; i++)
{
scanf("%d",&array[i]);
}
for(i = 0; i < N; i++)
{
switch(array[i] % 5)
{
case 0:
if(array[i] % 2 == 0)
{
A1 += array[i];
count_1++;
}
break;
case 1:
flag_2 *= -1;
A2 += flag_2*array[i];
count_2++;
break;
case 2:
count_3++;
break;
case 3:
count_4++;
A4 += array[i];
break;
case 4:
if(A5 < array[i])
A5 = array[i];
count_5++;
break;
default: break;
}
}
A4 = A4 / count_4;
if(count_1 == 0)
printf("N ");
else
printf("%d ",A1);
if(count_2 == 0)
printf("N ");
else
printf("%d ",A2);
if(count_3 == 0)
printf("N ");
else
printf("%d ",count_3);
if(count_4 == 0)
printf("N ");
else
printf("%.1f ",A4);
if(count_5 == 0)
printf("N");
else
printf("%d",A5);
return 0;
}边栏推荐
- 软件设计开发笔记2:基于QT设计串口调试工具
- Win11应用商店一直转圈解决办法
- AHA C language Chapter 7 you can do more with it (talks 27-28)
- June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
- Wechat applet: an artifact for calculating the full amount of orders
- Mobile phone wireless charging dual coil 15W scheme SOC IC ip6809
- fastjson中的@JSONField注解
- MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
- 数字藏品市场才刚刚开始
- A bit of knowledge - folding forging and Damascus steel
猜你喜欢

Real MySQL interview question (30) -- shell real estate order analysis

Is there a real part-time job online? How do college students find part-time jobs in summer?

Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time

Composite API
![[graduation season u; advanced technology Er] farewell to the confused self in the past two years. Regroup, junior I'm coming](/img/04/3121514fcd8fcf1c939cbca7f4c67a.jpg)
[graduation season u; advanced technology Er] farewell to the confused self in the past two years. Regroup, junior I'm coming

Software project management 8.4 Software project quality plan

Raspberry pie assert preliminary exercise

Redis缓存穿透解决方案-布隆过滤器

数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发

visdom的使用
随机推荐
制造业数字化转型存在问题及原因分析
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 网桥与交换机
Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time
PAT 乙等 1026 程序运行时间
FS4059A与FS5080E充电芯片的区别
C primer plus learning notes - 2. Constant and formatted IO (input / output)
Composite API
数字化工厂建设可划分为三个方面
How can digital collections empower economic entities?
Wechat applet: a new interesting test
Is there a real part-time job online? How do college students find part-time jobs in summer?
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
Build a gocd environment
MDM data cleaning function development description
LeetCode-1757. Recyclable and low-fat products_ SQL
What if win11 cannot record audio? Solution of win11 unable to input sound
Wechat applet: an artifact for calculating the full amount of orders
The digital collection market has just begun
Wechat applet: future wife query generator
Mobile phone wireless charging dual coil 15W scheme SOC IC ip6809