zoukankan      html  css  js  c++  java
  • LightOJ

    Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

    Status

    Description

    I was trying to solve problem '1234 - Harmonic Number', I wrote the following code

    long long H( int n ) {
        long long res = 0;
        for( int i = 1; i <= n; i++ )
            res = res + n / i;
        return res;
    }

    Yes, my error was that I was using the integer divisions only. However, you are given n, you have to find H(n) as in my code.

    Input

    Input starts with an integer T (≤ 1000), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n < 231).

    Output

    For each case, print the case number and H(n) calculated by the code.

    Sample Input

    11

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    2147483647

    Sample Output

    Case 1: 1

    Case 2: 3

    Case 3: 5

    Case 4: 8

    Case 5: 10

    Case 6: 14

    Case 7: 16

    Case 8: 20

    Case 9: 23

    Case 10: 27

    Case 11: 46475828386

    Source

    Problem Setter: Jane Alam Jan
    /**
              题意:求n/1 + n/2 + n/3 + ........n/n
              做法:先求tt = sqrt(n); 比如 n = 10 
              sqrt(10) = 3,10/1 ~ 10/10 之间有 10/6 ~ 10/10 之间都是1 个数为(10/1 - 10/2)个
              之间为2的个数为 (10/4 ~ 10/5) 有(10/2 ~ 10/3) 个
              为3的个数有 10 /3 有(10/3) 个
              然后加判断 如果 n/(int)sqrt(n) == (int)sqrt(n)  则sum -= n/(int)sqrt(n)
    **/
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    int main()
    {
    #ifndef ONLINE_JUDGE
              freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
              int T;
              int Case = 1;
              scanf("%d",&T);
              while(T--)
              {
                        long long n;
                        scanf("%lld",&n);
                        long long res = 0;
                        int tt = (int)sqrt(n);
                        for(int i=1;i<=tt;i++)
                        {
                                  res += (n/i);
                        }
                        for(int j=1;j<=tt;j++)
                        {
                                  res += (n/j - n/(j+1)) * j;
                        }
                        if( n/(int)sqrt(n) == (int)sqrt(n))
                        res -= (n/(int)sqrt(n));
                        printf("Case %d: %lld
    ",Case++,res);
              }
              return 0;
    }
  • 相关阅读:
    JAVA 注解的几大作用及使用方法详解
    内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
    关于Hash集合以及Java中的内存泄漏
    ifconfig命令详情
    route命令详情
    ping命令详解
    scp命令详解
    ssh命令详解
    telnet命令详解
    sudo命令详解
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4445544.html
Copyright © 2011-2022 走看看