伟大的旭哥 2021-05-17 19:02 采纳率: 0%
浏览 141

大佬求解这道题,求代码

这道题怎么做,差分数组,求附代码,c++编程

 

 

1.题目描述

Farmer John因为他的奶牛发出的声音太大而被他的邻居Bob投诉。

FJ的N(1≤N≤100000)头奶牛都在牧场上的不同位置上吃草,牧场可以看做是一个长的一维牧场。奶牛们非常健谈,每对奶牛同时进行对话,所以每头奶牛同时与其他N-1头奶牛发哞哞声。当奶牛i对奶牛j发出哞哞声时,这个哞哞声的音量必须等于i和j之间的距离,这样奶牛j才能听到哞哞声。

请帮助FJ计算N*(N-1)个哞哞声同时发声时产生的声音总量。

输入格式

第一行,一个整数N,表示奶牛的数量

接下来N行,每行一个整数,表示N头奶牛在牧场上的位置Li(0≤Li≤10^8)

输出格式

一个整数,表示总音量

输入输出样列

输入样例1:


 
5 1 5 3 2 4

输出样例1:


 
40

说明

样例说明:

5头奶牛的位置分别是:1,5,3,2,4

位置1的奶牛产生的音量是 1+2+3+4=10, 

位置5的奶牛产生的音量是 4+3+2+1=10, 

位置3的奶牛产生的音量是 2+1+1+2=6, 

位置2的奶牛产生的音量是 1+1+2+3=7, 

位置4的奶牛产生的音量是 3+2+1+1=7. 

所以总的音量是(10+10+6+7+7) = 40.

  • 写回答

3条回答 默认 最新

  • benbenli 2021-05-17 21:53
    关注
    
    #include <stdio.h>
    
    #define ARRAY_SIZE 100000
    
    int input_sounds(int sound[ARRAY_SIZE])
    {
        int number_of_sound = 0;
        char text[ARRAY_SIZE * 10];
        int i;
        
        printf("Please enter the number of sound values, followed by sound values, delimited by spaces or linebreaks:\n");
        scanf("%d", &number_of_sound);
        
        for (i = 0; i < number_of_sound; ++i)
            scanf("%d", &sound[i]);
        
        return number_of_sound;
    }
    
    unsigned long long calculate_total_sound(int sound[ARRAY_SIZE], int number_of_sound)
    {
        unsigned long long total_sound = 0;
        int i, j;
        
        for (i = 0; i < number_of_sound; ++i)
            for (j = 0; j < number_of_sound; ++j)
                if (i != j)
                {
                    int sound_to_hear = sound[i] - sound[j];
                    if (sound_to_hear < 0)
                        sound_to_hear = -sound_to_hear;
                    total_sound += sound_to_hear;
                }
    
        return total_sound;
    }
    
    int main()
    {   
        int sound[ARRAY_SIZE];
        int number_of_sound = 0;
        unsigned long long total_sound = 0;
    
        number_of_sound = input_sounds(sound);
        
        total_sound = calculate_total_sound(sound, number_of_sound);
        printf("The total sound is %lld\n", total_sound);
    
        return 0;
    }
    
    
    // Output:
    Please enter the number of sound values, followed by sound values, delimited by spaces or linebreaks:
    5 1 5 3 2 4
    The total sound is 40
    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表