[英]Trying to print one thing if found element in array, or print something else if not in array in C
如果试图在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]不是一个完美的正方形数十次。 我怎样才能做到这一点? 我花了几个小时解决这个问题。
您显示的代码非常耗时,因为如果数字为999,则需要迭代数千次。
使用math.h
sqrt()
函数查找给定数字是否为理想平方
尝试一下。
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");
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;
}
我正在尝试在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
使用库函数的另一种方法:
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;
}
“我试图不使用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
}
}
尝试这个 :
#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
这是所要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.