long long 的运行时间为什么比int长?

_西西_ 2013-10-05 07:56:34
杭电中,数据定义为long long 后, 运行的时间比int长,(hud 1597)结果导致程序超时。为什么?
...全文
552 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nice_cxf 2013-10-10
  • 打赏
  • 举报
回复
for (i=1; i<n; i++) n -= i; 这算法太强大了,应该是求从1加到i之和比n小的i的最大数 我只会用高斯公式解方程求出最接近的i要用到开方。。。
赵4老师 2013-10-10
  • 打赏
  • 举报
回复
引用 8 楼 u010499449 的回复:
[quote=引用 7 楼 zhao4zhong1 的回复:]
#include <stdio.h>
int main()
{
    long long n,i;
    int num;
    scanf("%d", &num);
    while (num--)
    {
        scanf("%lld", &n);
        for (i=1ll; i<n; i++)
          n -= i;
        if (n%9ll)
           printf("%lld\n", n%9ll);
        else
           printf("9\n");
    }
    return 0;
}
还是超时 请问 1ll是什么意思[/quote] C++ Language Reference C++ Integer Constants See Also Collapse All Expand All Language Filter: All Language Filter: Multiple Language Filter: Visual Basic Language Filter: C# Language Filter: C++ Language Filter: J# Language Filter: JScript Visual Basic (Declaration) Visual Basic (Usage) C# C++ J# JScript Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Grammar integer-constant: decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant: nonzero-digit decimal-constant digit octal-constant: 0 octal-constant octal-digit hexadecimal-constant: 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit: one of 1 2 3 4 5 6 7 8 9 octal-digit: one of 0 1 2 3 4 5 6 7 hexadecimal-digit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix: unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix: one of u U long-suffix: one of l L 64-bit integer-suffix: i64 LL ll To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: Copy Code int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: Copy Code int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the "x" does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: Copy Code int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: Copy Code unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value To specify a 64-bit integral type, use the LL, ll or i64 suffix. For example, Copy Code // 64bitsuffix.cpp #include <stdio.h> enum MyEnum { IntType, Int64Type }; MyEnum f1(int) { printf("in f1(int)\n"); return IntType; } MyEnum f1(__int64) { printf_s("in f1(__int64)\n"); return Int64Type; } int main() { MyEnum t1 = f1(0x1234), t2 = f1(0x1234i64); } Output in f1(int) in f1(__int64) See Also Reference Literals (C++) To make a suggestion or report a bug about Help or another feature of this product, go to the feedback site.
赵4老师 2013-10-10
  • 打赏
  • 举报
回复
#include <stdio.h>
int main()
{
    __int64 n,i;
    int num;
    scanf("%d", &num);
    while (num--)
    {
        scanf("%I64d", &n);
        for (i=1i64; i<n; i++)
          n -= i;
        if (n%9i64)
           printf("%I64d\n", n%9i64);
        else
           printf("9\n");
    }
    return 0;
}
_西西_ 2013-10-09
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
#include <stdio.h>
int main()
{
    long long n,i;
    int num;
    scanf("%d", &num);
    while (num--)
    {
        scanf("%lld", &n);
        for (i=1ll; i<n; i++)
          n -= i;
        if (n%9ll)
           printf("%lld\n", n%9ll);
        else
           printf("9\n");
    }
    return 0;
}
还是超时 请问 1ll是什么意思
赵4老师 2013-10-08
  • 打赏
  • 举报
回复
#include <stdio.h>
int main()
{
    long long n,i;
    int num;
    scanf("%d", &num);
    while (num--)
    {
        scanf("%lld", &n);
        for (i=1ll; i<n; i++)
          n -= i;
        if (n%9ll)
           printf("%lld\n", n%9ll);
        else
           printf("9\n");
    }
    return 0;
}
lanseyouhu 2013-10-07
  • 打赏
  • 举报
回复
不同的编译器对于二者的长度可能不同的。。
lm_whales 2013-10-06
  • 打赏
  • 举报
回复
1)32Bits 程序,对32Bits整数执行 ++ ,-= % 这样的运算 当然 比64Bits整数运算快。 如果充分利用,CPU的指令系统,可能会差不多; 不过C,C++ 不会利用的那么充分,总有些指令不好利用。 所以,如果算法难以优化的话,可以用汇编代码优化局部程序。 2)for (i=1; i<n; i++) n -= i; ????? 这个循环,有点奇怪。 你确定这里应该这么做么???
_西西_ 2013-10-05
  • 打赏
  • 举报
回复
引用 1 楼 qq120848369 的回复:
代码bug,再查一下吧。
#include <stdio.h>
int main()
{
    long long   n;
    int num, i;
    scanf("%d", &num);
    while (num--)
    {
        scanf("%I64d", &n);
        for (i=1; i<n; i++)
          n -= i;
        if (n%9)
           printf("%I64d\n", n%9);
        else
           printf("9\n");
    }
    return 0;
}
_西西_ 2013-10-05
  • 打赏
  • 举报
回复
引用 1 楼 qq120848369 的回复:
代码bug,再查一下吧。
#include <stdio.h>
int main()
{
    int num, i, n;
    scanf("%d", &num);
    while (num--)
    {
        scanf("%d", &n);
        for (i=1; i<n; i++)
          n -= i;
        if (n%9)
           printf("%d\n", n%9);
        else
           printf("9\n");
    }
    return 0;
}
_西西_ 2013-10-05
  • 打赏
  • 举报
回复
#include <stdio.h>
int main()
{
    int num, i, n;
    scanf("%d", &num);
    while (num--)
    {
        scanf("%d", &n);
        for (i=1; i<n; i++)
          n -= i;
        if (n%9)
           printf("%d\n", n%9);
        else
           printf("9\n");
    }
    return 0;
}
qq120848369 2013-10-05
  • 打赏
  • 举报
回复
代码bug,再查一下吧。

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧