预测数字
我希望用户输入一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
总共 3 个问题:
问题 1:
您的
arr
是一个悬空指针,并且您在scanf
中取消引用它。您需要:
代替
问题 2:
涉及
j
和k
的比较被错误地括起来:应该是
问题 3:
即使进行了上述 2 个修复,您的程序仍将陷入无限循环,因为您的
for
循环没有终止条件。由于您要求用户输入 4 位数字,因此您的所有循环应从
0
到9
如下:也为其他 3 个循环添加类似的检查。
Total of 3 problems:
Problem 1:
Your
arr
is a dangling pointer and you are dereferencing it inscanf
.You need:
in place of
Problem 2:
The comparison involving
j
andk
is incorrectly paranthesized:should be
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
till9
as:Add similar check for other 3 loops aswell.
关于您问题的这个特定部分,请考虑一下您尝试实现的算法。您拥有可用的号码,存储在
arr
中。如果用户选择数字9999
,您将在到达该数字之前迭代 10000 个数字。相反,如果您每次迭代一位数字并在找到正确的数字时停止(因为它是事先已知的),那么您将迭代 40 次。就数学复杂性而言,您当前的算法的最坏情况性能为 10n,而它可以实现为 10n。
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 number9999
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.
我发现了几个问题:
您没有为
*arr
分配任何内存。也许您应该将arr
定义为然后,在 scanf 中,您可以执行以下操作:
您可以在问题行上使用数组偏移表示法:
I see a couple of problems:
You're not allocating any memory for
*arr
. Perhaps you should definearr
asThen, in scanf, you can do something like:
You can just use the array offsets notation on the problem line:
你的括号放错地方了。
*(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, butarr[1]==j
(etc.) would be even better.考虑你最里面的循环
如果用户输入的数字不是以 000 开头的数字 - 这个循环将如何终止?这不会只是无限循环 i, j, k ==0 吗?
Consider your innermost loop
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 ?
您没有为 arr 分配任何空间!使用malloc分配空间。
另外,您正在将布尔值(c 中的 int)转换为地址!错误行中的大括号是错误的。
You didn't allocate any space for arr! Allocate the space using malloc.
Also, you are converting a boolean (int in c) into an address! The braces are wrong in the line of error.