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. windows到ubuntu

    按照xmarks同步浏览器书签. mvn, copy setting.xml 最好不要用apt-get install maven, 占用/的磁盘空间 mvn -U package -P"d ...

  2. java基础一(阅读Head First Java记录)

    写在前面的话 在实际应用java中,因为没有系统去看过书或者学习过,所以基础薄弱,刚好这本书是比较入门级的一些书,记录一下下面的一些基本概念,以方便自己来学习.当然如果对大家有帮助也是很好的. 因为书 ...

  3. 如何阅读jdk及开源框架的源码?

    1.熟悉设计模式 可以边读源码 ,边熟悉设计模式,理解编程思想. jdk中对应的设计模式见:http://blog.csdn.net/gtuu0123/article/details/6114197 ...

  4. shell编程9*9乘法表

    </pre>脚本内容<pre name="code" class="plain">#!/bin/bash for i in " ...

  5. hack vba password, en useful...

    Unbelivibale, but I found a very simple way that really works! Do the follwoing: 1. Create a new sim ...

  6. golang之panic,recover,defer

    defer,recover: 运行时恐慌一旦被引发,就会向调用方传播直至程序崩溃. recover内建函数用于“拦截”运行时恐慌,可以使当前的程序从恐慌状态中恢复并重新获得流程控制权. recover ...

  7. HttpClient详解

    HttpClient:是一个接口 首先需要先创建一个DefaultHttpClient的实例 HttpClient httpClient=new DefaultHttpClient(); 发送GET请 ...

  8. [Selenium]Turn Page By Scroll Bar

    Description: Need to turn page by operating scroll bar and find out the element in the current page. ...

  9. python3 二叉树的存储和三种遍历

    #coding:utf-8 class node(): def __init__(self,k=None,l=None,r=None): self.key=k; self.left=l; self.r ...

  10. 程序员不常用Linux命令集

    1) 关闭指定网卡,如关闭网卡eth0 ifconfig eth0 down 也可以使用ifdown,通常ifdown是一个指向ifup的软链接,而ifup为一个脚本文件. 2) 命令自启动,如希望机 ...