第一題、查找給定數

這是我第一次寫博客,希望能夠與大家多多交流!


/*
 * 本題要求從輸入的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
 */

import java.util.Scanner;

public class O {

    private static Scanner sc;

    public static void main(String[] args) {
        
        sc = new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        boolean b=false;
        
        int[] a=new int[n];
        
        for(int i=0;i<a.length;i++){
            a[i]=sc.nextInt();
        }

        for(int i=0;i<a.length;i++){
            if(a[i]==m){
                b=true;
                System.out.println(i);
            }        
        }
        
        if(!b){
            System.out.println("Not Found");
        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章