數組-04. 查找整數(10)

本題要求從輸入的N個整數中查找給定的X。如果找到,輸出X的位置(從0開始數);如果沒有找到,輸出“Not Found”。

輸入格式:

輸入在第1行中給出2個正整數N(<=20)和X,第2行給出N個整數。數字均不超過長整型,其間以空格分隔。

輸出格式:

在一行中輸出X的位置,或者“Not Found”。

輸入樣例1:
5 7
3 5 7 1 9
輸出樣例1:
2
輸入樣例2:
5 7
3 5 8 1 9
輸出樣例2:
Not Found

#include<stdio.h>

int main(){
    int a[21];
    int n,x,count=0;
    scanf("%d %d",&n,&x);   
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<n;i++){
        if(x!=a[i])
            count++;
        else
            printf("%d",i);
        }

    if(count==n)
        printf("Not Found");

return 0;
}
發佈了58 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章