C++ 如何找到其多维数据集以特定数字结尾的数字?

C++ 如何找到其多维数据集以特定数字结尾的数字?,c++,C++,给定一个以1、3、7或9结尾的数字N 永远存在一个数字M,当以相同的原始数字N进行立方运算时,M的位数永远不必超过N 示例:-N=123。M=947。(947)^3=849278123. 此处(947)^3以N结尾(即123) 编写一个以N为输入的程序,并找到M,其中M是与N相同位数的数,当以N为立方结束时 我将代码编写为: #include "iostream" #include "math.h" using namespace std; int main() { long long

给定一个以1、3、7或9结尾的数字N

永远存在一个数字M,当以相同的原始数字N进行立方运算时,M的位数永远不必超过N

示例:-N=123。M=947。(947)^3=849278123. 此处(947)^3以N结尾(即123)

编写一个以N为输入的程序,并找到M,其中M是与N相同位数的数,当以N为立方结束时

我将代码编写为:

#include "iostream"
#include "math.h"
using namespace std;

int main()
{
    long long int t,p,j,i,d,c,s;
    cin>>t;
    long long int *n= new long long int[t];
    for(i=0;i<t;i++)
    {
        cin>>n[i];
    }
    for(i=0;i<t;i++)
    {   d=0; j=1;
        p=n[i];
        while(p)
        {
            d++;
            p=p/10;

        } p=n[i];
        s= pow(10,d);
        while(1)
        {
            c=j*j*j;
            if(c%s==p){break;}
            j++;

        }
    cout<<j<<endl;
     }
    return 0;
}
#包括“iostream”
#包括“math.h”
使用名称空间std;
int main()
{
长整型t,p,j,i,d,c,s;
cin>>t;
长整型*n=新长整型[t];
对于(i=0;i>n[i];
}

对于(i=0;i,您可以做很多事情。首先,请注意,以奇数结尾的多维数据集必须以奇数开头,因此仅尝试奇数作为M。节省时间的因子2

下一步-要查找数字的最后3位,只需执行
number%1000
。不要使用
pow
。速度非常慢。请参阅我查找数字大小的技巧

你最终会得到这样的结果:

long int N, M, div;

printf("what is the value of N?\n");
scanf("%d", &N);
// test that it is reasonable before continuing...
// I did not write that code, but you should (size of N, and does it end in 1, 3, 7, or 9?

// find out how many digits N has:
div = 1;
while(N / div > 0) {
  div *= 10;
}

// now loop around odd M
for(M = 1; M < div; M+=2) {
  if( (M*M*M)%d==N) break;
}
// when you come out of this loop, M is the number you were looking for.
由此得出结论,如果
N
1
结尾,您可以只检查以
1
结尾的数字:

for(M=1; M<div; M+=10) {
蛮力法——对所有可能性进行循环。应用数学可能会围绕这一点进行循环,但现在我让应用逻辑规则来处理。找到答案需要0.02137秒10000次;运行一次需要0.000004秒,这是非常大的四舍五入

作为奖励,它似乎也适用于以“5”结尾的值

(编辑)Applied Logic建议您不必检查M>1000。毕竟,(1000+M)³=1000³+3*M²*1000+3*M*1000²+M³,而且由于我们使用的是
mod
模块,1000以上的所有内容都被取消,计算减少到M³——我想知道,也许我们应该将这个问题转移到math.stackexchange.com

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

int main(int argc, char **argv)
{
    long long int N, M, val;
    int ii;
    time_t start, end;

    if (argc == 2)
        N = strtoul (argv[1], NULL, 10);
    else
    {
        printf("what is the value of N?\n");
        scanf("%lld", &N);
    }

    if (~N & 1)
    {
        printf ("invalid input\n");
        return EINVAL;
    }

    start = clock();
    for (ii=0; ii<10000; ii++)
    {
    for (M=1; M<1000; M+=2)
    {
        val = M%1000;
        val = val*val*val;
        if ((val % 1000) == N)
            break;
    }
    }
    end = clock();

    printf("For N=%lld, M=%lld, cubed is %lld which ends in %lld\n", N, M, M*M*M, (M*M*M)%1000);

    printf("Time taken: %f sec for 10,000 loops\n", ((float)(end - start) ) / CLOCKS_PER_SEC);

    return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
长整型N,M,val;
int ii;
开始、结束的时间;
如果(argc==2)
N=strtoul(argv[1],NULL,10);
其他的
{
printf(“N的值是多少?\N”);
scanf(“%lld”、&N);
}
如果(~N&1)
{
printf(“无效输入\n”);
返回EINVAL;
}
开始=时钟();

对于(ii=0;ii让我们取数字N并重复计算其三次幂(模1000):

N0 = N
N1 = N0^3 mod 1000
N2 = N1^3 mod 1000
N3 = N2^3 mod 1000
...
似乎如果
N=123
,那么
N19=947
,这就很容易计算出立方根。可以证明这个过程会给我们一个答案(堆栈溢出位置不是进行证明的合适位置,所以请相信我或继续提问).这一过程并不明显比任何其他方法都快,但似乎已经足够快了;据我所知,它应该比蛮力方法快,而且有很多改进

某些代码(基于原始代码):


注意:这假设计算
j*j*j
没有溢出(小于2^63)。这对于多达6位的
N
来说已经足够了,这可能不够好,也可能不够好。

对于哪个输入,这个程序超过了时间限制?没有输入。这是一个在线比赛的一部分。输入文件未知。我只是时间效率更高的代码相关:它仍然超过了时间限制。我们最终没有原始的输入。没有我不知道你什么时候启动计时器,也不知道你在什么平台上运行。我只是运行了我的代码,添加了10000次循环(在得到输入N之后,在打印输出M之前)。这需要0.13秒才能运行,或者13微秒才能找到M。可能是因为您的计时器包含了输入数字所需的时间(这是程序中最慢的一步)?它没有超过之前的时间尝试运行我刚刚发布的确切代码,并使用
123
作为输入。@SwapnanilSaha:您知道发布的代码计算结果10000次,而不是仅计算一次吗?如果您对此进行调整,而不是简单地复制和粘贴此代码,那么它可能会超过0.0005秒,但不会超过0.0005秒我你会注意到的。为什么你允许M一直到100000?如果N不超过三位数,M永远不超过N(根据问题),你设置了一个非常宽泛的上限……但和你一样,我对原始代码看起来如此缓慢感到困惑(除非,正如我所怀疑的,他正在为整个程序计时,包括输入循环…@Floris:是的,我已经调整了它。请参见(编辑)注释。但是M的位数不超过N,它仍然可以更大(尽管对于
125
来说,正确答案是
5
)这就是为什么我在我的代码中使用了
div
来做这个把戏-找到一个足够大但不是疯狂的大的数字。然后我发现你可能必须超过N。如果N=11,M=71…实际上你需要用
div
来替换我有
1000
的几个地方。我会更新我的代码。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

int main(int argc, char **argv)
{
    long long int N, M, val;
    int ii;
    time_t start, end;

    if (argc == 2)
        N = strtoul (argv[1], NULL, 10);
    else
    {
        printf("what is the value of N?\n");
        scanf("%lld", &N);
    }

    if (~N & 1)
    {
        printf ("invalid input\n");
        return EINVAL;
    }

    start = clock();
    for (ii=0; ii<10000; ii++)
    {
    for (M=1; M<1000; M+=2)
    {
        val = M%1000;
        val = val*val*val;
        if ((val % 1000) == N)
            break;
    }
    }
    end = clock();

    printf("For N=%lld, M=%lld, cubed is %lld which ends in %lld\n", N, M, M*M*M, (M*M*M)%1000);

    printf("Time taken: %f sec for 10,000 loops\n", ((float)(end - start) ) / CLOCKS_PER_SEC);

    return 0;
}
N0 = N
N1 = N0^3 mod 1000
N2 = N1^3 mod 1000
N3 = N2^3 mod 1000
...
while(1)
{
    c = j * j * j % s;
    if (c == p)
       break;
    j = c;
}