题目来源: HackerRank
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:

fun(A)
    sum = 0
    for i = 1 to A.length
        for j = i+1 to A.length
            sum = sum + Floor((A[i]+A[j])/(A[i]*A[j])) 
    return sum

给出数组A,由你来计算fun(A)的结果。例如:A = {1, 4, 1},fun(A) = [5/4] + [2/1] + [5/4] = 1 + 2 + 1 = 4。
Input
第1行:1个数N,表示数组A的长度(1 <= N <= 100000)。
第2 - N + 1行:每行1个数A[i](1 <= A[i] <= 10^9)。
Output
输出fun(A)的计算结果。
Input示例
3
1 4 1
Output示例
4

求Floor((A[i]+A[j])/(A[i]*A[j])) ,实际上就是求1的数量和2的数量,有多少1就加一个1,有多少个2就是n*(n-1)/2。

我这个代码写麻烦了:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; long long val[100005]; int main()
{ int i,i2,n,pos;
long long j,ans=0;
scanf("%d",&n); for(i=0;i<n;i++)
{
scanf("%lld",val+i);
}
sort(val,val+n); for(i=0;i<n;i++)
{
if(val[i]==1||val[i]==2)
{
for(j=i+1;j<n;j++)
{
ans+=floor((double)((double)(val[i]+val[j])/(double)(val[i]*val[j])));
}
}
else
{
break;
}
}
cout<<ans<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

51nod 1305:Pairwise Sum and Divide的更多相关文章

  1. 1305 Pairwise Sum and Divide

    1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对 ...

  2. Pairwise Sum and Divide 51nod

      1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有这样 ...

  3. 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏

    1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...

  4. 1305 Pairwise Sum and Divide(数学 ,规律)

    HackerRank   1305 Pairwise Sum and Divide   有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = ...

  5. 51nod1305 Pairwise Sum and Divide

    题目链接:51nod 1305 Pairwise Sum and Divide 看完题我想都没想就直接暴力做了,AC后突然就反应过来了... Floor( (a+b)/(a*b) )=Floor( ( ...

  6. 51Nod 1305 Pairwise Sum and Divide | 思维 数学

    Output 输出fun(A)的计算结果. Input示例 3 1 4 1 Output示例 4 first try: #include "bits/stdc++.h" using ...

  7. [51nod] 1305 Pairwise Sum and Divide 数学

    有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = 0     for i = 1 to A.length         for j = ...

  8. 51nod 1305 Pairwise Sum and Divide

    有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = 0     for i = 1 to A.length         for j = ...

  9. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

随机推荐

  1. 安装ruby的一些坑

    之前一直下载不下来.是因为需要翻墙.

  2. 前端IT攻城狮--网址搜藏(-- 欢迎留言补充 --)

    一. 插件 1.validator: https://validator.niceue.com/docs/                 (一款专业表单验证插件) 2.jQuery Tabs-Eas ...

  3. idea-plugin-easycode

    1.背景 在练习使用mybatis-generator时候,无意间看到博文esaycode(代码神器),https://www.jianshu.com/p/e4192d7c6844,试验完,感觉这个工 ...

  4. leetcode303 Range Sum Query - Immutable

    """ Given an integer array nums, find the sum of the elements between indices i and j ...

  5. 如何拯救被Due逼疯的留学生们?

    Final季又到了,还有多少paper,多少project没完成?每年一到这个时候,手忙脚乱赶各种进度就成了留学小伙伴们共同的日常.任务多,不知道从何开始,拖延,烦躁……到底该怎么办?小编今天为各位介 ...

  6. window下Apache Jmeter基础用法

    1.下载Apache-jmeter-5.1.1.zip 2.解压到一个地方,就可以开始使用了,如果需要在CMD里快速打开,可以设置环境变量. 3.在bin里面,直接打开jmeter.bat. 可以修改 ...

  7. 32位CPU和64位CPU 区别

    操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的CPU设计. 64位操作系统针对的64位的CPU设计.操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的C ...

  8. MAC设置允许任何来源

    在升级了macOS Sierra (10.12)版本后在“安全性与隐私”中不再有“任何来源”选项 接下来,我们就打开终端,然后输入以下命令: sudo spctl --master-disable 输 ...

  9. CocosCreator - 向上传递事件(冒泡)

    /** *   分发事件到事件流中. *   this.node.dispatchEvent(new cc.Event.EventCustom("name",是否向上传递)) *  ...

  10. python 检测端口是否被占用

    前记   python中有些常用的东西,虽然小,但是非常实用.这里就做个备忘吧. 实例 检测端口是否被占用. ''' -- coding: utf-8 -- import os import sock ...