• 51nod 1305:Pairwise Sum and Divide


    题目来源: 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;
    }
    


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

  • 相关阅读:
    python--模块
    python--*args与**kw
    python--偏函数
    Reversible Cards ARC111 -B 思维,图论
    Simple Math ARC111
    状压DP详解(位运算)
    E
    Ball CodeForces
    Reward HDU
    Choosing Capital for Treeland CodeForces
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4899598.html
Copyright © 2020-2023  润新知