SDUSTOJ T1581 Average Number

本文介绍了一个编程挑战,要求计算一组整数的平均数,但不能使用加法、乘法或除法操作。通过巧妙地利用减法实现了这一目标,并提供了一个具体的C语言实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Average Number

Time Limit: 1 Sec  Memory Limit: 128 M

Description

Please calculate the average number of the given n numbers without doing calculations of addition, multiplication, or division, or using any library fuction which is not in "stdio.h". It is guaranteed that the results are all integers and they are not less than 1 and not bigger than 100.

Input

The first line of the input contains a single integer T, indicating the number of the test cases, one per line.
Each test case starts with an integer n(1 ≤ n ≤ 200), followed by n numbers to calculate the average number. All numbers are within the range of [1, 100].

Output

For each test case, output the average number, one per line.

Sample Input

17 46 93 49 70 66 69 20

Sample Output

59

HINT

If any character of "+", "*", "/" or any name of library fuction which is not in "stdio.h" appers in your code, you will get "Invalid Word" and your code will not be accepted.


分析:这个题目不难,关键是思路,不能用加法,乘法,除法,那为什么没说不能用减法。所以这个题的关键就是把所有的运算都转化为减法。


#include <stdio.h>
 
int main()
{
    int n,m,i,j;
    int temp,sum;
    double avr;
    scanf("%d",&n);
    for(i=1;i<=n;i=(i-(-1)))
    {
        scanf("%d",&m);
        for(j=1,sum=0;j<=m;j=(j-(-1)))
        {
            scanf("%d",&temp);
            sum = sum - (-temp);
        }
        for(avr=0;sum!=0;avr=(avr-(-1)))
        {
            sum = sum - m;
        }
        printf("%.0lf\n",avr);
    }
    return 0;
}


转载于:https://www.cnblogs.com/kunsoft/p/5312815.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值