HackerRank
 

有这样一段程序,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
 
看似简单的水题,但是里面的数学规律真的很难找,暴力的话超时,看了大神的代码后感觉一脸懵逼。欲辨已忘言。
这题真心坑,(a[i]+a[j])/(a[i]*a[j])只有当a[i]或a[j]为1或者为2是该式才不为零,其余情况都为0;
 

C++的运行时限为:1000 ms ,空间限制为:131072 KB 示例及语言说明请按这里
允许其他 AC 的用户查看此代码,分享代码才能查看别人的代码并有机会获得勋章
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    int a[100005];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
            sort(a+1,a+1+n);
            int c1=0,c2=0,c3=0;
            for(int i=1;i<=n;i++)
            {
                if(a[i]==1)
                 c1++;
                 else if(a[i]==2)
                        c2++;
                 else c3++;
            }
            int ans=c1*(c1+c2+c3-1)+c2*(c2-1)/2;
            printf("%d\n",ans);
    }
    return 0;
}
 
 
 
 
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
运行代码
提交代码

Test 1
特判题目没有标准输出数据

1305 Pairwise Sum and Divide(数学 ,规律)的更多相关文章

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

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

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

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

  3. 1305 Pairwise Sum and Divide

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

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

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

  5. 51nod 1305 Pairwise Sum and Divide

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

  6. 51nod 1305:Pairwise Sum and Divide

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

  7. Pairwise Sum and Divide 51nod

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

  8. 51nod1305 Pairwise Sum and Divide

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

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

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

随机推荐

  1. ORA-12528问题解决

    这个问题说明数据库没有Mount 最好先将系统日志一并清空,避免以下报错信息: ERROR:ORA-28056: Writing audit records to Windows Event Log ...

  2. docker 网络配置路由转发

    建好flannel 网络之后 iptables -L -n 查看 要全是accept iptables -P FORWARD ACCEPT 开启路由转发 修改/etc/sysctl.conf文件,添加 ...

  3. Python与Go快速排序

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 快速排序 # 时间复杂度 O(n lgn)-- O(n^2) def quick_sort(array) ...

  4. Add words to your picture

    [Add words to your picture] How to add text to your photo, and then style it with the Type tool. 1.O ...

  5. OGNL概述

    -------------------siwuxie095 OGNL 概述 1.OGNL 即 Object-Graph Navigation Language,对象图导航语言, 它是一个应用于 Jav ...

  6. UITextFeild银行卡/身份证/电话号任意分割.

    日常开发中可能有个需求, 1.银行卡每4位添加一个空格  2.电话号:3 4 4 比如(138 8888 8888)3.身份证(411111 20171213 1314) 看了网上许多方法都是输入的时 ...

  7. linux 关闭电子邮件传输服务

    1.检查sendmail的哪些runlevel开启: chkconfig --list sendmail

  8. 通过curl模拟多线程抓取网页(curl_multi_*)

    curl请求多个url,以前都是使用循环来处理.最近发现可以通过curl_multi_*系列函数来模拟多线程.比对一下,发现如果请求的url只有几个,2种方案耗时差不多,但是url比较多,差距就非常明 ...

  9. GlobalMemoryStatusEx获取内存

    typedef struct _MEMORYSTATUSEX { DWORD dwLength; DWORD dwMemoryLoad; DWORDLONG ullTotalPhys; DWORDLO ...

  10. Java Thread系列(十)Future 模式

    Java Thread系列(十)Future 模式 Future 模式适合在处理很耗时的业务逻辑时进行使用,可以有效的减少系统的响应时间,提高系统的吞吐量. 一.Future 模式核心思想 如下的请求 ...