7-137 Average

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&&para2*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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值