[英]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.