You are going to read a serial of none-negative integers, which ends with a negative number that does not count as one of the data. Your program calculates the average of all the none-negative integers, and prints the average which rounds to two decimal places.
When there’s no valid data at all, your program prints out:
None
输入格式:
A serial of none-negative integers, ending with a negative number, which is not one of the valide data.
输出格式:
A number rounds to two decimal places, which is the average of the serial.
The printf for this case is:
printf("%.2f\n", (double)sum/count);
输入例子
1 2 3 4 5 6 -1
输出例子
3.50
参考:
/*
* @Author: Ray123
* @Description: 求平均数,精确到小数点后两位
* 输入:1 2 3 4 5 6 -1
* 输出:3.50
* @Date: 2021-05-04 20:07:04
* @LastEditTime: 2021-05-04 22:54:53
* @FilePath: \undefinede:\MyApp\Microsoft VS Code\MyAdorableCode\PTA_c\137_Average.c
*/
#include <math.h>
#include <stdio.h>
#include <string.h>
int main()
{
int input;
int sum=0;
int count=0;
int para=1;//参数确保首次输入合法
int para2=1;//参数确保后续的输入合法
para=scanf("%d",&input);
if (input<=0)
{
para=0;
}
while (input>0&¶2*para==1)
{
sum+=input;
count++;
para2=scanf("%d",&input);
para2=para2*para;
}
if(para*para2==0){
printf("None");
}else{
double result;
result=sum*1.0/count;
//printf("%d\n",sum);
//printf("%d\n",count);
printf("%.2f",result);
}
return 0;
}
j