预测数字

发布于 2024-09-27 05:21:15 字数 745 浏览 0 评论 0原文

我希望用户输入一个 4 位数字,程序必须告诉这个 4 位数字是什么,即通过暴力攻击生成该 4 位数字。但是在下面提到的行中,编译器说无效间接。我也想有关于我实施它的方式的一些评论,这是一个很好的做法吗?

#include<stdio.h>
void BruteForceAttack(int *arr);
int main()
{
 int *arr,i;
 printf("Enter 4 digits ,press enter after entring each digit:\n");
 for(i=0;i<4;i++)
 scanf("%d",arr+i);
 BruteForceAttack(arr);
 getchar();
 return 0;
}
void BruteForceAttack(int *arr)
{
 int i,j,k,l;
 for(i=0;;i++)
 {
  for(j=0;;j++)
  {
   for(k=0;;k++)
   {
    for(l=0;;l++)
    {
      if((*(arr+0)==i)&&(*(arr+1==j))&&(*(arr+2==k))&&(*(arr+3)==l))   /*Here the compiler says invalid indirection*/
     {
      printf("The number is %d%d%d%d",i,j,k,l);
      return;
     }
    }
   }
  }
 }
}

I want the user to enter a 4 digit number and the program must tell what that 4 digit number was i.e generate that 4 digit number by Brute force attack.But at the line mentioned below the compiler says invalid indirection.I would also like to have some comments about they way I am implementing it,is it a good practise?

#include<stdio.h>
void BruteForceAttack(int *arr);
int main()
{
 int *arr,i;
 printf("Enter 4 digits ,press enter after entring each digit:\n");
 for(i=0;i<4;i++)
 scanf("%d",arr+i);
 BruteForceAttack(arr);
 getchar();
 return 0;
}
void BruteForceAttack(int *arr)
{
 int i,j,k,l;
 for(i=0;;i++)
 {
  for(j=0;;j++)
  {
   for(k=0;;k++)
   {
    for(l=0;;l++)
    {
      if((*(arr+0)==i)&&(*(arr+1==j))&&(*(arr+2==k))&&(*(arr+3)==l))   /*Here the compiler says invalid indirection*/
     {
      printf("The number is %d%d%d%d",i,j,k,l);
      return;
     }
    }
   }
  }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

偷得浮生 2024-10-04 05:21:15

总共 3 个问题:

问题 1:

您的 arr 是一个悬空指针,并且您在 scanf 中取消引用它。

您需要:

int arr[4]; 

代替

int *arr;

问题 2:

涉及 jk 的比较被错误地括起来:

&&(*(arr+1==j))&&(*(arr+2==k))

应该是

&&(*(arr+1)==j)&&(*(arr+2)==k)
          ^              ^

问题 3:

即使进行了上述 2 个修复,您的程序仍将陷入无限循环,因为您的 for 循环没有终止条件。

由于您要求用户输入 4 位数字,因此您的所有循环应从 09 如下:

for(i=0;i<10;i++)
        ^^^^^

也为其他 3 个循环添加类似的检查。

Total of 3 problems:

Problem 1:

Your arr is a dangling pointer and you are dereferencing it in scanf.

You need:

int arr[4]; 

in place of

int *arr;

Problem 2:

The comparison involving j and k is incorrectly paranthesized:

&&(*(arr+1==j))&&(*(arr+2==k))

should be

&&(*(arr+1)==j)&&(*(arr+2)==k)
          ^              ^

Problem 3:

Even with above 2 fixes your program will run into infinite loop, because your for loops have no terminating conditions.

Since you are asking user to enter 4 digits, all your loop should go from 0 till 9 as:

for(i=0;i<10;i++)
        ^^^^^

Add similar check for other 3 loops aswell.

ま柒月 2024-10-04 05:21:15

我也想要一些
关于他们的评论
实施它,这是一个好的做法吗?

关于您问题的这个特定部分,请考虑一下您尝试实现的算法。您拥有可用的号码,存储在 arr 中。如果用户选择数字 9999,您将在到达该数字之前迭代 10000 个数字。相反,如果您每次迭代一位数字并在找到正确的数字时停止(因为它是事先已知的),那么您将迭代 40 次。

就数学复杂性而言,您当前的算法的最坏情况性能为 10n,而它可以实现为 10n。

I would also like to have some
comments about they way I am
implementing it,is it a good practise?

With regards to this particular portion of your question, consider the algorithm you're trying to implement for a moment. You have the numbers available to you, stored in arr. If the user picks the number 9999 you will iterate through 10000 numbers before you reach it. Conversely, if you iterate through each digit one at a time and stop when you find the correct digit (since it is known beforehand) you iterate 40 times.

In terms of mathematical complexity, your current algorithm has a worst-case performance of 10n, whereas it could be implemented as 10n.

若能看破又如何 2024-10-04 05:21:15

我发现了几个问题:

  1. 您没有为 *arr 分配任何内存。也许您应该将 arr 定义为

    int arr[4];
    

    然后,在 scanf 中,您可以执行以下操作:

    scanf("%d", &arr[i]);
    
  2. 您可以在问题行上使用数组偏移表示法:

    if(arr[0] == i && arr[1] == j && arr[2] == k && arr[3] == l)
    

I see a couple of problems:

  1. You're not allocating any memory for *arr. Perhaps you should define arr as

    int arr[4];
    

    Then, in scanf, you can do something like:

    scanf("%d", &arr[i]);
    
  2. You can just use the array offsets notation on the problem line:

    if(arr[0] == i && arr[1] == j && arr[2] == k && arr[3] == l)
    
荒路情人 2024-10-04 05:21:15

你的括号放错地方了。 *(arr+1==j) 应该是 *(arr+1)==j 等。这将修复编译器警告,但是 arr[ 1]==j(等等)会更好。

Your parentheses are misplaced. *(arr+1==j) should be *(arr+1)==j, etc. That will fix the compiler warning, but arr[1]==j (etc.) would be even better.

盛装女皇 2024-10-04 05:21:15

考虑你最里面的循环

for(l=0;;l++)
    {
      if((*(arr+0)==i)&&(*(arr+1==j))&&(*(arr+2==k))&&(*(arr+3)==l))   /*Here the compiler says invalid indirection*/
     {
      printf("The number is %d%d%d%d",i,j,k,l);
      return;
     }
    }

如果用户输入的数字不是以 000 开头的数字 - 这个循环将如何终止?这不会只是无限循环 i, j, k ==0 吗?

Consider your innermost loop

for(l=0;;l++)
    {
      if((*(arr+0)==i)&&(*(arr+1==j))&&(*(arr+2==k))&&(*(arr+3)==l))   /*Here the compiler says invalid indirection*/
     {
      printf("The number is %d%d%d%d",i,j,k,l);
      return;
     }
    }

If the number entered by the user is anything that is not staring with 000 - how will this loop ever get terminated? Will this not just go on looping infinitely for i, j, k ==0 ?

阿楠 2024-10-04 05:21:15

您没有为 arr 分配任何空间!使用malloc分配空间。

...
int *arr,i;
arr = (int *) malloc(4*sizeof(int));
...

另外,您正在将布尔值(c 中的 int)转换为地址!错误行中的大括号是错误的。

...
if((*(arr+0)==i)&&((*(arr+1)==j))&&((*(arr+2)==k))&&((*(arr+3)==l))
..

You didn't allocate any space for arr! Allocate the space using malloc.

...
int *arr,i;
arr = (int *) malloc(4*sizeof(int));
...

Also, you are converting a boolean (int in c) into an address! The braces are wrong in the line of error.

...
if((*(arr+0)==i)&&((*(arr+1)==j))&&((*(arr+2)==k))&&((*(arr+3)==l))
..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文