2

如果在 for 循环中找到某个元素,我会尝试打印一件事,如果找不到,我会尝试打印其他内容。这应该很简单,但是我尝试了很多不同的方法,但似乎都没有奏效。

int squaresArray[1000];
int numberOfSquares = 1000;
int i = 0;
int found = 0;
int number = 100;

for (; i<numberOfSquares; i++)
{
    squaresArray[i] = i*i;
    if (number==squaresArray[i])
    {
        found = 1;
    }
            if (found == 1){
                printf("%d is a perfect square", number); 
                break;}
            else {
                printf("%d is not a perfect square", number);
                break;} 
    }

有几个问题,“找到”变量超出了 if 语句的范围,所以我不能在 if 语句之外执行 printf 部分,或者它只是打印“[number] is not a perfect square “几十次。我怎样才能做到这一点?我在这个问题上花了几个小时。

4

6 回答 6

1

您显示的代码非常耗时,因为如果数字是平方 999,您需要迭代一千次。

使用sqrt()函数 frommath.h来查找给定的数字是否是完美的平方不是

试试这个。

double param = 1024.0; //read different inputs with the help of scanf(). 
int i;

if ( ( (  i= (int) (sqrt(param)*10)  ) % 10) == 0 )  

      printf(" is a perfect square");
else
      printf(" is not a perfect square");

来自@jongware 的评论,这比上面的要复杂且容易理解。

   if ( ((int)sqrt(param))*((int)sqrt(param))==param)  

          printf(" is a perfect square");
    else
          printf(" is not a perfect square");     
于 2013-10-05T23:40:08.407 回答
0

使用库函数的另一种方法:

  int size = numberOfSquares / sizeof squaresArray[0];

  qsort(ints, size, sizeof(int), int_cmp);

  int *res = bsearch(&number, squaresArray, size, sizeof(squaresArrays[0]), int_cmp);

  if (res == NULL) {
        printf("%d not found\n", number);
  } 
  else {
        printf("got %d:\n", *res);
  }

您还需要提供比较功能:

int int_cmp(const void* a, const void* b) {
    const int *arg1 = a;
    const int *arg2 = b;
    return *arg1 - *arg2;
}
于 2013-10-05T23:32:40.290 回答
0

“我试图在不使用 math.h 的情况下对其进行编码”

这是不使用<math.h>库的解决方案。

我想,您知道您的程序可以找到唯一的“完美正方形” :) 无论如何,请参阅评论:他们会描述一些东西。

使用continue而不是break为了不破坏for-loop。

    int squaresArray[1000];
    int numberOfSquares = 999;
    int i;
    int found = 0;
    int number = 100;


    for ( i = 0 ; i < numberOfSquares; i++ )
    {
        squaresArray[i] = i*i;
        if (number == squaresArray[i])
            found = 1;
        else
            found = 0;

        if (found == 1){
            printf("%d is a perfect square", number);
            continue;   // Skipping the current loop and incrementing the `i` variable
        }
        else {
         // printf("%d is not a perfect square", number); 
         /* If you remove the slashes, you'll see 999 "N is not a perfect square"s,
            so you see why I've marked it as comment :) */
            continue;   // Skipping the current loop and incrementing the `i` variable
        }
    }
于 2013-10-05T23:44:09.950 回答
0

尝试这个 :

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

int main(){

int squaresArray[1000];
int numberOfSquares = 1000;
int i;
int found = 0;
int number = 100;

for (i=0; i<numberOfSquares; i++){
    squaresArray[i] = i*i;

    if (number==squaresArray[i]){
        found = 1;
        break;
    }
 }

if (found == 1){
      printf("%d is a perfect square of %d\n", number,i); 
}else {
      printf("%d is not a perfect square", number);

}
return 0;
}

这给了我以下输出:

100 is a perfect square of 10

这是想要的吗?

于 2013-10-05T23:49:46.903 回答
0
int squaresArray[1000];
int numberOfSquares = 1000;
int i = 0;
int found = 0;
int number = 0;

for (; i<numberOfSquares; i++)
{
    squaresArray[i] = i*i;
    if (number==squaresArray[i]){
        found = 1;
        break;
     }
}
if (found == 1){
                printf("%d is a perfect square", number); 
                break;}
            else {
                printf("%d is not a perfect square", number);
                break; 
    }
于 2013-10-05T23:07:57.673 回答
0

如果在 for 循环中找到某个元素,我将尝试打印一件事,如果未找到,我将尝试打印其他内容

伪代码:

int found = 0;
for each element e {
    if e is the one I'm looking for {
        found = 1
        stop the loop
    }
}
if (found)
    print one thing
else
    print something else
于 2013-10-05T23:13:53.400 回答